lens @ 311ab744fe1bf278b18c25892497271988399e9a

feat: Add user based files

Now files follow user path configuration
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 224b37a3b9bc401adccc92f285e19642cb7be633..c064d569c75c289e6dd9b3a436666431eb4b1a01 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -98,7 +98,7 @@
 	// controller
 	var (
 		userController       = service.NewAuthController(userRepository, userRepository, hexKey)
-		fileSystemController = service.NewFileSystemController(fileSystemRepository)
+		fileSystemController = service.NewFileSystemController(fileSystemRepository, userRepository)
 	)
 
 	// view
diff --git a/pkg/database/repository/user.go b/pkg/database/repository/user.go
index f8bd71921567ddf5ed5bdd3ed15e4e526f3bad64..358900782c1a41ec5bd7f0d068c03c7b1d80edfd 100644
--- a/pkg/database/repository/user.go
+++ b/pkg/database/repository/user.go
@@ -27,6 +27,7 @@ 	}
 
 	UserRepository interface {
 		Get(ctx context.Context, id uint) (*User, error)
+		GetPathFromUserID(ctx context.Context, id uint) (string, error)
 		List(ctx context.Context) ([]*User, error)
 		Create(ctx context.Context, createUser *CreateUser) (uint, error)
 		Update(ctx context.Context, id uint, updateUser *UpdateUser) error
diff --git a/pkg/database/sql/user.go b/pkg/database/sql/user.go
index 479a9c59d36cb407f4693b663c409eb71701ee6b..11718be860a23eee340db7bc8bc15046681e532f 100644
--- a/pkg/database/sql/user.go
+++ b/pkg/database/sql/user.go
@@ -201,3 +201,20 @@ 	}
 
 	return exists, nil
 }
+
+func (u *UserRepository) GetPathFromUserID(ctx context.Context, id uint) (string, error) {
+	var userPath string
+
+	result := u.db.
+		WithContext(ctx).
+		Model(&User{}).
+		Select("path").
+		Where("id = ?", id).
+		First(&userPath)
+
+	if result.Error != nil {
+		return "", result.Error
+	}
+
+	return userPath, nil
+}
diff --git a/pkg/ext/middleware.go b/pkg/ext/middleware.go
index c83b9989cd8e0719062d77df3c6cdae69e5c63c1..2dd1cca57f49e61df5123bdb61fa71dd6439d99d 100644
--- a/pkg/ext/middleware.go
+++ b/pkg/ext/middleware.go
@@ -92,6 +92,14 @@ 		next(ctx)
 	}
 }
 
+func GetTokenFromCtx(ctx *fasthttp.RequestCtx) *Token {
+	tokenValue := ctx.UserValue("token")
+	if token, ok := tokenValue.(*Token); ok {
+		return token
+	}
+	return nil
+}
+
 type InitialSetupMiddleware struct {
 	userRepository repository.UserRepository
 }
diff --git a/pkg/service/filesystem.go b/pkg/service/filesystem.go
index 3516ce2642ba63e2d8ef7f774469657d64123c70..cdfd106746896e4f7ad9cca777b4595346443258 100644
--- a/pkg/service/filesystem.go
+++ b/pkg/service/filesystem.go
@@ -1,17 +1,20 @@
 package service
 
 import (
+	"context"
 	"io/fs"
 	"net/url"
 	"path"
 	"strings"
 
 	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
+	"git.sr.ht/~gabrielgio/img/pkg/list"
 )
 
 type (
 	FileSystemController struct {
-		repository repository.FileSystemRepository
+		fsRepository   repository.FileSystemRepository
+		userRepository repository.UserRepository
 	}
 
 	DirectoryParam struct {
@@ -30,9 +33,13 @@ 		Files   []*FileParam
 	}
 )
 
-func NewFileSystemController(repository repository.FileSystemRepository) *FileSystemController {
+func NewFileSystemController(
+	fsRepository repository.FileSystemRepository,
+	userRepository repository.UserRepository,
+) *FileSystemController {
 	return &FileSystemController{
-		repository: repository,
+		fsRepository:   fsRepository,
+		userRepository: userRepository,
 	}
 }
 
@@ -63,26 +70,30 @@ 	}
 	return result
 }
 
-func (self *FileSystemController) GetPage(filepath string) (*Page, error) {
+func (self *FileSystemController) GetPage(ctx context.Context, userID uint, filepath string) (*Page, error) {
+	userPath, err := self.userRepository.GetPathFromUserID(ctx, userID)
+	if err != nil {
+		return nil, err
+	}
 	decodedPath, err := url.QueryUnescape(filepath)
 	if err != nil {
 		return nil, err
 	}
 
-	files, err := self.repository.List(decodedPath)
+	fullPath := path.Join(userPath, decodedPath)
+	files, err := self.fsRepository.List(fullPath)
 	if err != nil {
 		return nil, err
 	}
 
-	params := make([]*FileParam, 0, len(files))
-	for _, info := range files {
-		fullPath := path.Join(decodedPath, info.Name())
+	params := list.Map(files, func(info fs.FileInfo) *FileParam {
+		fullPath := path.Join(fullPath, info.Name())
 		scapedFullPath := url.QueryEscape(fullPath)
-		params = append(params, &FileParam{
+		return &FileParam{
 			Info:           info,
 			UrlEncodedPath: scapedFullPath,
-		})
-	}
+		}
+	})
 
 	return &Page{
 		Files:   params,
diff --git a/pkg/service/main_test.go b/pkg/service/main_test.go
index 5c10ecd7fc5d57b30c8344a2b00354b26cdbbef1..e1214dccec1e7c9fcf99fd4697db2452b6400e07 100644
--- a/pkg/service/main_test.go
+++ b/pkg/service/main_test.go
@@ -119,3 +119,11 @@ func (u *UserRepository) furtherID() uint {
 	u.icount++
 	return u.icount
 }
+
+func (u *UserRepository) GetPathFromUserID(ctx context.Context, id uint) (string, error) {
+	if user, ok := u.users[id]; ok {
+		return user.Path, nil
+	}
+
+	return "", errors.New("Not Found")
+}
diff --git a/pkg/view/filesystem.go b/pkg/view/filesystem.go
index d598b88014738c96532ea5b265f804ac9f87fcf2..7fd7331c7b306a61afbadee990e5a9c0293d51d1 100644
--- a/pkg/view/filesystem.go
+++ b/pkg/view/filesystem.go
@@ -11,8 +11,8 @@ )
 
 type (
 	FileSystemView struct {
-		controller service.FileSystemController
-		settings   repository.SettingsRepository
+		fsService service.FileSystemController
+		settings  repository.SettingsRepository
 	}
 	FilePage struct {
 		Page      *service.Page
@@ -26,15 +26,16 @@ 	controller service.FileSystemController,
 	settingsRepository repository.SettingsRepository,
 ) *FileSystemView {
 	return &FileSystemView{
-		controller: controller,
-		settings:   settingsRepository,
+		fsService: controller,
+		settings:  settingsRepository,
 	}
 }
 
 func (self *FileSystemView) Index(ctx *fasthttp.RequestCtx) error {
 	pathValue := string(ctx.FormValue("path"))
+	token := ext.GetTokenFromCtx(ctx)
 
-	page, err := self.controller.GetPage(pathValue)
+	page, err := self.fsService.GetPage(ctx, token.UserID, pathValue)
 	if err != nil {
 		return err
 	}
diff --git a/pkg/view/media.go b/pkg/view/media.go
index 0b588f4f4c52b7aff77b48979490d43e703b95f8..bea515d9e6657d47e21c4136a57a94995734b0f8 100644
--- a/pkg/view/media.go
+++ b/pkg/view/media.go
@@ -98,7 +98,9 @@ 	pathHash := string(ctx.FormValue("path_hash"))
 
 	path, err := self.mediaRepository.GetThumbnailPath(ctx, pathHash)
 	if err != nil {
-		return self.GetImage(ctx)
+		ctx.Redirect("/media/image?path_hash="+pathHash, 307)
+		// nolint: nilerr
+		return nil
 	}
 
 	ctx.Request.Header.SetContentType("image/jpeg")