Compare commits
7 Commits
1031ad7e8c
...
205b70eeab
| Author | SHA1 | Date | |
|---|---|---|---|
|
205b70eeab
|
|||
|
de3ba44093
|
|||
|
343abe9bb5
|
|||
|
ba90d5a0ed
|
|||
|
6cf349bafa
|
|||
|
823f5f091a
|
|||
|
541caffd65
|
4
go.mod
4
go.mod
@@ -2,10 +2,12 @@ module tomatentum.net/svg-templater
|
|||||||
|
|
||||||
go 1.24.9
|
go 1.24.9
|
||||||
|
|
||||||
|
require github.com/glebarez/go-sqlite v1.22.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
github.com/glebarez/go-sqlite v1.22.0 // indirect
|
|
||||||
github.com/google/uuid v1.5.0 // indirect
|
github.com/google/uuid v1.5.0 // indirect
|
||||||
|
github.com/hymkor/exregexp-go v0.2.0 // indirect
|
||||||
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/sys v0.15.0 // indirect
|
golang.org/x/sys v0.15.0 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -4,6 +4,8 @@ github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec
|
|||||||
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
|
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
|
||||||
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
||||||
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/hymkor/exregexp-go v0.2.0 h1:tyIB8S9gpUwiBi3aDXXXL2yIXqTMWZzW3hs+UNDqcRM=
|
||||||
|
github.com/hymkor/exregexp-go v0.2.0/go.mod h1:bm661vkJcg9TbcYNr7QOcOgGlr+Jxy1Qb84YjVON3bg=
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
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=
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ const SVGTABLECREATE string = `
|
|||||||
);`
|
);`
|
||||||
|
|
||||||
const INSERTSVGSQL string = "INSERT INTO svg VALUES (?, ?);"
|
const INSERTSVGSQL string = "INSERT INTO svg VALUES (?, ?);"
|
||||||
|
const GETSPECIFICSVGSQL string = "SELECT * FROM svg WHERE name = ?;"
|
||||||
|
const GETSVGSQL string = "SELECT * FROM svg;"
|
||||||
|
|
||||||
func InsertSVG(data *svg.TemplateData) error {
|
func InsertSVG(data *svg.TemplateData) error {
|
||||||
json, err := json.Marshal(data.TemplateKeys)
|
json, err := json.Marshal(data.TemplateKeys)
|
||||||
@@ -26,3 +28,47 @@ func InsertSVG(data *svg.TemplateData) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetSVG() ([]svg.TemplateData, error) {
|
||||||
|
result, err := database.Query(GETSVGSQL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer result.Close()
|
||||||
|
templates := make([]svg.TemplateData, 0)
|
||||||
|
for result.Next() {
|
||||||
|
var (
|
||||||
|
id string
|
||||||
|
keysjson []byte
|
||||||
|
keys []string
|
||||||
|
)
|
||||||
|
if err := result.Scan(&id, &keysjson); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(keysjson, &keys); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
templates = append(templates, svg.TemplateData{Id: id, TemplateKeys: keys})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := result.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return templates, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSpecificSVG(id string) (svg.TemplateData, error) {
|
||||||
|
result := database.QueryRow(GETSPECIFICSVGSQL, id)
|
||||||
|
|
||||||
|
var (
|
||||||
|
keysjson []byte
|
||||||
|
keys []string
|
||||||
|
)
|
||||||
|
if err := result.Scan(&id, &keysjson); err != nil {
|
||||||
|
return svg.TemplateData{}, err
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(keysjson, &keys); err != nil {
|
||||||
|
return svg.TemplateData{}, err
|
||||||
|
}
|
||||||
|
return svg.TemplateData{Id: id, TemplateKeys: keys}, nil
|
||||||
|
}
|
||||||
|
|||||||
105
internal/routes/download.go
Normal file
105
internal/routes/download.go
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"tomatentum.net/svg-templater/pkg/format"
|
||||||
|
"tomatentum.net/svg-templater/pkg/svg/actions"
|
||||||
|
)
|
||||||
|
|
||||||
|
type downloadRequest struct {
|
||||||
|
TemplateKeys map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
type downloadResponse struct {
|
||||||
|
Url string
|
||||||
|
}
|
||||||
|
|
||||||
|
func DownloadSVG(w http.ResponseWriter, r *http.Request) {
|
||||||
|
contentType := r.Header.Get("Content-Type")
|
||||||
|
if contentType != "application/json" {
|
||||||
|
http.Error(w, "Incorrect Content-Type. Needs application/json.", http.StatusUnsupportedMediaType)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var request downloadRequest
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
id := r.PathValue("id")
|
||||||
|
if id == "" {
|
||||||
|
http.Error(w, "No ID provided", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
widthString := r.URL.Query().Get("w")
|
||||||
|
width := 0
|
||||||
|
if widthString != "" {
|
||||||
|
width, err = strconv.Atoi(widthString)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Invalid w parameter", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
heightString := r.URL.Query().Get("h")
|
||||||
|
height := 0
|
||||||
|
if heightString != "" {
|
||||||
|
height, err = strconv.Atoi(heightString)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Invalid h parameter", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
convParam := format.ConversionParameters{
|
||||||
|
Format: r.URL.Query().Get("format"),
|
||||||
|
Width: width,
|
||||||
|
Height: height,
|
||||||
|
}
|
||||||
|
if convParam.Format == "" {
|
||||||
|
convParam.Format = "svg"
|
||||||
|
}
|
||||||
|
|
||||||
|
templateParam := actions.TemplateParameters{
|
||||||
|
Id: id,
|
||||||
|
Keys: request.TemplateKeys,
|
||||||
|
}
|
||||||
|
|
||||||
|
filename, err := actions.ProvideFile(&templateParam, &convParam)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
urlparsed := getPublicUrl(r, filename)
|
||||||
|
log.Printf("Made %s available as %s\n", id, urlparsed)
|
||||||
|
response := downloadResponse{
|
||||||
|
Url: urlparsed,
|
||||||
|
}
|
||||||
|
w.Header().Add("Content-Type", "application/json")
|
||||||
|
json, err := json.Marshal(response)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPublicUrl(r *http.Request, path string) string {
|
||||||
|
newURL := url.URL{
|
||||||
|
Host: r.Host,
|
||||||
|
Path: filepath.Join("public", path),
|
||||||
|
}
|
||||||
|
newURL.Scheme = "http"
|
||||||
|
if r.TLS != nil {
|
||||||
|
newURL.Scheme = "https"
|
||||||
|
}
|
||||||
|
return newURL.String()
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"tomatentum.net/svg-templater/pkg/svg/actions"
|
"tomatentum.net/svg-templater/pkg/svg/actions"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateSVG(writer http.ResponseWriter, r http.Request) {
|
func CreateSVG(writer http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
contentType := r.Header.Get("Content-Type")
|
contentType := r.Header.Get("Content-Type")
|
||||||
if contentType != "image/svg+xml" {
|
if contentType != "image/svg+xml" {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"tomatentum.net/svg-templater/internal/routes"
|
"tomatentum.net/svg-templater/internal/routes"
|
||||||
"tomatentum.net/svg-templater/pkg/auth"
|
"tomatentum.net/svg-templater/pkg/auth"
|
||||||
@@ -20,10 +19,17 @@ func PrepareHTTP() {
|
|||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
routes.CreateSVG(w, *r)
|
routes.CreateSVG(w, r)
|
||||||
|
})
|
||||||
|
registerAuthorizedFunc("/svg/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
routes.DownloadSVG(w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
registerAuthorized("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir())))
|
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir())))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
@@ -41,7 +47,3 @@ func registerAuthorized(path string, handler http.Handler) {
|
|||||||
func registerAuthorizedFunc(path string, f func(w http.ResponseWriter, r *http.Request)) {
|
func registerAuthorizedFunc(path string, f func(w http.ResponseWriter, r *http.Request)) {
|
||||||
registerAuthorized(path, http.HandlerFunc(f))
|
registerAuthorized(path, http.HandlerFunc(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPublicPath(path string) string {
|
|
||||||
return filepath.Join("public", path)
|
|
||||||
}
|
|
||||||
|
|||||||
22
pkg/svg/actions/download.go
Normal file
22
pkg/svg/actions/download.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"tomatentum.net/svg-templater/pkg/format"
|
||||||
|
"tomatentum.net/svg-templater/pkg/svg"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ProvideFile(r *TemplateParameters, conversion *format.ConversionParameters) (string, error) {
|
||||||
|
templatedSvgblob, err := Template(r)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
log.Printf("Converting %s to format %s (w=%d,h=%d)", r.Id, conversion.Format, conversion.Width, conversion.Height)
|
||||||
|
result, err := format.ConvertByte(templatedSvgblob, *conversion)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return svg.Storage.CreatePublic(bytes.NewReader(result), conversion.Format)
|
||||||
|
}
|
||||||
66
pkg/svg/actions/template.go
Normal file
66
pkg/svg/actions/template.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"maps"
|
||||||
|
"regexp"
|
||||||
|
"slices"
|
||||||
|
|
||||||
|
"github.com/hymkor/exregexp-go"
|
||||||
|
"tomatentum.net/svg-templater/internal/database"
|
||||||
|
"tomatentum.net/svg-templater/pkg/svg"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TemplateParameters struct {
|
||||||
|
Id string
|
||||||
|
Keys map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func Template(r *TemplateParameters) ([]byte, error) {
|
||||||
|
mapkeys := slices.Collect(maps.Keys(r.Keys))
|
||||||
|
ok, err := verifyTemplate(r.Id, mapkeys)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("Template does not exist.")
|
||||||
|
}
|
||||||
|
log.Printf("Replacing keys of %s template\n", r.Id)
|
||||||
|
|
||||||
|
reader, err := svg.Storage.Get(r.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer reader.Close()
|
||||||
|
svgblob, err := io.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
replaceAll(&svgblob, r.Keys)
|
||||||
|
log.Printf("Finished replacing keys of %s template\n", r.Id)
|
||||||
|
return svgblob, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func replaceAll(svgblob *[]byte, keys map[string]string) {
|
||||||
|
regex := regexp.MustCompile(svg.KeyRegex)
|
||||||
|
*svgblob = []byte(exregexp.ReplaceAllStringSubmatchFunc(regex, string(*svgblob), func(s []string) string {
|
||||||
|
log.Printf("Replacing key %s with %s\n", s[1], keys[s[1]])
|
||||||
|
return keys[s[1]]
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func verifyTemplate(id string, keys []string) (bool, error) {
|
||||||
|
data, err := database.GetSpecificSVG(id)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, key := range data.TemplateKeys {
|
||||||
|
if !slices.Contains(keys, key) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
@@ -27,9 +27,9 @@ func Create(svgbuf []byte) (svg.TemplateData, error) {
|
|||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func populateKeys(data *svg.TemplateData, svg []byte) {
|
func populateKeys(data *svg.TemplateData, svgblob []byte) {
|
||||||
regex := regexp.MustCompile(`\{\{\s*(.*?)\s*\}\}`)
|
regex := regexp.MustCompile(svg.KeyRegex)
|
||||||
result := regex.FindAllSubmatch(svg, -1)
|
result := regex.FindAllSubmatch(svgblob, -1)
|
||||||
templateKeys := make([]string, len(result))
|
templateKeys := make([]string, len(result))
|
||||||
|
|
||||||
for i, matches := range result {
|
for i, matches := range result {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SvgStorage interface {
|
type SvgStorage interface {
|
||||||
@@ -59,7 +60,7 @@ func (f FileSvgStorage) Get(id string) (io.ReadCloser, error) {
|
|||||||
|
|
||||||
func (f FileSvgStorage) CreatePublic(data io.Reader, filetype string) (string, error) {
|
func (f FileSvgStorage) CreatePublic(data io.Reader, filetype string) (string, error) {
|
||||||
path := filepath.Join(f.basepath, f.publicSubPath)
|
path := filepath.Join(f.basepath, f.publicSubPath)
|
||||||
if err := os.Mkdir(path, 0755); err != nil {
|
if err := os.MkdirAll(path, 0755); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,8 +69,12 @@ func (f FileSvgStorage) CreatePublic(data io.Reader, filetype string) (string, e
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
_, err = io.Copy(file, data)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
return file.Name(), nil
|
return strings.TrimPrefix(file.Name(), path), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FileSvgStorage) GetPublic(path string) (io.ReadCloser, error) {
|
func (f FileSvgStorage) GetPublic(path string) (io.ReadCloser, error) {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package svg
|
package svg
|
||||||
|
|
||||||
|
const KeyRegex string = `\{\{\s*(.*?)\s*\}\}`
|
||||||
|
|
||||||
type TemplateData struct {
|
type TemplateData struct {
|
||||||
Id string
|
Id string
|
||||||
TemplateKeys []string
|
TemplateKeys []string
|
||||||
|
|||||||
Reference in New Issue
Block a user