-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·132 lines (103 loc) · 2.81 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/helloworlde/cos/tool"
"github.com/tencentyun/scf-go-lib/cloudfunction"
events "github.com/tencentyun/scf-go-lib/events"
)
func main() {
cloudfunction.Start(operate)
}
func operate(request events.APIGatewayRequest) (string, error) {
requestContent := request.Body
if requestContent == "" {
fmt.Println("请求内容为空")
return generateResponse(false, "请求内容为空", nil), nil
}
req := Request{}
err := json.Unmarshal([]byte(requestContent), &req)
if err != nil {
fmt.Println("反序列化请求 Body 失败: ", err)
return generateResponse(false, "反序列化请求 Body 失败", nil), nil
}
fmt.Println("请求 Body 内容: ", req)
token := os.Getenv("COS_TOKEN")
if req.Token != token {
fmt.Println("Token 不正确: ", req.Token)
return generateResponse(false, "UN_AUTHENTICATION", nil), nil
}
if req.Action != "UPLOAD" && req.Action != "DOWNLOAD" {
fmt.Println("Action 不正确: ", req.Action)
return generateResponse(false, "Action must be UPLOAD or DOWNLOAD", nil), nil
}
if req.Domain == "" {
fmt.Println("Domain/Cookie 不正确, Domain: ", req.Domain, " Cookie: ", req.Cookie)
return generateResponse(false, "Domain 不正确", nil), nil
}
var resp = Response{}
fileName := getFileName(req.Domain)
if "DOWNLOAD" == req.Action {
resp, err = executeDownload(fileName)
} else {
resp, err = executeUpload(fileName, req.Cookie)
}
if err != nil {
return generateResponse(false, err.Error(), nil), nil
}
return generateResponse(true, "SUCCESS", &resp), nil
}
func executeUpload(name string, cookie string) (Response, error) {
err := tool.Upload(name, cookie)
if err != nil {
return Response{}, err
}
resp := Response{}
return resp, nil
}
func trimFilePrefix(name string) string {
prefix := os.Getenv("COS_PATH")
name = strings.TrimLeft(name, prefix+"/")
return name
}
func getFileName(domain string) string {
prefix := os.Getenv("COS_PATH")
name := fmt.Sprintf("%s/%s", prefix, domain)
return name
}
func executeDownload(name string) (Response, error) {
result, err := tool.Download(name)
if err != nil {
return Response{}, err
}
resp := Response{
Domain: trimFilePrefix(name),
Cookie: result,
}
return resp, nil
}
func generateResponse(success bool, message string, body *Response) string {
var content = ""
if body == nil {
body = &Response{}
}
body.Success = success
body.Message = message
bodyBytes, _ := json.Marshal(body)
content = string(bodyBytes)
return content
}
type Response struct {
Success bool `json:"success"`
Message string `json:"message"`
Domain string `json:"domain"`
Cookie string `json:"cookie"`
}
type Request struct {
Token string `json:"token"`
Action string `json:"action"`
Domain string `json:"domain"`
Cookie string `json:"cookie"`
}