feat(fonts): add font endpoints
All checks were successful
build / Go-Build (push) Successful in 27s
All checks were successful
build / Go-Build (push) Successful in 27s
This commit is contained in:
@@ -1,12 +1,45 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"tomatentum.net/svg-templater/pkg/svg"
|
||||||
|
)
|
||||||
|
|
||||||
func AddFont(w http.ResponseWriter, r *http.Request) {
|
func AddFont(w http.ResponseWriter, r *http.Request) {
|
||||||
|
contentType := r.Header.Get("Content-Type")
|
||||||
if r.Header.Get("Content-Type") != "font/ttf" {
|
if contentType != "font/ttf" && contentType != "font/otf" {
|
||||||
http.Error(w, "", http.StatusUnsupportedMediaType)
|
http.Error(w, "", http.StatusUnsupportedMediaType)
|
||||||
return
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,14 @@ func PrepareHTTP() {
|
|||||||
}
|
}
|
||||||
routes.CreateSVG(w, *r)
|
routes.CreateSVG(w, *r)
|
||||||
})
|
})
|
||||||
|
registerAuthorized("/font/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
|
routes.GetFonts(w, r)
|
||||||
|
case "POST":
|
||||||
|
routes.AddFont(w, r)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
|
|||||||
@@ -63,10 +63,6 @@ func (f FileSvgStorage) Get(id string) (io.Reader, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f FileSvgStorage) AddFont(fontreader io.Reader, format string) error {
|
func (f FileSvgStorage) AddFont(fontreader io.Reader, format string) error {
|
||||||
fontblob, err := io.ReadAll(fontreader)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if format != "ttf" && format != "otf" {
|
if format != "ttf" && format != "otf" {
|
||||||
return errors.New("Format not supported")
|
return errors.New("Format not supported")
|
||||||
}
|
}
|
||||||
@@ -75,7 +71,11 @@ func (f FileSvgStorage) AddFont(fontreader io.Reader, format string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
file.Write(fontblob)
|
log.Printf("Writing %s font to %s\n", format, file.Name())
|
||||||
|
_, err = io.Copy(file, fontreader)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,6 +102,7 @@ func (f FileSvgStorage) GetFonts() ([]string, error) {
|
|||||||
fonts[i] = fontname
|
fonts[i] = fontname
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Printf("Got fonts \n%s\n", strings.Join(fonts, "\n"))
|
||||||
return fonts, nil
|
return fonts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user