/
params.go
108 lines (95 loc) · 2.21 KB
/
params.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package upload
import (
"encoding/json"
"io"
"strings"
"github.com/SYNQfm/helpers/common"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
const (
DefaultCtype = "video/mp4"
DefaultAcl = "private"
)
type AwsUploadF interface {
Upload(io.Reader) (*s3manager.UploadOutput, error)
}
type UploadParameters struct {
Action string `json:"action"`
AwsAccessKeyId string `json:"AWSAccessKeyId"`
ContentType string `json:"Content-Type"`
Policy string `json:"policy"`
Signature string `json:"signature"`
Acl string `json:"acl"`
Region string `json:"region"`
Key string `json:"key"`
SuccessStatus string `json:"success_action_status"`
SignatureUrl string `json:"signature_url"`
VideoId string `json:"video_id"`
AssetId string `json:"asset_id"`
}
type UploadRequest struct {
AssetId string `json:"asset_id"`
ContentType string `json:"content_type"`
Ext string `json:"ext"`
Type string `json:"type"`
Acl string `json:"acl"`
}
func NewUploadRequest(data []byte) (UploadRequest, error) {
var req UploadRequest
err := json.Unmarshal(data, &req)
if err != nil {
return req, err
}
err = req.ProcessCtype()
if err != nil {
return req, err
}
return req, nil
}
func parseCtype(ctype string) (string, error) {
newType := ctype
if len(strings.Split(newType, "/")) != 2 {
return "", common.NewError("invalid ctype '%s'", newType)
}
switch ctype {
case "image/jpg": // causes error in policy, change to jpeg
newType = "image/jpeg"
case "video/msvideo":
newType = "video/avi"
}
return newType, nil
}
func (u *UploadRequest) ProcessCtype() error {
ctype := u.ContentType
if ctype != "" {
c, err := parseCtype(ctype)
if err != nil {
return err
}
u.ContentType = c
} else {
u.ContentType = DefaultCtype
}
return nil
}
func (u UploadRequest) GetAcl() string {
if u.Acl != "" {
return u.Acl
}
return DefaultAcl
}
func (u UploadRequest) GetCType() string {
return u.ContentType
}
func (u UploadRequest) GetType() string {
if u.Type != "" {
return u.Type
}
return u.ContentType
}
func (u UploadRequest) GetExt() string {
if u.Ext != "" {
return u.Ext
}
return common.GetFileExtension(u.GetCType())
}