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"