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:
37
internal/command/commandline.go
Normal file
37
internal/command/commandline.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"tomatentum.net/svg-templater/internal/server"
|
||||
"tomatentum.net/svg-templater/pkg/svg"
|
||||
)
|
||||
|
||||
var (
|
||||
help bool
|
||||
generateTokenFlag bool
|
||||
deleteTokenFlag bool
|
||||
datapath string
|
||||
)
|
||||
|
||||
func PrepareCommandLine() {
|
||||
flag.BoolVar(&help, "help", false, "Displays the help String")
|
||||
flag.BoolVar(&generateTokenFlag, "tokengen", false, "Generate token with name")
|
||||
flag.BoolVar(&deleteTokenFlag, "tokendel", false, "Delete token with name")
|
||||
flag.StringVar(&datapath, "data", "/var/lib/svg-templater", "Override data directory")
|
||||
}
|
||||
|
||||
func HandleCommandline() {
|
||||
flag.Parse()
|
||||
if generateTokenFlag {
|
||||
GenerateTokenCommand()
|
||||
} else if deleteTokenFlag {
|
||||
DeleteTokenCommand()
|
||||
} else if help {
|
||||
flag.PrintDefaults()
|
||||
} else {
|
||||
svg.Storage = svg.NewFileStorage(datapath)
|
||||
server.PrepareHTTP()
|
||||
server.Start()
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package svgtemplater
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
28
internal/database/svgdb.go
Normal file
28
internal/database/svgdb.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"tomatentum.net/svg-templater/pkg/svg"
|
||||
)
|
||||
|
||||
const SVGTABLECREATE string = `
|
||||
CREATE TABLE IF NOT EXISTS svg (
|
||||
name varchar(16) PRIMARY KEY NOT NULL,
|
||||
templatekeys longtext NOT NULL
|
||||
);`
|
||||
|
||||
const INSERTSVGSQL string = "INSERT INTO svg VALUES (?, ?);"
|
||||
|
||||
func InsertSVG(data *svg.TemplateData) error {
|
||||
json, err := json.Marshal(data.TemplateKeys)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := database.Exec(INSERTSVGSQL, data.Id, string(json)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
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
|
||||
}
|
||||
32
internal/server/http.go
Normal file
32
internal/server/http.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"tomatentum.net/svg-templater/internal/routes"
|
||||
"tomatentum.net/svg-templater/pkg/auth"
|
||||
)
|
||||
|
||||
func PrepareHTTP() {
|
||||
registerAuthorized("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "You are authorized!")
|
||||
})
|
||||
registerAuthorized("/svg/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
routes.CreateSVG(w, *r)
|
||||
})
|
||||
}
|
||||
|
||||
func Start() {
|
||||
log.Println("Starting http server on :3000")
|
||||
http.ListenAndServe(":3000", nil)
|
||||
}
|
||||
|
||||
func registerAuthorized(path string, f func(w http.ResponseWriter, r *http.Request)) {
|
||||
http.HandleFunc(path, auth.AuthMiddleware(http.HandlerFunc(f)))
|
||||
log.Println("Registered authorized handler for", path)
|
||||
}
|
||||
Reference in New Issue
Block a user