feat(template): add Fileserver for download
Some checks failed
build / Go-Build (push) Failing after 12m37s

This commit is contained in:
2026-02-08 22:46:58 +01:00
parent 741b196497
commit 1031ad7e8c
3 changed files with 28 additions and 10 deletions

View File

@@ -30,7 +30,7 @@ func HandleCommandline() {
} else if help { } else if help {
flag.PrintDefaults() flag.PrintDefaults()
} else { } else {
svg.Storage = svg.NewFileStorage(datapath) svg.Storage = svg.NewFileStorage(datapath, "public")
server.PrepareHTTP() server.PrepareHTTP()
server.Start() server.Start()
} }

View File

@@ -4,22 +4,26 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"path/filepath"
"tomatentum.net/svg-templater/internal/routes" "tomatentum.net/svg-templater/internal/routes"
"tomatentum.net/svg-templater/pkg/auth" "tomatentum.net/svg-templater/pkg/auth"
"tomatentum.net/svg-templater/pkg/svg"
) )
func PrepareHTTP() { func PrepareHTTP() {
registerAuthorized("/", func(w http.ResponseWriter, r *http.Request) { registerAuthorizedFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "You are authorized!") 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" { if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
routes.CreateSVG(w, *r) routes.CreateSVG(w, *r)
}) })
registerAuthorized("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir())))
} }
func Start() { func Start() {
@@ -29,7 +33,15 @@ func Start() {
} }
} }
func registerAuthorized(path string, f func(w http.ResponseWriter, r *http.Request)) { func registerAuthorized(path string, handler http.Handler) {
http.HandleFunc(path, auth.AuthMiddleware(http.HandlerFunc(f))) http.HandleFunc(path, auth.AuthMiddleware(handler))
log.Println("Registered authorized handler for", path) 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)
}

View File

@@ -3,6 +3,7 @@ package svg
import ( import (
"io" "io"
"log" "log"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
) )
@@ -12,22 +13,23 @@ type SvgStorage interface {
Get(id string) (io.ReadCloser, error) Get(id string) (io.ReadCloser, error)
CreatePublic(data io.Reader, filetype string) (string, error) CreatePublic(data io.Reader, filetype string) (string, error)
GetPublic(path string) (io.ReadCloser, error) GetPublic(path string) (io.ReadCloser, error)
GetPublicDir() http.Dir
} }
var _ SvgStorage = FileSvgStorage{} var _ SvgStorage = FileSvgStorage{}
var TempDir string = "" var TempDir string = ""
type FileSvgStorage struct { type FileSvgStorage struct {
basepath string basepath, publicSubPath string
} }
func NewFileStorage(path string) *FileSvgStorage { func NewFileStorage(path, publicSubPath string) *FileSvgStorage {
err := os.MkdirAll(path, 0755) err := os.MkdirAll(path, 0755)
if err != nil { if err != nil {
panic(err) panic(err)
} }
log.Println("Initialized file storage handler") log.Println("Initialized file storage handler")
return &FileSvgStorage{path} return &FileSvgStorage{path, publicSubPath}
} }
func (f FileSvgStorage) Create(id string, svg io.Reader) (string, error) { 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) { 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 { if err := os.Mkdir(path, 0755); err != nil {
return "", err 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) { 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 { if err != nil {
return nil, err return nil, err
} }
return file, nil 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) { func CreateTemp(data io.Reader, filetype string) (string, error) {
file, err := os.CreateTemp(TempDir, "*."+filetype) file, err := os.CreateTemp(TempDir, "*."+filetype)
if err != nil { if err != nil {