Add support for file deletion with DELETE request

This commit is contained in:
CronyAkatsuki 2023-12-21 13:11:24 +01:00
parent 8c1ab0b53b
commit 8b1337bdc1
2 changed files with 18 additions and 1 deletions

17
main.go
View File

@ -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")
}

View File

@ -32,7 +32,7 @@
<p>
To delete a file using curl:
<code>curl "{{.Host}}/delete/file_name"</code>
<code>curl -X DELETE "{{.Host}}/files/:file"</code>
</p>
<p>