feat(page): add Page action endpoints
build / Go-Build (push) Successful in 1m26s
build / Go-Build (pull_request) Successful in 1m27s

This commit is contained in:
2026-05-27 23:07:35 +02:00
parent 664ae1f7d3
commit 70e14ec63c
2 changed files with 96 additions and 0 deletions
+80
View File
@@ -0,0 +1,80 @@
package routes
import (
"encoding/json"
"io"
"net/http"
"strconv"
"tomatentum.net/svg-templater/pkg/svg"
"tomatentum.net/svg-templater/pkg/svg/actions"
)
func AddPage(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(128000000); err != nil {
http.Error(w, "Couldn't parse form data.", http.StatusBadRequest)
return
}
fileheaders := r.MultipartForm.File["files"]
id := r.PathValue("id")
pages := make([]svg.TemplatePage, 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
}
page, err := actions.AddPage(id, buf)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
pages[i] = page
}
err := json.NewEncoder(w).Encode(pages)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func DeletePage(w http.ResponseWriter, r *http.Request) {
id, pagestring := r.PathValue("id"), r.PathValue("page")
page, err := strconv.Atoi(pagestring)
if err != nil {
http.Error(w, "Page must be a number", http.StatusBadRequest)
return
}
ok, err := actions.DeletePage(id, page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if !ok {
http.Error(w, "Could not delete page", http.StatusInternalServerError)
return
}
}
+16
View File
@@ -43,6 +43,22 @@ func PrepareHTTP() {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
})
registerAuthorizedFunc("/svg/{id}/page/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
routes.AddPage(w, r)
})
registerAuthorizedFunc("/svg/{id}/page/{page}", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "DELETE" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
routes.DeletePage(w, r)
})
registerAuthorizedFunc("/font/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":