Files
svg-templater/pkg/svg/storage.go
T
2026-05-27 22:02:11 +02:00

194 lines
4.4 KiB
Go

package svg
import (
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"golang.org/x/image/font/sfnt"
)
type SvgStorage interface {
Create(name string, svg io.Reader) (string, error)
Get(name string) (io.ReadCloser, error)
Delete(name string) error
List() ([]os.DirEntry, error)
AddFont(reader io.Reader, format string) error
GetFonts() ([]string, error)
GetFontsDir() string
CreatePublic(data io.Reader, suffix string, filetype string) (string, error)
GetPublic(path string) (io.ReadCloser, error)
GetPublicDir() http.Dir
}
var _ SvgStorage = FileSvgStorage{}
var TempDir string = ""
type FileSvgStorage struct {
basepath, publicSubPath, fontssubpath string
}
func NewFileStorage(path, publicSubPath, fontssubpath string) *FileSvgStorage {
err := os.MkdirAll(path, 0755)
if err != nil {
panic(err)
}
err = os.MkdirAll(filepath.Join(path, fontssubpath), 0755)
if err != nil {
panic(err)
}
log.Println("Initialized file storage handler")
return &FileSvgStorage{path, publicSubPath, fontssubpath}
}
func (f FileSvgStorage) Create(name string, svg io.Reader) (string, error) {
file, err := os.Create(filepath.Join(f.basepath, name+".svg"))
if err != nil {
return "", err
}
defer file.Close()
_, err = io.Copy(file, svg)
if err != nil {
return "", err
}
log.Println("Created File: ", file.Name())
return file.Name(), nil
}
func (f FileSvgStorage) Get(name string) (io.ReadCloser, error) {
file, err := os.Open(filepath.Join(f.basepath, name+".svg"))
if err != nil {
return nil, err
}
return file, nil
}
func (f FileSvgStorage) Delete(name string) error {
var path string
if strings.HasSuffix(name, ".svg") {
path = filepath.Join(f.basepath, name)
} else {
path = filepath.Join(f.basepath, name+".svg")
}
defer log.Println("Deleted File: " + path)
return os.Remove(path)
}
func (f FileSvgStorage) List() ([]os.DirEntry, error) {
return os.ReadDir(f.basepath)
}
func (f FileSvgStorage) CreatePublic(data io.Reader, suffix string, filetype string) (string, error) {
path := filepath.Join(f.basepath, f.publicSubPath)
if err := os.MkdirAll(path, 0755); err != nil {
return "", err
}
file, err := os.CreateTemp(path, fmt.Sprintf("*-%s."+filetype, suffix))
if err != nil {
return "", err
}
defer file.Close()
_, err = io.Copy(file, data)
if err != nil {
return "", err
}
return filepath.Base(file.Name()), nil
}
func (f FileSvgStorage) GetPublic(path string) (io.ReadCloser, error) {
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 {
return "", err
}
defer file.Close()
_, err = io.Copy(file, data)
if err != nil {
return "", err
}
return file.Name(), nil
}
func (f FileSvgStorage) AddFont(fontreader io.Reader, format string) error {
if format != "ttf" && format != "otf" {
return errors.New("Format not supported")
}
file, err := os.CreateTemp(filepath.Join(f.basepath, f.fontssubpath), "*."+format)
if err != nil {
return err
}
defer file.Close()
log.Printf("Writing %s font to %s\n", format, file.Name())
_, err = io.Copy(file, fontreader)
if err != nil {
return err
}
return nil
}
func (f FileSvgStorage) GetFonts() ([]string, error) {
path := filepath.Join(f.basepath, f.fontssubpath)
entries, err := os.ReadDir(path)
if err != nil {
return nil, err
}
fonts := make([]string, len(entries))
for i, entry := range entries {
if entry.IsDir() {
continue
}
if strings.HasSuffix(entry.Name(), ".ttf") || strings.HasSuffix(entry.Name(), ".otf") {
fontblob, err := os.ReadFile(filepath.Join(path, entry.Name()))
if err != nil {
return nil, err
}
fontname, err := getFontName(fontblob)
if err != nil {
return nil, err
}
fonts[i] = fontname
}
}
log.Printf("Got fonts \n%s\n", strings.Join(fonts, "\n"))
return fonts, nil
}
func (f FileSvgStorage) GetFontsDir() string {
return filepath.Join(f.basepath, f.fontssubpath)
}
func getFontName(svgblob []byte) (string, error) {
font, err := sfnt.Parse(svgblob)
if err != nil {
return "", err
}
name, err := font.Name(nil, sfnt.NameIDFull)
if err != nil {
return "", err
}
return name, nil
}