forked from josjevv/moire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset.go
93 lines (72 loc) · 2.05 KB
/
asset.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package handlers
import (
"net/http"
"time"
"github.com/bulletind/moire/db"
"gopkg.in/simversity/gottp.v3"
)
const TemporaryRedirectCode = 302
type Asset struct {
gottp.BaseHandler
}
func (self *Asset) Get(request *gottp.Request) {
_id, ok := request.GetArgument("_id").(string)
if !ok {
request.Raise(gottp.HttpError{
http.StatusNotFound,
"Not found",
})
return
}
valid := ValidateSignature(request)
if valid == false {
return
}
conn := getConn()
asset := getAsset(conn, _id)
request.Writer.Header().Set("Access-Control-Allow-Origin", "*")
_, no_redirect := request.GetArgument("no_redirect").(string)
if asset.Status != db.READY && asset.FileType == ImageFile {
pollUntilReady(conn, _id)
asset = getAsset(conn, _id)
}
// This needs explanation and refactoring
url, expiryDate, err := getURL(asset)
if err != nil {
if asset.FileType == ImageFile && no_redirect != true {
// If its an Image and no_redirect is not provided, error is ir-relevant.
// It will always return the placeholder image anyway.
// we don't want the expires caching header to be set, as we expect the asset to be uploaded soon.
//
// Why we don't show a 404 for images just like with other assets or show a thumbnail for these other assets as well,
// remains a mistery.
request.Redirect(url, TemporaryRedirectCode)
return
}
// show a 404 error for other assets
request.Raise(gottp.HttpError{
http.StatusNotFound,
err.Error(),
})
return
}
// aws signing key expiration date and will be used to set relevant caching periods accordingly
request.Writer.Header().Set("Expires", expiryDate.Format(time.RFC1123))
request.Redirect(url, TemporaryRedirectCode)
return
}
func (self *Asset) Post(request *gottp.Request) {
_id, ok := request.GetArgument("_id").(string)
if !ok {
request.Raise(gottp.HttpError{
http.StatusNotFound,
"Not found",
})
return
}
conn := getConn()
asset := getAsset(conn, _id)
signedUrl := getSignedUploadURL(asset.Bucket, asset.Path, asset.MimeType)
request.Write(signedUrl)
return
}