aboutsummaryrefslogtreecommitdiff
path: root/pkg/service
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/service')
-rw-r--r--pkg/service/filesystem.go33
-rw-r--r--pkg/service/main_test.go8
2 files changed, 30 insertions, 11 deletions
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")
+}