ProfouzorsLinx/delete.go

45 lines
972 B
Go
Raw Permalink Normal View History

2015-09-29 05:46:43 +02:00
package main
import (
"fmt"
2015-09-29 05:46:43 +02:00
"net/http"
2019-01-25 08:33:11 +01:00
"github.com/andreimarcu/linx-server/backends"
2015-09-29 05:46:43 +02:00
"github.com/zenazn/goji/web"
)
func deleteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
requestKey := r.Header.Get("Linx-Delete-Key")
2021-02-21 06:27:16 +01:00
if len(r.URL.Query().Get("linx-delete-key")) > 0 {
2021-02-21 06:29:53 +01:00
requestKey = r.URL.Query().Get("linx-delete-key")
2021-02-21 06:15:30 +01:00
}
2015-09-29 05:46:43 +02:00
filename := c.URLParams["name"]
2021-02-21 06:15:30 +01:00
2019-01-25 08:33:11 +01:00
// Ensure that file exists and delete key is correct
metadata, err := storageBackend.Head(filename)
if err == backends.NotFoundErr {
2015-09-29 05:46:43 +02:00
notFoundHandler(c, w, r) // 404 - file doesn't exist
return
2019-01-25 08:33:11 +01:00
} else if err != nil {
2015-09-29 05:46:43 +02:00
unauthorizedHandler(c, w, r) // 401 - no metadata available
return
}
if metadata.DeleteKey == requestKey {
2019-01-25 08:33:11 +01:00
err := storageBackend.Delete(filename)
if err != nil {
2015-10-04 18:47:20 +02:00
oopsHandler(c, w, r, RespPLAIN, "Could not delete")
2015-09-29 05:46:43 +02:00
return
}
fmt.Fprintf(w, "DELETED")
2015-09-29 05:46:43 +02:00
return
} else {
unauthorizedHandler(c, w, r) // 401 - wrong delete key
return
}
}