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)
|
||||
}
|
||||
Reference in New Issue
Block a user