From 311ab744fe1bf278b18c25892497271988399e9a Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Tue, 4 Jul 2023 18:38:10 +0200 Subject: feat: Add user based files Now files follow user path configuration --- pkg/service/filesystem.go | 33 ++++++++++++++++++++++----------- pkg/service/main_test.go | 8 ++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) (limited to 'pkg/service') diff --git a/pkg/service/filesystem.go b/pkg/service/filesystem.go index 3516ce2..cdfd106 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 @@ type ( } ) -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 @@ func getHistory(filepath string) []*DirectoryParam { 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 5c10ecd..e1214dc 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") +} -- cgit v1.2.3