-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_delete.go
57 lines (51 loc) · 1.35 KB
/
file_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
48
49
50
51
52
53
54
55
56
57
package basicserver
import (
"errors"
"github.com/globalsign/mgo/bson"
"github.com/kataras/iris"
)
// ServeFileDelete serves
// Method: DELETE
// Resource: http://localhost/api/file
//
// This resource requires `Authorization` header, e.g.:
//
// Content-Type: application/json
// Authorization: Bearer {token}
//
// In order to delete a file, a DELETE request to /api/file resource need to be send.
//
// {
// "name": "uploaded_image.jpg"
// }
//
// If everything goes well, then this will return status code `200` and no response body.
//
// In case of error, this will return status code `400` or `500` and `text/plain` error
// message as response.
//
// In case of invalid/expired token, this will return status code `401` and `text/plain`
// error message as a response.
//
func (app *BasicApp) ServeFileDelete() iris.Handler {
return func(ctx iris.Context) {
var input bson.M
err := ctx.ReadJSON(&input)
if err != nil {
app.HandleError(err, ctx, iris.StatusBadRequest)
return
}
uid := ctx.Values().Get("uid").(string)
filename := input["name"].(string)
if filename == "" {
err = errors.New("Name Field Not Provided")
app.HandleError(err, ctx, iris.StatusBadRequest)
return
}
err = app.Coll.Files.Remove(uid + ":" + filename)
if err != nil {
app.HandleError(err, ctx, iris.StatusBadRequest)
return
}
}
}