forked from ZizzyDizzyMC/linx-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
44 lines (35 loc) · 973 Bytes
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
"net/http"
"github.com/superryanguo/linx-server/backends"
"github.com/zenazn/goji/web"
)
func deleteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
requestKey := r.Header.Get("Linx-Delete-Key")
if len(r.URL.Query().Get("linx-delete-key")) > 0 {
requestKey = r.URL.Query().Get("linx-delete-key")
}
filename := c.URLParams["name"]
// Ensure that file exists and delete key is correct
metadata, err := storageBackend.Head(filename)
if err == backends.NotFoundErr {
notFoundHandler(c, w, r) // 404 - file doesn't exist
return
} else if err != nil {
unauthorizedHandler(c, w, r) // 401 - no metadata available
return
}
if metadata.DeleteKey == requestKey {
err := storageBackend.Delete(filename)
if err != nil {
oopsHandler(c, w, r, RespPLAIN, "Could not delete")
return
}
fmt.Fprintf(w, "DELETED")
return
} else {
unauthorizedHandler(c, w, r) // 401 - wrong delete key
return
}
}