Add support for file deletion with DELETE request
This commit is contained in:
parent
8c1ab0b53b
commit
8b1337bdc1
17
main.go
17
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")
|
||||
}
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user