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 {
flag.PrintDefaults()
} else {
svg.Storage = svg.NewFileStorage(datapath)
svg.Storage = svg.NewFileStorage(datapath, "public")
server.PrepareHTTP()
server.Start()
}

View File

@@ -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)
}

View File

@@ -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 {