Compare commits
4 Commits
56abe1dee4
...
0118a53d3e
| Author | SHA1 | Date | |
|---|---|---|---|
|
0118a53d3e
|
|||
|
6b61fe1e54
|
|||
| 35e873397d | |||
|
7a9cf7ab93
|
2
go.mod
2
go.mod
@@ -10,7 +10,9 @@ require (
|
|||||||
github.com/hymkor/exregexp-go v0.2.0
|
github.com/hymkor/exregexp-go v0.2.0
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
|
golang.org/x/image v0.36.0
|
||||||
golang.org/x/sys v0.15.0 // indirect
|
golang.org/x/sys v0.15.0 // indirect
|
||||||
|
golang.org/x/text v0.34.0 // indirect
|
||||||
modernc.org/libc v1.37.6 // indirect
|
modernc.org/libc v1.37.6 // indirect
|
||||||
modernc.org/mathutil v1.6.0 // indirect
|
modernc.org/mathutil v1.6.0 // indirect
|
||||||
modernc.org/memory v1.7.2 // indirect
|
modernc.org/memory v1.7.2 // indirect
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -10,9 +10,13 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
|
|||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||||
|
golang.org/x/image v0.36.0 h1:Iknbfm1afbgtwPTmHnS2gTM/6PPZfH+z2EFuOkSbqwc=
|
||||||
|
golang.org/x/image v0.36.0/go.mod h1:YsWD2TyyGKiIX1kZlu9QfKIsQ4nAAK9bdgdrIsE7xy4=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
||||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
|
||||||
|
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
|
||||||
modernc.org/libc v1.37.6 h1:orZH3c5wmhIQFTXF+Nt+eeauyd+ZIt2BX6ARe+kD+aw=
|
modernc.org/libc v1.37.6 h1:orZH3c5wmhIQFTXF+Nt+eeauyd+ZIt2BX6ARe+kD+aw=
|
||||||
modernc.org/libc v1.37.6/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
|
modernc.org/libc v1.37.6/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
|
||||||
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
|
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func HandleCommandline() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if !help {
|
if !help {
|
||||||
svg.Storage = svg.NewFileStorage(datapath, "public")
|
svg.Storage = svg.NewFileStorage(datapath, "public", "fonts")
|
||||||
if err := database.OpenSQLite(datapath); err != nil {
|
if err := database.OpenSQLite(datapath); err != nil {
|
||||||
log.Fatal("Failed opening DB:\n", err)
|
log.Fatal("Failed opening DB:\n", err)
|
||||||
return
|
return
|
||||||
@@ -43,6 +43,7 @@ func HandleCommandline() {
|
|||||||
} else if help {
|
} else if help {
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
} else {
|
} else {
|
||||||
|
svg.Storage = svg.NewFileStorage(datapath, "public", "fonts")
|
||||||
server.PrepareHTTP()
|
server.PrepareHTTP()
|
||||||
server.Start()
|
server.Start()
|
||||||
}
|
}
|
||||||
|
|||||||
45
internal/routes/font.go
Normal file
45
internal/routes/font.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"tomatentum.net/svg-templater/pkg/svg"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddFont(w http.ResponseWriter, r *http.Request) {
|
||||||
|
contentType := r.Header.Get("Content-Type")
|
||||||
|
if contentType != "font/ttf" && contentType != "font/otf" {
|
||||||
|
http.Error(w, "", http.StatusUnsupportedMediaType)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println("Received font add request")
|
||||||
|
format := strings.TrimPrefix(contentType, "font/")
|
||||||
|
|
||||||
|
if err := svg.Storage.AddFont(r.Body, format); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetFonts(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Println("Serving all available fonts")
|
||||||
|
fonts, err := svg.Storage.GetFonts()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
json, err := json.Marshal(fonts)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Add("Content-Type", "application/json")
|
||||||
|
w.Write(json)
|
||||||
|
}
|
||||||
@@ -28,6 +28,14 @@ func PrepareHTTP() {
|
|||||||
}
|
}
|
||||||
routes.DownloadSVG(w, r)
|
routes.DownloadSVG(w, r)
|
||||||
})
|
})
|
||||||
|
registerAuthorizedFunc("/font/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
|
routes.GetFonts(w, r)
|
||||||
|
case "POST":
|
||||||
|
routes.AddFont(w, r)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir())))
|
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir())))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,23 @@
|
|||||||
package svg
|
package svg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/image/font/sfnt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SvgStorage interface {
|
type SvgStorage interface {
|
||||||
Create(id string, svg io.Reader) (string, error)
|
Create(id string, svg io.Reader) (string, error)
|
||||||
Get(id string) (io.ReadCloser, error)
|
Get(id string) (io.ReadCloser, error)
|
||||||
|
AddFont(reader io.Reader, format string) error
|
||||||
|
GetFonts() ([]string, error)
|
||||||
|
GetFontsDir() (string, 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
|
GetPublicDir() http.Dir
|
||||||
@@ -21,16 +27,20 @@ var _ SvgStorage = FileSvgStorage{}
|
|||||||
var TempDir string = ""
|
var TempDir string = ""
|
||||||
|
|
||||||
type FileSvgStorage struct {
|
type FileSvgStorage struct {
|
||||||
basepath, publicSubPath string
|
basepath, publicSubPath, fontssubpath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFileStorage(path, publicSubPath string) *FileSvgStorage {
|
func NewFileStorage(path, publicSubPath, fontssubpath string) *FileSvgStorage {
|
||||||
err := os.MkdirAll(path, 0755)
|
err := os.MkdirAll(path, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
err = os.MkdirAll(filepath.Join(path, fontssubpath), 0755)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
log.Println("Initialized file storage handler")
|
log.Println("Initialized file storage handler")
|
||||||
return &FileSvgStorage{path, publicSubPath}
|
return &FileSvgStorage{path, publicSubPath, fontssubpath}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FileSvgStorage) Create(id string, svg io.Reader) (string, error) {
|
func (f FileSvgStorage) Create(id string, svg io.Reader) (string, error) {
|
||||||
@@ -102,3 +112,63 @@ func CreateTemp(data io.Reader, filetype string) (string, error) {
|
|||||||
|
|
||||||
return file.Name(), nil
|
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, error) {
|
||||||
|
return filepath.Join(f.basepath, f.fontssubpath), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user