From 14e5580efd51c7b9e70d304715e512a2ea2a1b21 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Fri, 1 Dec 2023 00:26:06 +0100 Subject: feat: Add option to download file --- pkg/view/filesystem.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'pkg/view') 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 } -- cgit v1.2.3