diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2023-12-01 00:26:06 +0100 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2023-12-01 00:26:06 +0100 |
commit | 14e5580efd51c7b9e70d304715e512a2ea2a1b21 (patch) | |
tree | 8d118d355f631fd9225354d8dc65b4b225946489 /pkg/view | |
parent | c3ea735c0f03a0827a8e753a5b5adf6e31f4c925 (diff) | |
download | lens-14e5580efd51c7b9e70d304715e512a2ea2a1b21.tar.gz lens-14e5580efd51c7b9e70d304715e512a2ea2a1b21.tar.bz2 lens-14e5580efd51c7b9e70d304715e512a2ea2a1b21.zip |
feat: Add option to download file
Diffstat (limited to 'pkg/view')
-rw-r--r-- | pkg/view/filesystem.go | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/pkg/view/filesystem.go b/pkg/view/filesystem.go index 9071ec0..f78f8a6 100644 --- a/pkg/view/filesystem.go +++ b/pkg/view/filesystem.go @@ -1,7 +1,9 @@ package view import ( + "mime" "net/http" + "path/filepath" "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/ext" @@ -37,7 +39,24 @@ func (self *FileSystemView) Index(w http.ResponseWriter, r *http.Request) error user = ext.GetUserFromCtx(r) ) - page, err := self.fsService.GetPage(r.Context(), user.ID, pathValue) + fullpath, err := self.fsService.GetFullpath(r.Context(), user.ID, pathValue) + if err != nil { + return err + } + + isFile, err := self.fsService.IsFile(r.Context(), fullpath) + if err != nil { + return err + } + + if isFile { + mimetype := mime.TypeByExtension(filepath.Ext(fullpath)) + w.Header().Set("Content-Type", mimetype) + http.ServeFile(w, r, fullpath) + return nil + } + + page, err := self.fsService.GetPage(r.Context(), pathValue, fullpath) if err != nil { return err } |