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:
49
internal/routes/upload.go
Normal file
49
internal/routes/upload.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"tomatentum.net/svg-templater/pkg/svg/actions"
|
||||
)
|
||||
|
||||
func CreateSVG(writer 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)
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
data, err := actions.Create(readsvg)
|
||||
|
||||
if err != nil {
|
||||
http.Error(writer, 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)
|
||||
log.Println("Created SVG Template " + data.Id)
|
||||
}
|
||||
|
||||
func validateSVG(svgbuf []byte) (bool, error) {
|
||||
return bytes.Contains(svgbuf[:512], []byte("<svg")), nil
|
||||
}
|
||||
Reference in New Issue
Block a user