/
util.go
300 lines (288 loc) · 8.89 KB
/
util.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package vtencent
import (
"context"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"path"
"strconv"
"strings"
"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/http_range"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/go-resty/resty/v2"
)
func (d *Vtencent) request(url, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
req := base.RestyClient.R()
req.SetHeaders(map[string]string{
"cookie": d.Cookie,
"content-type": "application/json",
"origin": d.conf.origin,
"referer": d.conf.referer,
})
if callback != nil {
callback(req)
} else {
req.SetBody("{}")
}
if resp != nil {
req.SetResult(resp)
}
res, err := req.Execute(method, url)
if err != nil {
return nil, err
}
code := utils.Json.Get(res.Body(), "Code").ToString()
if code != "Success" {
switch code {
case "AuthFailure.SessionInvalid":
if err != nil {
return nil, errors.New(code)
}
default:
return nil, errors.New(code)
}
return d.request(url, method, callback, resp)
}
return res.Body(), nil
}
func (d *Vtencent) ugcRequest(url, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
req := base.RestyClient.R()
req.SetHeaders(map[string]string{
"cookie": d.Cookie,
"content-type": "application/json",
"origin": d.conf.origin,
"referer": d.conf.referer,
})
if callback != nil {
callback(req)
} else {
req.SetBody("{}")
}
if resp != nil {
req.SetResult(resp)
}
res, err := req.Execute(method, url)
if err != nil {
return nil, err
}
code := utils.Json.Get(res.Body(), "Code").ToInt()
if code != 0 {
message := utils.Json.Get(res.Body(), "message").ToString()
if len(message) == 0 {
message = utils.Json.Get(res.Body(), "msg").ToString()
}
return nil, errors.New(message)
}
return res.Body(), nil
}
func (d *Vtencent) LoadUser() (string, error) {
api := "https://api.vs.tencent.com/SaaS/Account/DescribeAccount"
res, err := d.request(api, http.MethodPost, func(req *resty.Request) {}, nil)
if err != nil {
return "", err
}
return utils.Json.Get(res, "Data", "TfUid").ToString(), nil
}
func (d *Vtencent) GetFiles(dirId string) ([]File, error) {
var res []File
//offset := 0
for {
api := "https://api.vs.tencent.com/PaaS/Material/SearchResource"
form := fmt.Sprintf(`{
"Text":"",
"Text":"",
"Offset":%d,
"Limit":50,
"Sort":{"Field":"%s","Order":"%s"},
"CreateTimeRanges":[],
"MaterialTypes":[],
"ReviewStatuses":[],
"Tags":[],
"SearchScopes":[{"Owner":{"Type":"PERSON","Id":"%s"},"ClassId":%s,"SearchOneDepth":true}]
}`, len(res), d.Addition.OrderBy, d.Addition.OrderDirection, d.TfUid, dirId)
var resp RspFiles
_, err := d.request(api, http.MethodPost, func(req *resty.Request) {
req.SetBody(form).ForceContentType("application/json")
}, &resp)
if err != nil {
return nil, err
}
res = append(res, resp.Data.ResourceInfoSet...)
if len(resp.Data.ResourceInfoSet) <= 0 || len(res) >= resp.Data.TotalCount {
break
}
}
return res, nil
}
func (d *Vtencent) CreateUploadMaterial(classId int, fileName string, UploadSummaryKey string) (RspCreatrMaterial, error) {
api := "https://api.vs.tencent.com/PaaS/Material/CreateUploadMaterial"
form := base.Json{"Owner": base.Json{"Type": "PERSON", "Id": d.TfUid},
"MaterialType": "VIDEO", "Name": fileName, "ClassId": classId,
"UploadSummaryKey": UploadSummaryKey}
var resps RspCreatrMaterial
_, err := d.request(api, http.MethodPost, func(req *resty.Request) {
req.SetBody(form).ForceContentType("application/json")
}, &resps)
if err != nil {
return RspCreatrMaterial{}, err
}
return resps, nil
}
func (d *Vtencent) ApplyUploadUGC(signature string, stream model.FileStreamer) (RspApplyUploadUGC, error) {
api := "https://vod2.qcloud.com/v3/index.php?Action=ApplyUploadUGC"
form := base.Json{
"signature": signature,
"videoName": stream.GetName(),
"videoType": strings.ReplaceAll(path.Ext(stream.GetName()), ".", ""),
"videoSize": stream.GetSize(),
}
var resps RspApplyUploadUGC
_, err := d.ugcRequest(api, http.MethodPost, func(req *resty.Request) {
req.SetBody(form).ForceContentType("application/json")
}, &resps)
if err != nil {
return RspApplyUploadUGC{}, err
}
return resps, nil
}
func (d *Vtencent) CommitUploadUGC(signature string, vodSessionKey string) (RspCommitUploadUGC, error) {
api := "https://vod2.qcloud.com/v3/index.php?Action=CommitUploadUGC"
form := base.Json{
"signature": signature,
"vodSessionKey": vodSessionKey,
}
var resps RspCommitUploadUGC
rsp, err := d.ugcRequest(api, http.MethodPost, func(req *resty.Request) {
req.SetBody(form).ForceContentType("application/json")
}, &resps)
if err != nil {
return RspCommitUploadUGC{}, err
}
if len(resps.Data.Video.URL) == 0 {
return RspCommitUploadUGC{}, errors.New(string(rsp))
}
return resps, nil
}
func (d *Vtencent) FinishUploadMaterial(SummaryKey string, VodVerifyKey string, UploadContext, VodFileId string) (RspFinishUpload, error) {
api := "https://api.vs.tencent.com/PaaS/Material/FinishUploadMaterial"
form := base.Json{
"UploadContext": UploadContext,
"VodVerifyKey": VodVerifyKey,
"VodFileId": VodFileId,
"UploadFullKey": SummaryKey}
var resps RspFinishUpload
rsp, err := d.request(api, http.MethodPost, func(req *resty.Request) {
req.SetBody(form).ForceContentType("application/json")
}, &resps)
if err != nil {
return RspFinishUpload{}, err
}
if len(resps.Data.MaterialID) == 0 {
return RspFinishUpload{}, errors.New(string(rsp))
}
return resps, nil
}
func (d *Vtencent) FinishHashUploadMaterial(SummaryKey string, UploadContext string) (RspFinishUpload, error) {
api := "https://api.vs.tencent.com/PaaS/Material/FinishUploadMaterial"
var resps RspFinishUpload
form := base.Json{
"UploadContext": UploadContext,
"UploadFullKey": SummaryKey}
rsp, err := d.request(api, http.MethodPost, func(req *resty.Request) {
req.SetBody(form).ForceContentType("application/json")
}, &resps)
if err != nil {
return RspFinishUpload{}, err
}
if len(resps.Data.MaterialID) == 0 {
return RspFinishUpload{}, errors.New(string(rsp))
}
return resps, nil
}
func (d *Vtencent) FileUpload(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
classId, err := strconv.Atoi(dstDir.GetID())
if err != nil {
return err
}
const chunkLength int64 = 1024 * 1024 * 10
reader, err := stream.RangeRead(http_range.Range{Start: 0, Length: chunkLength})
if err != nil {
return err
}
chunkHash, err := utils.HashReader(utils.SHA1, reader)
if err != nil {
return err
}
rspCreatrMaterial, err := d.CreateUploadMaterial(classId, stream.GetName(), chunkHash)
if err != nil {
return err
}
if rspCreatrMaterial.Data.QuickUpload {
SummaryKey := stream.GetHash().GetHash(utils.SHA1)
if len(SummaryKey) < utils.SHA1.Width {
if SummaryKey, err = utils.HashReader(utils.SHA1, stream); err != nil {
return err
}
}
UploadContext := rspCreatrMaterial.Data.UploadContext
_, err = d.FinishHashUploadMaterial(SummaryKey, UploadContext)
if err != nil {
return err
}
return nil
}
hash := sha1.New()
rspUGC, err := d.ApplyUploadUGC(rspCreatrMaterial.Data.VodUploadSign, stream)
if err != nil {
return err
}
params := rspUGC.Data
certificate := params.TempCertificate
cfg := &aws.Config{
HTTPClient: base.HttpClient,
// S3ForcePathStyle: aws.Bool(true),
Credentials: credentials.NewStaticCredentials(certificate.SecretID, certificate.SecretKey, certificate.Token),
Region: aws.String(params.StorageRegionV5),
Endpoint: aws.String(fmt.Sprintf("cos.%s.myqcloud.com", params.StorageRegionV5)),
}
ss, err := session.NewSession(cfg)
if err != nil {
return err
}
uploader := s3manager.NewUploader(ss)
if stream.GetSize() > s3manager.MaxUploadParts*s3manager.DefaultUploadPartSize {
uploader.PartSize = stream.GetSize() / (s3manager.MaxUploadParts - 1)
}
input := &s3manager.UploadInput{
Bucket: aws.String(fmt.Sprintf("%s-%d", params.StorageBucket, params.StorageAppID)),
Key: ¶ms.Video.StoragePath,
Body: io.TeeReader(stream, io.MultiWriter(hash, driver.NewProgress(stream.GetSize(), up))),
}
_, err = uploader.UploadWithContext(ctx, input)
if err != nil {
return err
}
rspCommitUGC, err := d.CommitUploadUGC(rspCreatrMaterial.Data.VodUploadSign, rspUGC.Data.VodSessionKey)
if err != nil {
return err
}
VodVerifyKey := rspCommitUGC.Data.Video.VerifyContent
VodFileId := rspCommitUGC.Data.FileID
UploadContext := rspCreatrMaterial.Data.UploadContext
SummaryKey := hex.EncodeToString(hash.Sum(nil))
_, err = d.FinishUploadMaterial(SummaryKey, VodVerifyKey, UploadContext, VodFileId)
if err != nil {
return err
}
return nil
}