2015-09-29 05:46:43 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-04 08:25:00 +02:00
|
|
|
"fmt"
|
2015-09-29 05:46:43 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
func deleteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
2015-10-06 06:31:09 +02:00
|
|
|
requestKey := r.Header.Get("Linx-Delete-Key")
|
2015-09-29 05:46:43 +02:00
|
|
|
|
|
|
|
filename := c.URLParams["name"]
|
|
|
|
|
|
|
|
// Ensure requested file actually exists
|
2016-06-07 08:37:42 +02:00
|
|
|
if _, readErr := fileBackend.Exists(filename); os.IsNotExist(readErr) {
|
2015-09-29 05:46:43 +02:00
|
|
|
notFoundHandler(c, w, r) // 404 - file doesn't exist
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure delete key is correct
|
2015-10-08 04:45:34 +02:00
|
|
|
metadata, err := metadataRead(filename)
|
2015-09-29 05:46:43 +02:00
|
|
|
if err != nil {
|
|
|
|
unauthorizedHandler(c, w, r) // 401 - no metadata available
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-08 04:45:34 +02:00
|
|
|
if metadata.DeleteKey == requestKey {
|
2016-06-07 08:37:42 +02:00
|
|
|
fileDelErr := fileBackend.Delete(filename)
|
2017-05-02 06:25:56 +02:00
|
|
|
metaDelErr := metaStorageBackend.Delete(filename)
|
2015-09-29 05:46:43 +02:00
|
|
|
|
|
|
|
if (fileDelErr != nil) || (metaDelErr != 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
|
|
|
|
}
|
|
|
|
|
2015-10-04 08:25:00 +02:00
|
|
|
fmt.Fprintf(w, "DELETED")
|
2015-09-29 05:46:43 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
} else {
|
|
|
|
unauthorizedHandler(c, w, r) // 401 - wrong delete key
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|