diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-29 23:33:02 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-29 23:33:02 +0200 |
commit | 7a414da9a802d5eeee911b3536790a061e1d7503 (patch) | |
tree | e009cd94e72bf908701ca6067833ed14f6860b43 /pkg/service/filesystem.go | |
parent | 1ae70dbd9124675d4a510954619b01edd5f1f6c3 (diff) | |
download | lens-7a414da9a802d5eeee911b3536790a061e1d7503.tar.gz lens-7a414da9a802d5eeee911b3536790a061e1d7503.tar.bz2 lens-7a414da9a802d5eeee911b3536790a061e1d7503.zip |
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.
Diffstat (limited to 'pkg/service/filesystem.go')
-rw-r--r-- | pkg/service/filesystem.go | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/pkg/service/filesystem.go b/pkg/service/filesystem.go new file mode 100644 index 0000000..3516ce2 --- /dev/null +++ b/pkg/service/filesystem.go @@ -0,0 +1,91 @@ +package service + +import ( + "io/fs" + "net/url" + "path" + "strings" + + "git.sr.ht/~gabrielgio/img/pkg/database/repository" +) + +type ( + FileSystemController struct { + repository repository.FileSystemRepository + } + + DirectoryParam struct { + Name string + UrlEncodedPath string + } + + FileParam struct { + UrlEncodedPath string + Info fs.FileInfo + } + + Page struct { + History []*DirectoryParam + Files []*FileParam + } +) + +func NewFileSystemController(repository repository.FileSystemRepository) *FileSystemController { + return &FileSystemController{ + 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 *FileSystemController) 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 +} |