feat(upload): add first prototype of File storage, svg database and svg processing. Also added --help command
All checks were successful
build / Go-Build (push) Successful in 1m5s
All checks were successful
build / Go-Build (push) Successful in 1m5s
This commit is contained in:
50
pkg/svg/actions/upload.go
Normal file
50
pkg/svg/actions/upload.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"log"
|
||||
"regexp"
|
||||
|
||||
"tomatentum.net/svg-templater/internal/database"
|
||||
"tomatentum.net/svg-templater/pkg/svg"
|
||||
)
|
||||
|
||||
func Create(svgbuf []byte) (svg.TemplateData, error) {
|
||||
data := svg.TemplateData{Id: generateId(), TemplateKeys: nil}
|
||||
populateKeys(&data, svgbuf)
|
||||
|
||||
_, err := svg.Storage.Create(data.Id, bytes.NewReader(svgbuf))
|
||||
if err != nil {
|
||||
return svg.TemplateData{}, err
|
||||
}
|
||||
|
||||
if err := database.InsertSVG(&data); err != nil {
|
||||
return svg.TemplateData{}, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func populateKeys(data *svg.TemplateData, svg []byte) {
|
||||
regex := regexp.MustCompile(`\{\{\s*(.*?)\s*\}\}`)
|
||||
result := regex.FindAllSubmatch(svg, -1)
|
||||
templateKeys := make([]string, len(result))
|
||||
|
||||
for i, matches := range result {
|
||||
varname := matches[1] // first capture group
|
||||
templateKeys[i] = string(varname)
|
||||
}
|
||||
data.TemplateKeys = templateKeys
|
||||
log.Println("Found keys:\n", templateKeys)
|
||||
}
|
||||
|
||||
func generateId() string {
|
||||
token := make([]byte, 16)
|
||||
if _, err := rand.Read(token); err != nil {
|
||||
log.Fatal("Failed to generate Token:\n", err)
|
||||
return ""
|
||||
}
|
||||
return hex.EncodeToString(token)
|
||||
}
|
||||
50
pkg/svg/storage.go
Normal file
50
pkg/svg/storage.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package svg
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type SvgStorage interface {
|
||||
Create(id string, svg io.Reader) (string, error)
|
||||
Get(id string) (io.Reader, error)
|
||||
}
|
||||
|
||||
type FileSvgStorage struct {
|
||||
basepath string
|
||||
}
|
||||
|
||||
func NewFileStorage(path string) *FileSvgStorage {
|
||||
err := os.MkdirAll(path, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &FileSvgStorage{path}
|
||||
}
|
||||
|
||||
func (f FileSvgStorage) Create(id string, svg io.Reader) (string, error) {
|
||||
file, err := os.Create(filepath.Join(f.basepath, id+".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(id string) (io.Reader, error) {
|
||||
file, err := os.Open(filepath.Join(f.basepath, "svg", id+".svg"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
8
pkg/svg/templates.go
Normal file
8
pkg/svg/templates.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package svg
|
||||
|
||||
type TemplateData struct {
|
||||
Id string
|
||||
TemplateKeys []string
|
||||
}
|
||||
|
||||
var Storage SvgStorage
|
||||
Reference in New Issue
Block a user