From 1031ad7e8c18facd6427b30bb60e5b1449d4ada0 Mon Sep 17 00:00:00 2001 From: Tueem Date: Sun, 8 Feb 2026 22:46:58 +0100 Subject: [PATCH] feat(template): add Fileserver for download --- internal/command/commandline.go | 2 +- internal/server/http.go | 20 ++++++++++++++++---- pkg/svg/storage.go | 16 +++++++++++----- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/internal/command/commandline.go b/internal/command/commandline.go index 32b528b..768256d 100644 --- a/internal/command/commandline.go +++ b/internal/command/commandline.go @@ -30,7 +30,7 @@ func HandleCommandline() { } else if help { flag.PrintDefaults() } else { - svg.Storage = svg.NewFileStorage(datapath) + svg.Storage = svg.NewFileStorage(datapath, "public") server.PrepareHTTP() server.Start() } diff --git a/internal/server/http.go b/internal/server/http.go index ab2c96e..e840cb3 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -4,22 +4,26 @@ import ( "fmt" "log" "net/http" + "path/filepath" "tomatentum.net/svg-templater/internal/routes" "tomatentum.net/svg-templater/pkg/auth" + "tomatentum.net/svg-templater/pkg/svg" ) func PrepareHTTP() { - registerAuthorized("/", func(w http.ResponseWriter, r *http.Request) { + registerAuthorizedFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "You are authorized!") }) - registerAuthorized("/svg/", func(w http.ResponseWriter, r *http.Request) { + registerAuthorizedFunc("/svg/", func(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } routes.CreateSVG(w, *r) }) + + registerAuthorized("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir()))) } func Start() { @@ -29,7 +33,15 @@ func Start() { } } -func registerAuthorized(path string, f func(w http.ResponseWriter, r *http.Request)) { - http.HandleFunc(path, auth.AuthMiddleware(http.HandlerFunc(f))) +func registerAuthorized(path string, handler http.Handler) { + http.HandleFunc(path, auth.AuthMiddleware(handler)) log.Println("Registered authorized handler for", path) } + +func registerAuthorizedFunc(path string, f func(w http.ResponseWriter, r *http.Request)) { + registerAuthorized(path, http.HandlerFunc(f)) +} + +func GetPublicPath(path string) string { + return filepath.Join("public", path) +} diff --git a/pkg/svg/storage.go b/pkg/svg/storage.go index 45d3e7b..343106b 100644 --- a/pkg/svg/storage.go +++ b/pkg/svg/storage.go @@ -3,6 +3,7 @@ package svg import ( "io" "log" + "net/http" "os" "path/filepath" ) @@ -12,22 +13,23 @@ type SvgStorage interface { Get(id string) (io.ReadCloser, error) CreatePublic(data io.Reader, filetype string) (string, error) GetPublic(path string) (io.ReadCloser, error) + GetPublicDir() http.Dir } var _ SvgStorage = FileSvgStorage{} var TempDir string = "" type FileSvgStorage struct { - basepath string + basepath, publicSubPath string } -func NewFileStorage(path string) *FileSvgStorage { +func NewFileStorage(path, publicSubPath string) *FileSvgStorage { err := os.MkdirAll(path, 0755) if err != nil { panic(err) } log.Println("Initialized file storage handler") - return &FileSvgStorage{path} + return &FileSvgStorage{path, publicSubPath} } func (f FileSvgStorage) Create(id string, svg io.Reader) (string, error) { @@ -56,7 +58,7 @@ func (f FileSvgStorage) Get(id string) (io.ReadCloser, error) { } func (f FileSvgStorage) CreatePublic(data io.Reader, filetype string) (string, error) { - path := filepath.Join(f.basepath, "public") + path := filepath.Join(f.basepath, f.publicSubPath) if err := os.Mkdir(path, 0755); err != nil { return "", err } @@ -71,13 +73,17 @@ func (f FileSvgStorage) CreatePublic(data io.Reader, filetype string) (string, e } func (f FileSvgStorage) GetPublic(path string) (io.ReadCloser, error) { - file, err := os.Open(filepath.Join(f.basepath, "public", path)) + file, err := os.Open(filepath.Join(f.basepath, f.publicSubPath, path)) if err != nil { return nil, err } return file, nil } +func (f FileSvgStorage) GetPublicDir() http.Dir { + return http.Dir(filepath.Join(f.basepath, f.publicSubPath)) +} + func CreateTemp(data io.Reader, filetype string) (string, error) { file, err := os.CreateTemp(TempDir, "*."+filetype) if err != nil {