diff --git a/internal/database/database.go b/internal/database/database.go index 03a4d12..f8144a8 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -32,5 +32,8 @@ func InitDB() { } func Close() error { + if database == nil { + return nil + } return database.Close() } diff --git a/internal/database/svgdb.go b/internal/database/svgdb.go index 97d03df..9993dfa 100644 --- a/internal/database/svgdb.go +++ b/internal/database/svgdb.go @@ -15,6 +15,7 @@ const SVGTABLECREATE string = ` const INSERTSVGSQL string = "INSERT INTO svg VALUES (?, ?);" const GETSPECIFICSVGSQL string = "SELECT * FROM svg WHERE name = ?;" const GETSVGSQL string = "SELECT * FROM svg;" +const DELETESVGSQL string = "DELETE FROM svg WHERE name = ?;" func InsertSVG(data *svg.TemplateData) error { json, err := json.Marshal(data.TemplateKeys) @@ -72,3 +73,19 @@ func GetSpecificSVG(id string) (svg.TemplateData, error) { } return svg.TemplateData{Id: id, TemplateKeys: keys}, nil } + +func DeleteSvg(id string) (bool, error) { + res, err := database.Exec(DELETESVGSQL, 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 +} diff --git a/internal/routes/delete.go b/internal/routes/delete.go new file mode 100644 index 0000000..677d9ec --- /dev/null +++ b/internal/routes/delete.go @@ -0,0 +1,26 @@ +package routes + +import ( + "net/http" + + "tomatentum.net/svg-templater/pkg/svg/actions" +) + +func DeleteSvg(writer http.ResponseWriter, reader *http.Request) { + + id := reader.PathValue("id") + if id == "" { + http.Error(writer, "No ID provided", http.StatusBadRequest) + return + } + + success, err := actions.Delete(id) + if err != nil { + http.Error(writer, err.Error(), http.StatusInternalServerError) + return + } + if !success { + http.Error(writer, "No template to delete.", http.StatusBadRequest) + return + } +} diff --git a/internal/routes/upload.go b/internal/routes/upload.go index 1c19dab..f7ff97c 100644 --- a/internal/routes/upload.go +++ b/internal/routes/upload.go @@ -41,7 +41,6 @@ func CreateSVG(writer http.ResponseWriter, r *http.Request) { 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) { diff --git a/internal/server/http.go b/internal/server/http.go index 0c8d58c..633cccd 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -22,11 +22,16 @@ func PrepareHTTP() { routes.CreateSVG(w, r) }) registerAuthorizedFunc("/svg/{id}", func(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + switch r.Method { + case "GET": + routes.DownloadSVG(w, r) + return + case "DELETE": + routes.DeleteSvg(w, r) return } - routes.DownloadSVG(w, r) + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + }) registerAuthorizedFunc("/font/", func(w http.ResponseWriter, r *http.Request) { switch r.Method { diff --git a/pkg/svg/actions/delete.go b/pkg/svg/actions/delete.go new file mode 100644 index 0000000..e63597e --- /dev/null +++ b/pkg/svg/actions/delete.go @@ -0,0 +1,20 @@ +package actions + +import ( + "log" + + "tomatentum.net/svg-templater/internal/database" + "tomatentum.net/svg-templater/pkg/svg" +) + +func Delete(id string) (bool, error) { + if success, err := database.DeleteSvg(id); err != nil || !success { + return success, err + } + + if err := svg.Storage.Delete(id); err != nil { + return false, err + } + log.Println("Deleted SVG Template " + id) + return true, nil +} diff --git a/pkg/svg/actions/upload.go b/pkg/svg/actions/upload.go index ea7577c..bc4bd47 100644 --- a/pkg/svg/actions/upload.go +++ b/pkg/svg/actions/upload.go @@ -24,6 +24,7 @@ func Create(svgbuf []byte) (svg.TemplateData, error) { return svg.TemplateData{}, err } + log.Println("Created SVG Template " + data.Id) return data, nil } diff --git a/pkg/svg/storage.go b/pkg/svg/storage.go index b5a7b9b..f9cb207 100644 --- a/pkg/svg/storage.go +++ b/pkg/svg/storage.go @@ -15,6 +15,7 @@ import ( type SvgStorage interface { Create(id string, svg io.Reader) (string, error) Get(id string) (io.ReadCloser, error) + Delete(id string) error AddFont(reader io.Reader, format string) error GetFonts() ([]string, error) GetFontsDir() (string, error) @@ -68,6 +69,12 @@ func (f FileSvgStorage) Get(id string) (io.ReadCloser, error) { return file, nil } +func (f FileSvgStorage) Delete(id string) error { + path := filepath.Join(f.basepath, id+".svg") + defer log.Println("Deleted File: " + path) + return os.Remove(path) +} + func (f FileSvgStorage) CreatePublic(data io.Reader, filetype string) (string, error) { path := filepath.Join(f.basepath, f.publicSubPath) if err := os.MkdirAll(path, 0755); err != nil {