This commit is contained in:
+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