forked from andreimarcu/linx-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
47 lines (39 loc) · 994 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
45
46
47
package main
import (
"fmt"
"net/http"
"strings"
"github.com/andreimarcu/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")
filename := c.URLParams["name"]
s := strings.Split(filename, "/")
if len(s) != 2 {
notFoundHandler(c, w, r)
return
}
filename = s[1] + "." + s[0]
// 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
}
}