From 7a414da9a802d5eeee911b3536790a061e1d7503 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Thu, 29 Jun 2023 23:33:02 +0200 Subject: ref: Move all controller under the same folder Move all controller to the same folder and rename them to service. Moving them to the same folder allow an easier setup for testing. --- pkg/components/filesystem/controller.go | 91 --------------------------------- 1 file changed, 91 deletions(-) delete mode 100644 pkg/components/filesystem/controller.go (limited to 'pkg/components/filesystem/controller.go') diff --git a/pkg/components/filesystem/controller.go b/pkg/components/filesystem/controller.go deleted file mode 100644 index 6c613a3..0000000 --- a/pkg/components/filesystem/controller.go +++ /dev/null @@ -1,91 +0,0 @@ -package filesystem - -import ( - "io/fs" - "net/url" - "path" - "strings" - - "git.sr.ht/~gabrielgio/img/pkg/database/repository" -) - -type ( - Controller struct { - repository repository.FileSystemRepository - } - - DirectoryParam struct { - Name string - UrlEncodedPath string - } - - FileParam struct { - UrlEncodedPath string - Info fs.FileInfo - } - - Page struct { - History []*DirectoryParam - Files []*FileParam - } -) - -func NewController(repository repository.FileSystemRepository) *Controller { - return &Controller{ - repository: repository, - } -} - -func getHistory(filepath string) []*DirectoryParam { - var ( - paths = strings.Split(filepath, "/") - result = make([]*DirectoryParam, 0, len(paths)) - acc = "" - ) - - // add root folder - result = append(result, &DirectoryParam{ - Name: "...", - UrlEncodedPath: "", - }) - - if len(paths) == 1 && paths[0] == "" { - return result - } - - for _, p := range paths { - acc = path.Join(acc, p) - result = append(result, &DirectoryParam{ - Name: p, - UrlEncodedPath: url.QueryEscape(acc), - }) - } - return result -} - -func (self *Controller) GetPage(filepath string) (*Page, error) { - decodedPath, err := url.QueryUnescape(filepath) - if err != nil { - return nil, err - } - - files, err := self.repository.List(decodedPath) - if err != nil { - return nil, err - } - - params := make([]*FileParam, 0, len(files)) - for _, info := range files { - fullPath := path.Join(decodedPath, info.Name()) - scapedFullPath := url.QueryEscape(fullPath) - params = append(params, &FileParam{ - Info: info, - UrlEncodedPath: scapedFullPath, - }) - } - - return &Page{ - Files: params, - History: getHistory(decodedPath), - }, nil -} -- cgit v1.2.3