-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
141 lines (116 loc) · 3.52 KB
/
upload.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
133
134
135
136
137
138
139
140
141
package deploy
import (
"context"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/cloudwego/hertz/pkg/app"
"github.com/pkg/errors"
cdn "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
terrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
"github.com/stulzq/hexo-deploy-agent/config"
"github.com/stulzq/hexo-deploy-agent/logger"
"github.com/stulzq/hexo-deploy-agent/util"
"github.com/stulzq/hexo-deploy-agent/web/handlers/deploy/model"
)
var (
deployConf *model.DeployConfig
cdnClient *cdn.Client
)
const (
uploadPath = "deploy/upload/"
unzipTmp = "deploy/tmp/"
)
func init() {
// load config
var conf model.DeployConfig
config.GetStruct("deploy", &conf)
deployConf = &conf
if util.IsDebug() {
deployConf.CDN.AccessKey = os.Getenv("AK")
deployConf.CDN.SecretKey = os.Getenv("SK")
deployConf.Dingtalk.Url = os.Getenv("DINGTALK_URL")
}
// create dir
if err := os.MkdirAll(uploadPath, os.ModePerm); err != nil {
panic(errors.Wrap(err, "mkdir err"))
}
// tencentyun cdn client
credential := common.NewCredential(
deployConf.CDN.AccessKey,
deployConf.CDN.SecretKey,
)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "cdn.tencentcloudapi.com"
cdnClient, _ = cdn.NewClient(credential, "", cpf)
}
// Upload upload deploy file(zip)
func Upload(_ context.Context, c *app.RequestContext) {
// get file
file, err := c.FormFile("f")
if err != nil {
logger.Error("[Web][Deploy][Upload] receive file err,", err)
c.String(500, err.Error())
return
}
// check ext
ext := filepath.Ext(file.Filename)
if strings.ToLower(ext) != ".zip" {
c.String(400, "file ext now allow")
return
}
// save file
fileSavePath := filepath.Join(uploadPath, file.Filename)
logger.Info("[Web][Deploy][Upload] receive file: ", file.Filename)
err = c.SaveUploadedFile(file, fileSavePath)
if err != nil {
logger.Error("[Web][Deploy][Upload] save file err,", err)
c.String(500, err.Error())
return
}
c.String(200, "ok")
// deploy
go processDeploy(fileSavePath)
}
func processDeploy(fileSavePath string) {
logger.Info("[Deploy][Job] start deploy ", fileSavePath)
// unzip
unzipTargetDir := deployConf.BlogDir
if util.IsDebug() {
unzipTargetDir = unzipTmp
}
if err := util.UnZip(fileSavePath, unzipTargetDir); err != nil {
logger.Error("[Deploy][Job] unzip err,", err)
return
}
logger.Info("[Deploy][Job] unzip success to path: ", unzipTargetDir)
// cdn
if deployConf.CDN.Enable {
request := cdn.NewPurgePathCacheRequest()
request.Paths = deployConf.CDN.RefreshPaths
request.FlushType = deployConf.CDN.FlushType
response, err := cdnClient.PurgePathCache(request)
if _, ok := err.(*terrors.TencentCloudSDKError); ok {
logger.Error("[Deploy][Job] an API error has returned: ", err)
return
}
if err != nil {
logger.Error("[Deploy][Job] cdn request err: ", err)
return
}
logger.Info("[Deploy][Job] cdn response: ", response.ToJsonString())
}
logger.Info("[Deploy][Job] successfully")
if deployConf.Dingtalk.Enable {
const msg = `{"msgtype": "text","text": {"content":"Deploy successfully!"}}`
// send msg https://open.dingtalk.com/document/group/custom-robot-access
if _, err := http.Post(deployConf.Dingtalk.Url, "application/json", strings.NewReader(msg)); err != nil {
logger.Error("[Deploy][Job] send msg failed, ", err)
}
}
os.Remove(fileSavePath)
os.RemoveAll(unzipTmp)
}