aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/server/main.go2
-rw-r--r--pkg/database/repository/user.go1
-rw-r--r--pkg/database/sql/user.go17
-rw-r--r--pkg/ext/middleware.go8
-rw-r--r--pkg/service/filesystem.go33
-rw-r--r--pkg/service/main_test.go8
-rw-r--r--pkg/view/filesystem.go11
-rw-r--r--pkg/view/media.go4
8 files changed, 66 insertions, 18 deletions
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 224b37a..c064d56 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -98,7 +98,7 @@ func main() {
// 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 f8bd719..3589007 100644
--- a/pkg/database/repository/user.go
+++ b/pkg/database/repository/user.go
@@ -27,6 +27,7 @@ type (
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 479a9c5..11718be 100644
--- a/pkg/database/sql/user.go
+++ b/pkg/database/sql/user.go
@@ -201,3 +201,20 @@ func (u *UserRepository) Any(ctx context.Context) (bool, error) {
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 c83b998..2dd1cca 100644
--- a/pkg/ext/middleware.go
+++ b/pkg/ext/middleware.go
@@ -92,6 +92,14 @@ func (a *AuthMiddleware) LoggedIn(next fasthttp.RequestHandler) fasthttp.Request
}
}
+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 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")
+}
diff --git a/pkg/view/filesystem.go b/pkg/view/filesystem.go
index d598b88..7fd7331 100644
--- a/pkg/view/filesystem.go
+++ b/pkg/view/filesystem.go
@@ -11,8 +11,8 @@ import (
type (
FileSystemView struct {
- controller service.FileSystemController
- settings repository.SettingsRepository
+ fsService service.FileSystemController
+ settings repository.SettingsRepository
}
FilePage struct {
Page *service.Page
@@ -26,15 +26,16 @@ func NewFileSystemView(
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 0b588f4..bea515d 100644
--- a/pkg/view/media.go
+++ b/pkg/view/media.go
@@ -98,7 +98,9 @@ func (self *MediaView) GetThumbnail(ctx *fasthttp.RequestCtx) error {
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")