forked from BonneVoyager/basicserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_get.go
46 lines (41 loc) · 1.18 KB
/
file_get.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
package basicserver
import (
"time"
"github.com/kataras/iris/v12"
)
// ServeFileGet serves
// Method: GET
// Resource: http://localhost/api/file/{id:string}
//
// This resource requires `Authorization` header, e.g.:
//
// Authorization: Bearer {token}
//
// If everything goes well then we will receive status code 200 and response with the file
// associated with provided id in query parameter.
//
// In case of error, this will return status code `400` or `500` and `text/plain` error
// message as a response.
//
// In case of invalid/expired token, this will return status code `401` and `text/plain`
// error message as a response.
//
func (app *BasicApp) ServeFileGet() iris.Handler {
return func(ctx iris.Context) {
fileID := ctx.Params().Get("id")
uid := ctx.Values().Get("uid").(string)
fileName := uid + ":" + fileID
file, err := app.Coll.Files.Open(fileName)
if err != nil {
if err.Error() == "not found" {
app.HandleError(err, ctx, iris.StatusUnauthorized)
ctx.WriteString("No Such File")
} else {
app.HandleError(err, ctx, iris.StatusInternalServerError)
}
return
}
defer file.Close()
ctx.ServeContent(file, fileName, time.Now(), true)
}
}