lens @ 311ab744fe1bf278b18c25892497271988399e9a

feat: Add user based files

Now files follow user path configuration
  1diff --git a/cmd/server/main.go b/cmd/server/main.go
  2index 224b37a3b9bc401adccc92f285e19642cb7be633..c064d569c75c289e6dd9b3a436666431eb4b1a01 100644
  3--- a/cmd/server/main.go
  4+++ b/cmd/server/main.go
  5@@ -98,7 +98,7 @@
  6 	// controller
  7 	var (
  8 		userController       = service.NewAuthController(userRepository, userRepository, hexKey)
  9-		fileSystemController = service.NewFileSystemController(fileSystemRepository)
 10+		fileSystemController = service.NewFileSystemController(fileSystemRepository, userRepository)
 11 	)
 12 
 13 	// view
 14diff --git a/pkg/database/repository/user.go b/pkg/database/repository/user.go
 15index f8bd71921567ddf5ed5bdd3ed15e4e526f3bad64..358900782c1a41ec5bd7f0d068c03c7b1d80edfd 100644
 16--- a/pkg/database/repository/user.go
 17+++ b/pkg/database/repository/user.go
 18@@ -27,6 +27,7 @@ 	}
 19 
 20 	UserRepository interface {
 21 		Get(ctx context.Context, id uint) (*User, error)
 22+		GetPathFromUserID(ctx context.Context, id uint) (string, error)
 23 		List(ctx context.Context) ([]*User, error)
 24 		Create(ctx context.Context, createUser *CreateUser) (uint, error)
 25 		Update(ctx context.Context, id uint, updateUser *UpdateUser) error
 26diff --git a/pkg/database/sql/user.go b/pkg/database/sql/user.go
 27index 479a9c59d36cb407f4693b663c409eb71701ee6b..11718be860a23eee340db7bc8bc15046681e532f 100644
 28--- a/pkg/database/sql/user.go
 29+++ b/pkg/database/sql/user.go
 30@@ -201,3 +201,20 @@ 	}
 31 
 32 	return exists, nil
 33 }
 34+
 35+func (u *UserRepository) GetPathFromUserID(ctx context.Context, id uint) (string, error) {
 36+	var userPath string
 37+
 38+	result := u.db.
 39+		WithContext(ctx).
 40+		Model(&User{}).
 41+		Select("path").
 42+		Where("id = ?", id).
 43+		First(&userPath)
 44+
 45+	if result.Error != nil {
 46+		return "", result.Error
 47+	}
 48+
 49+	return userPath, nil
 50+}
 51diff --git a/pkg/ext/middleware.go b/pkg/ext/middleware.go
 52index c83b9989cd8e0719062d77df3c6cdae69e5c63c1..2dd1cca57f49e61df5123bdb61fa71dd6439d99d 100644
 53--- a/pkg/ext/middleware.go
 54+++ b/pkg/ext/middleware.go
 55@@ -92,6 +92,14 @@ 		next(ctx)
 56 	}
 57 }
 58 
 59+func GetTokenFromCtx(ctx *fasthttp.RequestCtx) *Token {
 60+	tokenValue := ctx.UserValue("token")
 61+	if token, ok := tokenValue.(*Token); ok {
 62+		return token
 63+	}
 64+	return nil
 65+}
 66+
 67 type InitialSetupMiddleware struct {
 68 	userRepository repository.UserRepository
 69 }
 70diff --git a/pkg/service/filesystem.go b/pkg/service/filesystem.go
 71index 3516ce2642ba63e2d8ef7f774469657d64123c70..cdfd106746896e4f7ad9cca777b4595346443258 100644
 72--- a/pkg/service/filesystem.go
 73+++ b/pkg/service/filesystem.go
 74@@ -1,17 +1,20 @@
 75 package service
 76 
 77 import (
 78+	"context"
 79 	"io/fs"
 80 	"net/url"
 81 	"path"
 82 	"strings"
 83 
 84 	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
 85+	"git.sr.ht/~gabrielgio/img/pkg/list"
 86 )
 87 
 88 type (
 89 	FileSystemController struct {
 90-		repository repository.FileSystemRepository
 91+		fsRepository   repository.FileSystemRepository
 92+		userRepository repository.UserRepository
 93 	}
 94 
 95 	DirectoryParam struct {
 96@@ -30,9 +33,13 @@ 		Files   []*FileParam
 97 	}
 98 )
 99 
100-func NewFileSystemController(repository repository.FileSystemRepository) *FileSystemController {
101+func NewFileSystemController(
102+	fsRepository repository.FileSystemRepository,
103+	userRepository repository.UserRepository,
104+) *FileSystemController {
105 	return &FileSystemController{
106-		repository: repository,
107+		fsRepository:   fsRepository,
108+		userRepository: userRepository,
109 	}
110 }
111 
112@@ -63,26 +70,30 @@ 	}
113 	return result
114 }
115 
116-func (self *FileSystemController) GetPage(filepath string) (*Page, error) {
117+func (self *FileSystemController) GetPage(ctx context.Context, userID uint, filepath string) (*Page, error) {
118+	userPath, err := self.userRepository.GetPathFromUserID(ctx, userID)
119+	if err != nil {
120+		return nil, err
121+	}
122 	decodedPath, err := url.QueryUnescape(filepath)
123 	if err != nil {
124 		return nil, err
125 	}
126 
127-	files, err := self.repository.List(decodedPath)
128+	fullPath := path.Join(userPath, decodedPath)
129+	files, err := self.fsRepository.List(fullPath)
130 	if err != nil {
131 		return nil, err
132 	}
133 
134-	params := make([]*FileParam, 0, len(files))
135-	for _, info := range files {
136-		fullPath := path.Join(decodedPath, info.Name())
137+	params := list.Map(files, func(info fs.FileInfo) *FileParam {
138+		fullPath := path.Join(fullPath, info.Name())
139 		scapedFullPath := url.QueryEscape(fullPath)
140-		params = append(params, &FileParam{
141+		return &FileParam{
142 			Info:           info,
143 			UrlEncodedPath: scapedFullPath,
144-		})
145-	}
146+		}
147+	})
148 
149 	return &Page{
150 		Files:   params,
151diff --git a/pkg/service/main_test.go b/pkg/service/main_test.go
152index 5c10ecd7fc5d57b30c8344a2b00354b26cdbbef1..e1214dccec1e7c9fcf99fd4697db2452b6400e07 100644
153--- a/pkg/service/main_test.go
154+++ b/pkg/service/main_test.go
155@@ -119,3 +119,11 @@ func (u *UserRepository) furtherID() uint {
156 	u.icount++
157 	return u.icount
158 }
159+
160+func (u *UserRepository) GetPathFromUserID(ctx context.Context, id uint) (string, error) {
161+	if user, ok := u.users[id]; ok {
162+		return user.Path, nil
163+	}
164+
165+	return "", errors.New("Not Found")
166+}
167diff --git a/pkg/view/filesystem.go b/pkg/view/filesystem.go
168index d598b88014738c96532ea5b265f804ac9f87fcf2..7fd7331c7b306a61afbadee990e5a9c0293d51d1 100644
169--- a/pkg/view/filesystem.go
170+++ b/pkg/view/filesystem.go
171@@ -11,8 +11,8 @@ )
172 
173 type (
174 	FileSystemView struct {
175-		controller service.FileSystemController
176-		settings   repository.SettingsRepository
177+		fsService service.FileSystemController
178+		settings  repository.SettingsRepository
179 	}
180 	FilePage struct {
181 		Page      *service.Page
182@@ -26,15 +26,16 @@ 	controller service.FileSystemController,
183 	settingsRepository repository.SettingsRepository,
184 ) *FileSystemView {
185 	return &FileSystemView{
186-		controller: controller,
187-		settings:   settingsRepository,
188+		fsService: controller,
189+		settings:  settingsRepository,
190 	}
191 }
192 
193 func (self *FileSystemView) Index(ctx *fasthttp.RequestCtx) error {
194 	pathValue := string(ctx.FormValue("path"))
195+	token := ext.GetTokenFromCtx(ctx)
196 
197-	page, err := self.controller.GetPage(pathValue)
198+	page, err := self.fsService.GetPage(ctx, token.UserID, pathValue)
199 	if err != nil {
200 		return err
201 	}
202diff --git a/pkg/view/media.go b/pkg/view/media.go
203index 0b588f4f4c52b7aff77b48979490d43e703b95f8..bea515d9e6657d47e21c4136a57a94995734b0f8 100644
204--- a/pkg/view/media.go
205+++ b/pkg/view/media.go
206@@ -98,7 +98,9 @@ 	pathHash := string(ctx.FormValue("path_hash"))
207 
208 	path, err := self.mediaRepository.GetThumbnailPath(ctx, pathHash)
209 	if err != nil {
210-		return self.GetImage(ctx)
211+		ctx.Redirect("/media/image?path_hash="+pathHash, 307)
212+		// nolint: nilerr
213+		return nil
214 	}
215 
216 	ctx.Request.Header.SetContentType("image/jpeg")