From 8b1337bdc1178b940feb66e884bf194055f7b8f2 Mon Sep 17 00:00:00 2001 From: CronyAkatsuki Date: Thu, 21 Dec 2023 13:11:24 +0100 Subject: [PATCH] Add support for file deletion with DELETE request --- main.go | 17 +++++++++++++++++ public/views/index.html | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index eef5e0e..5362292 100644 --- a/main.go +++ b/main.go @@ -54,6 +54,8 @@ func main() { e.GET("/files/", Files) + e.DELETE("/files/:file", Delete) + e.Logger.Fatal(e.Start(":1323")) } @@ -198,3 +200,18 @@ func Upload(c echo.Context) error { return c.Redirect(http.StatusMovedPermanently, "/files/"+file.Filename) } + +func Delete(c echo.Context) error { + file := c.Param("file") + filePath := "files/" + file + + if _, err := os.Stat(filePath); err != nil { + return c.String(http.StatusOK, "That file doesn't exist on the server\n") + } + + err := os.Remove(filePath) + if err != nil { + return c.String(http.StatusOK, "Error while deleting "+file+"\n") + } + return c.String(http.StatusOK, "Deleted file from server\n") +} diff --git a/public/views/index.html b/public/views/index.html index 4e9972b..869823d 100644 --- a/public/views/index.html +++ b/public/views/index.html @@ -32,7 +32,7 @@

To delete a file using curl: - curl "{{.Host}}/delete/file_name" + curl -X DELETE "{{.Host}}/files/:file"