This commit is contained in:
@@ -25,6 +25,8 @@ func OpenSQLite(basepath string) error {
|
||||
func InitDB() {
|
||||
_, err := database.Exec(TOKENTABLECREATE)
|
||||
_, err = database.Exec(SVGTABLECREATE)
|
||||
_, err = database.Exec(SVGPAGETABLECREATE)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("Failed to init database:\n", err)
|
||||
}
|
||||
|
||||
+20
-30
@@ -1,32 +1,23 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"tomatentum.net/svg-templater/pkg/svg"
|
||||
)
|
||||
|
||||
const SVGTABLECREATE string = `
|
||||
CREATE TABLE IF NOT EXISTS svg (
|
||||
id varchar(16) PRIMARY KEY NOT NULL,
|
||||
name varchar(32),
|
||||
templatekeys longtext NOT NULL
|
||||
name varchar(32)
|
||||
);`
|
||||
|
||||
const INSERTSVGSQL string = "INSERT INTO svg VALUES (?, ?, ?);"
|
||||
const INSERTSVGSQL string = "INSERT INTO svg VALUES (?, ?);"
|
||||
const GETSPECIFICSVGSQL string = "SELECT * FROM svg WHERE id = ?;"
|
||||
const GETSVGSQL string = "SELECT * FROM svg;"
|
||||
const DELETESVGSQL string = "DELETE FROM svg WHERE id = ?;"
|
||||
const RENAMESVGSQL string = "UPDATE svg SET name = ? WHERE id = ?;"
|
||||
|
||||
func InsertSVG(data *svg.TemplateData) error {
|
||||
json, err := json.Marshal(data.TemplateKeys)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := database.Exec(INSERTSVGSQL, data.Id, data.Name, string(json)); err != nil {
|
||||
if _, err := database.Exec(INSERTSVGSQL, data.Id, data.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -42,18 +33,19 @@ func GetSVG() ([]svg.TemplateData, error) {
|
||||
templates := make([]svg.TemplateData, 0)
|
||||
for result.Next() {
|
||||
var (
|
||||
id string
|
||||
name string
|
||||
keysjson []byte
|
||||
keys []string
|
||||
id string
|
||||
name string
|
||||
)
|
||||
if err := result.Scan(&id, &name, &keysjson); err != nil {
|
||||
if err := result.Scan(&id, &name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal(keysjson, &keys); err != nil {
|
||||
pages, err := GetSVGPages(id)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
templates = append(templates, svg.TemplateData{Id: id, Name: name, TemplateKeys: keys})
|
||||
|
||||
templates = append(templates, svg.TemplateData{Id: id, Name: name, Pages: pages})
|
||||
}
|
||||
|
||||
if err := result.Err(); err != nil {
|
||||
@@ -65,18 +57,17 @@ func GetSVG() ([]svg.TemplateData, error) {
|
||||
func GetSpecificSVG(id string) (svg.TemplateData, error) {
|
||||
result := database.QueryRow(GETSPECIFICSVGSQL, id)
|
||||
|
||||
var (
|
||||
name string
|
||||
keysjson []byte
|
||||
keys []string
|
||||
)
|
||||
if err := result.Scan(&id, &name, &keysjson); err != nil {
|
||||
var name string
|
||||
if err := result.Scan(&id, &name); err != nil {
|
||||
return svg.TemplateData{}, err
|
||||
}
|
||||
if err := json.Unmarshal(keysjson, &keys); err != nil {
|
||||
|
||||
pages, err := GetSVGPages(id)
|
||||
|
||||
if err != nil {
|
||||
return svg.TemplateData{}, err
|
||||
}
|
||||
return svg.TemplateData{Id: id, Name: name, TemplateKeys: keys}, nil
|
||||
return svg.TemplateData{Id: id, Name: name, Pages: pages}, nil
|
||||
}
|
||||
|
||||
func DeleteSvg(id string) (bool, error) {
|
||||
@@ -92,12 +83,11 @@ func DeleteSvg(id string) (bool, error) {
|
||||
if num == 0 {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
return DeleteAllSVGPages(id)
|
||||
}
|
||||
|
||||
func RenameSvg(id string, name string) error {
|
||||
res, err := database.Exec(RENAMESVGSQL, name, id)
|
||||
log.Println(id, name, res)
|
||||
_, err := database.Exec(RENAMESVGSQL, name, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"tomatentum.net/svg-templater/pkg/svg"
|
||||
)
|
||||
|
||||
func AddPage(page svg.TemplatePage) {
|
||||
|
||||
}
|
||||
|
||||
func DeletePage(templateID string, page int) {
|
||||
|
||||
}
|
||||
|
||||
const SVGPAGETABLECREATE string = `
|
||||
CREATE TABLE IF NOT EXISTS svgpage (
|
||||
id varchar(16) NOT NULL,
|
||||
page int NOT NULL,
|
||||
templatekeys longtext NOT NULL,
|
||||
PRIMARY KEY (id, page)
|
||||
);`
|
||||
|
||||
const INSERTSVGPAGESQL string = "INSERT INTO svgpage VALUES (?, ?, ?);"
|
||||
const GETSPECIFICSVGPAGESSQL string = "SELECT * FROM svgpage WHERE id = ?;"
|
||||
const DELETESVGPAGESQL string = "DELETE FROM svgpage WHERE id = ? AND page = ?;"
|
||||
const DELETEALLSVGPAGESQL string = "DELETE FROM svgpage WHERE id = ?;"
|
||||
const COUNTSVGPAGESQL string = "SELECT COUNT(*) FROM svgpage WHERE id = ?;"
|
||||
|
||||
func InsertSVGPage(data *svg.TemplatePage) (int, error) {
|
||||
json, err := json.Marshal(data.TemplateKeys)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
count, err := GetPageCount(data.TemplateId)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if _, err := database.Exec(INSERTSVGPAGESQL, data.TemplateId, count+1, string(json)); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count + 1, nil
|
||||
}
|
||||
|
||||
func GetSVGPages(id string) ([]svg.TemplatePage, error) {
|
||||
res, err := database.Query(GETSPECIFICSVGPAGESSQL, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Close()
|
||||
pages := make([]svg.TemplatePage, 0)
|
||||
for res.Next() {
|
||||
var (
|
||||
id string
|
||||
page int
|
||||
keysjson []byte
|
||||
keys []string
|
||||
)
|
||||
if err := res.Scan(&id, &page, &keysjson); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal(keysjson, &keys); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pages = append(pages, svg.TemplatePage{TemplateId: id, Page: page, TemplateKeys: keys})
|
||||
}
|
||||
return pages, nil
|
||||
}
|
||||
|
||||
func DeleteSVGPage(id string, page int) (bool, error) {
|
||||
res, err := database.Exec(DELETESVGPAGESQL, id, page)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
num, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if num == 0 {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func DeleteAllSVGPages(id string) (bool, error) {
|
||||
res, err := database.Exec(DELETEALLSVGPAGESQL, id)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
num, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if num == 0 {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func GetPageCount(id string) (int, error) {
|
||||
res := database.QueryRow(COUNTSVGPAGESQL, id)
|
||||
|
||||
var count int
|
||||
|
||||
if err := res.Scan(&count); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
+32
-18
@@ -10,39 +10,53 @@ import (
|
||||
"tomatentum.net/svg-templater/pkg/svg/actions"
|
||||
)
|
||||
|
||||
func CreateSVG(writer http.ResponseWriter, r *http.Request) {
|
||||
func CreateSVG(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
if contentType != "image/svg+xml" {
|
||||
http.Error(writer, "Incorrect Content-Type. Needs image/svg+xml.", http.StatusUnsupportedMediaType)
|
||||
if err := r.ParseMultipartForm(128000000); err != nil {
|
||||
http.Error(w, "Couldn't parse form data.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
readsvg, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("Error while reading Body\n", err)
|
||||
return
|
||||
}
|
||||
fileheaders := r.MultipartForm.File["files"]
|
||||
|
||||
if ok, err := validateSVG(readsvg); err != nil || !ok {
|
||||
http.Error(writer, err.Error(), http.StatusUnsupportedMediaType)
|
||||
log.Println("Wrong Media Type was uploaded\n", err)
|
||||
return
|
||||
files := make([][]byte, len(fileheaders))
|
||||
|
||||
for i, fileh := range fileheaders {
|
||||
file, err := fileh.Open()
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
buf, err := io.ReadAll(file)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if ok, err := validateSVG(buf); err != nil || !ok {
|
||||
http.Error(w, err.Error(), http.StatusUnsupportedMediaType)
|
||||
log.Println("Wrong Media Type was uploaded\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
files[i] = buf
|
||||
}
|
||||
|
||||
name := r.URL.Query().Get("name")
|
||||
|
||||
data, err := actions.Create(readsvg, name)
|
||||
data, err := actions.Create(files, name)
|
||||
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("Failed creating SVG template\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
writer.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(writer).Encode(data)
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
||||
func validateSVG(svgbuf []byte) (bool, error) {
|
||||
|
||||
Reference in New Issue
Block a user