forked from Angey40/BaiduPCS-Go
/
prepare.go
702 lines (602 loc) · 21.2 KB
/
prepare.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package baidupcs
import (
"bytes"
"fmt"
"github.com/iikira/BaiduPCS-Go/baidupcs/pcserror"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/requester/multipartreader"
"github.com/json-iterator/go"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"unsafe"
)
func handleRespClose(resp *http.Response) error {
if resp != nil {
return resp.Body.Close()
}
return nil
}
func handleRespStatusError(opreation string, resp *http.Response) pcserror.Error {
errInfo := pcserror.NewPCSErrorInfo(opreation)
// http 响应错误处理
switch resp.StatusCode {
case 413: // Request Entity Too Large
// 上传的文件太大了
resp.Body.Close()
errInfo.SetNetError(fmt.Errorf("http 响应错误, %s", resp.Status))
return errInfo
}
return nil
}
// PrepareUK 获取用户 UK, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareUK() (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL := GetHTTPScheme(pcs.isHTTPS) + "://pan.baidu.com/api/user/getinfo?need_selfinfo=1"
errInfo := pcserror.NewPanErrorInfo(OperationGetUK)
resp, err := pcs.client.Req("GET", pcsURL, nil, map[string]string{
"User-Agent": NetdiskUA,
})
if err != nil {
handleRespClose(resp)
errInfo.SetNetError(err)
return nil, errInfo
}
return resp.Body, nil
}
// PrepareQuotaInfo 获取当前用户空间配额信息, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareQuotaInfo() (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL := pcs.generatePCSURL("quota", "info")
baiduPCSVerbose.Infof("%s URL: %s\n", OperationQuotaInfo, pcsURL)
resp, err := pcs.client.Req("GET", pcsURL.String(), nil, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationQuotaInfo,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareFilesDirectoriesBatchMeta 获取多个文件/目录的元信息, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareFilesDirectoriesBatchMeta(paths ...string) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
sendData, err := (&PathsListJSON{}).JSON(paths...)
if err != nil {
panic(OperationFilesDirectoriesMeta + ", json 数据构造失败, " + err.Error())
}
pcsURL := pcs.generatePCSURL("file", "meta")
baiduPCSVerbose.Infof("%s URL: %s\n", OperationFilesDirectoriesMeta, pcsURL)
// 表单上传
mr := multipartreader.NewMultipartReader()
mr.AddFormFeild("param", bytes.NewReader(sendData))
mr.CloseMultipart()
resp, err := pcs.client.Req("POST", pcsURL.String(), mr, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationFilesDirectoriesMeta,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareFilesDirectoriesList 获取目录下的文件和目录列表, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareFilesDirectoriesList(path string, options *OrderOptions) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
if path == "" {
path = "/"
}
pcsURL := pcs.generatePCSURL("file", "list", map[string]string{
"path": path,
"by": *(*string)(unsafe.Pointer(&options.By)),
"order": *(*string)(unsafe.Pointer(&options.Order)),
"limit": "0-2147483647",
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationFilesDirectoriesList, pcsURL)
resp, err := pcs.client.Req("GET", pcsURL.String(), nil, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationFilesDirectoriesList,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareSearch 按文件名搜索文件, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareSearch(targetPath, keyword string, recursive bool) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
var re string
if recursive {
re = "1"
} else {
re = "0"
}
pcsURL := pcs.generatePCSURL("file", "search", map[string]string{
"path": targetPath,
"wd": keyword,
"re": re,
})
resp, err := pcs.client.Req("GET", pcsURL.String(), nil, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationSearch,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareRemove 批量删除文件/目录, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareRemove(paths ...string) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
sendData, err := (&PathsListJSON{}).JSON(paths...)
if err != nil {
panic(OperationMove + ", json 数据构造失败, " + err.Error())
}
pcsURL := pcs.generatePCSURL("file", "delete")
baiduPCSVerbose.Infof("%s URL: %s\n", OperationRemove, pcsURL)
// 表单上传
mr := multipartreader.NewMultipartReader()
mr.AddFormFeild("param", bytes.NewReader(sendData))
mr.CloseMultipart()
resp, err := pcs.client.Req("POST", pcsURL.String(), mr, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationRemove,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareMkdir 创建目录, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareMkdir(pcspath string) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL := pcs.generatePCSURL("file", "mkdir", map[string]string{
"path": pcspath,
})
baiduPCSVerbose.Infof("%s URL: %s", OperationMkdir, pcsURL)
resp, err := pcs.client.Req("POST", pcsURL.String(), nil, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationMkdir,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
func (pcs *BaiduPCS) prepareCpMvOp(op string, cpmvJSON ...*CpMvJSON) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
var method string
switch op {
case OperationCopy:
method = "copy"
case OperationMove, OperationRename:
method = "move"
default:
panic("Unknown opreation: " + op)
}
errInfo := pcserror.NewPCSErrorInfo(op)
sendData, err := (&CpMvListJSON{
List: cpmvJSON,
}).JSON()
if err != nil {
//json 数据生成失败
panic(err)
}
pcsURL := pcs.generatePCSURL("file", method)
baiduPCSVerbose.Infof("%s URL: %s\n", op, pcsURL)
// 表单上传
mr := multipartreader.NewMultipartReader()
mr.AddFormFeild("param", bytes.NewReader(sendData))
mr.CloseMultipart()
resp, err := pcs.client.Req("POST", pcsURL.String(), mr, nil)
if err != nil {
handleRespClose(resp)
errInfo.SetNetError(err)
return nil, errInfo
}
return resp.Body, nil
}
// PrepareRename 重命名文件/目录, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareRename(from, to string) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
return pcs.prepareCpMvOp(OperationRename, &CpMvJSON{
From: from,
To: to,
})
}
// PrepareCopy 批量拷贝文件/目录, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareCopy(cpmvJSON ...*CpMvJSON) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
return pcs.prepareCpMvOp(OperationCopy, cpmvJSON...)
}
// PrepareMove 批量移动文件/目录, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareMove(cpmvJSON ...*CpMvJSON) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
return pcs.prepareCpMvOp(OperationMove, cpmvJSON...)
}
// PrepareRapidUpload 秒传文件, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareRapidUpload(targetPath, contentMD5, sliceMD5, crc32 string, length int64) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsError = pcs.checkIsdir(OperationRapidUpload, targetPath)
if pcsError != nil {
return nil, pcsError
}
pcsURL := pcs.generatePCSURL("file", "rapidupload", map[string]string{
"path": targetPath, // 上传文件的全路径名
"content-length": strconv.FormatInt(length, 10), // 待秒传的文件长度
"content-md5": contentMD5, // 待秒传的文件的MD5
"slice-md5": sliceMD5, // 待秒传的文件前256kb的MD5
"content-crc32": crc32, // 待秒传文件CRC32
"ondup": "overwrite", // overwrite: 表示覆盖同名文件; newcopy: 表示生成文件副本并进行重命名,命名规则为“文件名_日期.后缀”
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationRapidUpload, pcsURL)
resp, err := pcs.client.Req("POST", pcsURL.String(), nil, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationRapidUpload,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareLocateDownload 提取下载链接, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareLocateDownload(pcspath string) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL := pcs.generatePCSURL("file", "locatedownload", map[string]string{
"path": pcspath,
"ver": "2",
"vip": "1",
"network_type": "wifi",
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationLocateDownload, pcsURL)
resp, err := pcs.client.Req("POST", pcsURL.String(), nil, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationLocateDownload,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareUpload 上传单个文件, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareUpload(targetPath string, uploadFunc UploadFunc) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsError = pcs.checkIsdir(OperationUpload, targetPath)
if pcsError != nil {
return nil, pcsError
}
pcsURL := pcs.generatePCSURL("file", "upload", map[string]string{
"path": targetPath,
"ondup": "overwrite",
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationUpload, pcsURL)
resp, err := uploadFunc(pcsURL.String(), pcs.client.Jar)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationUpload,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
pcsError = handleRespStatusError(OperationUpload, resp)
if pcsError != nil {
return
}
return resp.Body, nil
}
// PrepareUploadTmpFile 分片上传—文件分片及上传, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareUploadTmpFile(uploadFunc UploadFunc) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL := pcs.generatePCSURL("file", "upload", map[string]string{
"type": "tmpfile",
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationUploadTmpFile, pcsURL)
resp, err := uploadFunc(pcsURL.String(), pcs.client.Jar)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationUploadTmpFile,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
pcsError = handleRespStatusError(OperationUploadTmpFile, resp)
if pcsError != nil {
return
}
return resp.Body, nil
}
// PrepareUploadCreateSuperFile 分片上传—合并分片文件, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareUploadCreateSuperFile(targetPath string, blockList ...string) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsError = pcs.checkIsdir(OperationUploadCreateSuperFile, targetPath)
if pcsError != nil {
return nil, pcsError
}
bl := BlockListJSON{
BlockList: blockList,
}
sendData, err := jsoniter.Marshal(&bl)
if err != nil {
panic(err)
}
pcsURL := pcs.generatePCSURL("file", "createsuperfile", map[string]string{
"path": targetPath,
"ondup": "overwrite",
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationUploadCreateSuperFile, pcsURL)
// 表单上传
mr := multipartreader.NewMultipartReader()
mr.AddFormFeild("param", bytes.NewReader(sendData))
mr.CloseMultipart()
resp, err := pcs.client.Req("POST", pcsURL.String(), mr, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationUploadCreateSuperFile,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareUploadPrecreate 分片上传—Precreate, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareUploadPrecreate(targetPath, contentMD5, sliceMD5, crc32 string, size int64, bolckList ...string) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL := &url.URL{
Scheme: GetHTTPScheme(pcs.isHTTPS),
Host: PanBaiduCom,
Path: "api/precreate",
}
baiduPCSVerbose.Infof("%s URL: %s\n", OperationUploadPrecreate, pcsURL)
resp, err := pcs.client.Req("POST", pcsURL.String(), map[string]string{
"path": targetPath,
"size": strconv.FormatInt(size, 10),
"isdir": "0",
"block_list": mergeStringList(bolckList...),
"autoinit": "1",
"content-md5": contentMD5,
"slice-md5": sliceMD5,
"contentCrc32": crc32,
"rtype": "2",
}, map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": NetdiskUA,
})
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationUploadPrecreate,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
pcsError = handleRespStatusError(OperationUploadPrecreate, resp)
if pcsError != nil {
return
}
return resp.Body, nil
}
// PrepareUploadSuperfile2 另一个上传接口
func (pcs *BaiduPCS) PrepareUploadSuperfile2(uploadid, targetPath string, partseq int, partOffset int64, uploadFunc UploadFunc) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL := pcs.generatePCSURL("superfile2", "upload", map[string]string{
"type": "tmpfile",
"path": targetPath,
"partseq": strconv.Itoa(partseq),
"partoffset": strconv.FormatInt(partOffset, 10),
"uploadid": uploadid,
"vip": "1",
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationUploadSuperfile2, pcsURL)
resp, err := uploadFunc(pcsURL.String(), pcs.client.Jar)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationUploadSuperfile2,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
pcsError = handleRespStatusError(OperationUpload, resp)
if pcsError != nil {
return
}
return resp.Body, nil
}
// PrepareCloudDlAddTask 添加离线下载任务, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareCloudDlAddTask(sourceURL, savePath string) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL2 := pcs.generatePCSURL2("services/cloud_dl", "add_task", map[string]string{
"save_path": savePath,
"source_url": sourceURL,
"timeout": "2147483647",
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationCloudDlAddTask, pcsURL2)
resp, err := pcs.client.Req("POST", pcsURL2.String(), nil, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationCloudDlAddTask,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareCloudDlQueryTask 精确查询离线下载任务, 只返回服务器响应数据和错误信息,
// taskids 例子: 12123,234234,2344, 用逗号隔开多个 task_id
func (pcs *BaiduPCS) PrepareCloudDlQueryTask(taskIDs string) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL2 := pcs.generatePCSURL2("services/cloud_dl", "query_task", map[string]string{
"op_type": "1",
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationCloudDlQueryTask, pcsURL2)
// 表单上传
mr := multipartreader.NewMultipartReader()
mr.AddFormFeild("task_ids", strings.NewReader(taskIDs))
mr.CloseMultipart()
resp, err := pcs.client.Req("POST", pcsURL2.String(), mr, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationCloudDlQueryTask,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareCloudDlListTask 查询离线下载任务列表, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareCloudDlListTask() (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL2 := pcs.generatePCSURL2("services/cloud_dl", "list_task", map[string]string{
"need_task_info": "1",
"status": "255",
"start": "0",
"limit": "1000",
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationCloudDlListTask, pcsURL2)
resp, err := pcs.client.Req("POST", pcsURL2.String(), nil, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationCloudDlListTask,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
func (pcs *BaiduPCS) prepareCloudDlCDTask(opreation, method string, taskID int64) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL2 := pcs.generatePCSURL2("services/cloud_dl", method, map[string]string{
"task_id": strconv.FormatInt(taskID, 10),
})
baiduPCSVerbose.Infof("%s URL: %s\n", opreation, pcsURL2)
resp, err := pcs.client.Req("POST", pcsURL2.String(), nil, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: opreation,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareCloudDlCancelTask 取消离线下载任务, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareCloudDlCancelTask(taskID int64) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
return pcs.prepareCloudDlCDTask(OperationCloudDlCancelTask, "cancel_task", taskID)
}
// PrepareCloudDlDeleteTask 取消离线下载任务, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareCloudDlDeleteTask(taskID int64) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
return pcs.prepareCloudDlCDTask(OperationCloudDlDeleteTask, "delete_task", taskID)
}
// PrepareCloudDlClearTask 清空离线下载任务记录, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareCloudDlClearTask() (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL2 := pcs.generatePCSURL2("services/cloud_dl", "clear_task")
baiduPCSVerbose.Infof("%s URL: %s\n", OperationCloudDlClearTask, pcsURL2)
resp, err := pcs.client.Req("POST", pcsURL2.String(), nil, nil)
if err != nil {
handleRespClose(resp)
return nil, &pcserror.PCSErrInfo{
Operation: OperationCloudDlClearTask,
ErrType: pcserror.ErrTypeNetError,
Err: err,
}
}
return resp.Body, nil
}
// PrepareSharePSet 私密分享文件, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareSharePSet(paths []string, period int) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL := &url.URL{
Scheme: GetHTTPScheme(pcs.isHTTPS),
Host: PanBaiduCom,
Path: "share/pset",
}
errInfo := pcserror.NewPanErrorInfo(OperationShareSet)
baiduPCSVerbose.Infof("%s URL: %s\n", OperationShareSet, pcsURL)
resp, err := pcs.client.Req("POST", pcsURL.String(), map[string]string{
"path_list": mergeStringList(paths...),
"schannel": "0",
"channel_list": "[]",
"period": strconv.Itoa(period),
}, map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": NetdiskUA,
})
if err != nil {
handleRespClose(resp)
errInfo.SetNetError(err)
return nil, errInfo
}
return resp.Body, nil
}
// PrepareShareCancel 取消分享, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareShareCancel(shareIDs []int64) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL := &url.URL{
Scheme: GetHTTPScheme(pcs.isHTTPS),
Host: PanBaiduCom,
Path: "share/cancel",
}
errInfo := pcserror.NewPanErrorInfo(OperationShareCancel)
baiduPCSVerbose.Infof("%s URL: %s\n", OperationShareCancel, pcsURL)
ss := converter.SliceInt64ToString(shareIDs)
resp, err := pcs.client.Req("POST", pcsURL.String(), map[string]string{
"shareid_list": "[" + strings.Join(ss, ",") + "]",
}, map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": NetdiskUA,
})
if err != nil {
handleRespClose(resp)
errInfo.SetNetError(err)
return nil, errInfo
}
return resp.Body, nil
}
// PrepareShareList 列出分享列表, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareShareList(page int) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
query := url.Values{}
query.Set("page", strconv.Itoa(page))
query.Set("desc", "1")
query.Set("order", "time")
pcsURL := &url.URL{
Scheme: GetHTTPScheme(pcs.isHTTPS),
Host: PanBaiduCom,
Path: "share/record",
RawQuery: query.Encode(),
}
errInfo := pcserror.NewPanErrorInfo(OperationShareList)
baiduPCSVerbose.Infof("%s URL: %s\n", OperationShareList, pcsURL)
resp, err := pcs.client.Req("GET", pcsURL.String(), nil, map[string]string{
"User-Agent": NetdiskUA,
})
if err != nil {
handleRespClose(resp)
errInfo.SetNetError(err)
return nil, errInfo
}
return resp.Body, nil
}