This commit is contained in:
@@ -2,6 +2,7 @@ package actions
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"tomatentum.net/svg-templater/internal/database"
|
||||
"tomatentum.net/svg-templater/pkg/svg"
|
||||
@@ -12,9 +13,18 @@ func Delete(id string) (bool, error) {
|
||||
return success, err
|
||||
}
|
||||
|
||||
if err := svg.Storage.Delete(id); err != nil {
|
||||
entries, err := svg.Storage.List()
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
if strings.HasPrefix(entry.Name(), id) {
|
||||
svg.Storage.Delete(entry.Name())
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("Deleted SVG Template " + id)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"tomatentum.net/svg-templater/internal/database"
|
||||
"tomatentum.net/svg-templater/pkg/svg"
|
||||
)
|
||||
|
||||
const FILEFORMAT string = "%s-%d"
|
||||
|
||||
func AddPage(id string, svgbuf []byte) (svg.TemplatePage, error) {
|
||||
data := svg.TemplatePage{TemplateId: id}
|
||||
populateKeys(&data, svgbuf)
|
||||
|
||||
page, err := database.InsertSVGPage(&data)
|
||||
|
||||
data.Page = page
|
||||
|
||||
if err != nil {
|
||||
return svg.TemplatePage{}, err
|
||||
}
|
||||
|
||||
_, err = svg.Storage.Create(fmt.Sprintf(FILEFORMAT, id, page), bytes.NewReader(svgbuf))
|
||||
if err != nil {
|
||||
return svg.TemplatePage{}, err
|
||||
}
|
||||
|
||||
log.Println("Created SVG Template " + data.TemplateId + " Page " + strconv.Itoa(page))
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func DeletePage(id string, page int) (bool, error) {
|
||||
if success, err := database.DeleteSVGPage(id, page); err != nil || !success {
|
||||
return success, err
|
||||
}
|
||||
|
||||
if err := svg.Storage.Delete(fmt.Sprintf(FILEFORMAT, id, page)); err != nil {
|
||||
return false, err
|
||||
}
|
||||
log.Println("Deleted SVG Template " + id + " page " + strconv.Itoa(page))
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func populateKeys(data *svg.TemplatePage, svgblob []byte) {
|
||||
regex := regexp.MustCompile(svg.KeyRegex)
|
||||
result := regex.FindAllSubmatch(svgblob, -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)
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func verifyTemplate(id string, keys []string) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for _, key := range data.TemplateKeys {
|
||||
for _, key := range data.AllKeys() {
|
||||
if !slices.Contains(keys, key) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
+13
-21
@@ -1,25 +1,30 @@
|
||||
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, name string) (svg.TemplateData, error) {
|
||||
data := svg.TemplateData{Id: generateId(), Name: name, TemplateKeys: nil}
|
||||
populateKeys(&data, svgbuf)
|
||||
func Create(svgbufs [][]byte, name string) (svg.TemplateData, error) {
|
||||
id := generateId()
|
||||
|
||||
_, err := svg.Storage.Create(data.Id, bytes.NewReader(svgbuf))
|
||||
if err != nil {
|
||||
return svg.TemplateData{}, err
|
||||
pages := make([]svg.TemplatePage, len(svgbufs))
|
||||
for i, pagebuf := range svgbufs {
|
||||
page, err := AddPage(id, pagebuf)
|
||||
|
||||
if err != nil {
|
||||
return svg.TemplateData{}, err
|
||||
}
|
||||
|
||||
pages[i] = page
|
||||
}
|
||||
|
||||
data := svg.TemplateData{Id: id, Name: name, Pages: pages}
|
||||
|
||||
if err := database.InsertSVG(&data); err != nil {
|
||||
return svg.TemplateData{}, err
|
||||
}
|
||||
@@ -28,19 +33,6 @@ func Create(svgbuf []byte, name string) (svg.TemplateData, error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func populateKeys(data *svg.TemplateData, svgblob []byte) {
|
||||
regex := regexp.MustCompile(svg.KeyRegex)
|
||||
result := regex.FindAllSubmatch(svgblob, -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 {
|
||||
|
||||
Reference in New Issue
Block a user