feat(template): add Fileserver for download
Some checks failed
build / Go-Build (push) Failing after 12m37s
Some checks failed
build / Go-Build (push) Failing after 12m37s
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user