forked from qjfoidnh/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud_dl.go
337 lines (283 loc) · 8.6 KB
/
cloud_dl.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
package baidupcs
import (
"errors"
"github.com/qjfoidnh/BaiduPCS-Go/baidupcs/pcserror"
"github.com/qjfoidnh/BaiduPCS-Go/pcstable"
"github.com/qjfoidnh/BaiduPCS-Go/pcsutil/converter"
"github.com/qjfoidnh/BaiduPCS-Go/pcsutil/pcstime"
"io"
"path"
"strconv"
"strings"
)
type (
// CloudDlFileInfo 离线下载的文件信息
CloudDlFileInfo struct {
FileName string `json:"file_name"`
FileSize int64 `json:"file_size"`
}
// CloudDlTaskInfo 离线下载的任务信息
CloudDlTaskInfo struct {
TaskID int64
Status int // 0下载成功, 1下载进行中, 2系统错误, 3资源不存在, 4下载超时, 5资源存在但下载失败, 6存储空间不足, 7任务取消
StatusText string
FileSize int64 // 文件大小
FinishedSize int64 // 文件大小
CreateTime int64 // 创建时间
StartTime int64 // 开始时间
FinishTime int64 // 结束时间
SavePath string // 保存的路径
SourceURL string // 资源地址
TaskName string // 任务名称, 一般为文件名
OdType int
FileList []*CloudDlFileInfo
Result int // 0查询成功,结果有效,1要查询的task_id不存在
}
// CloudDlTaskList 离线下载的任务信息列表
CloudDlTaskList []*CloudDlTaskInfo
// cloudDlTaskInfo 用于解析远程返回的JSON
cloudDlTaskInfo struct {
Status string `json:"status"`
FileSize string `json:"file_size"`
FinishedSize string `json:"finished_size"`
CreateTime string `json:"create_time"`
StartTime string `json:"start_time"`
FinishTime string `json:"finish_time"`
SavePath string `json:"save_path"`
SourceURL string `json:"source_url"`
TaskName string `json:"task_name"`
OdType string `json:"od_type"`
FileList []*struct {
FileName string `json:"file_name"`
FileSize string `json:"file_size"`
} `json:"file_list"`
Result int `json:"result"`
}
taskIDJSON struct {
TaskID string `json:"task_id"`
}
taskIDInt64JSON struct {
TaskID int64 `json:"task_id"`
}
cloudDlAddTaskJSON struct {
*pcserror.PCSErrInfo
taskIDInt64JSON
}
cloudDlQueryTaskJSON struct {
TaskInfo map[string]*cloudDlTaskInfo `json:"task_info"`
*pcserror.PCSErrInfo
}
cloudDlListTaskJSON struct {
TaskInfo []*taskIDJSON `json:"task_info"`
*pcserror.PCSErrInfo
}
cloudDlClearJSON struct {
Total int `json:"total"`
*pcserror.PCSErrInfo
}
)
func (ci *cloudDlTaskInfo) convert() *CloudDlTaskInfo {
ci2 := &CloudDlTaskInfo{
Status: converter.MustInt(ci.Status),
FileSize: converter.MustInt64(ci.FileSize),
FinishedSize: converter.MustInt64(ci.FinishedSize),
CreateTime: converter.MustInt64(ci.CreateTime),
StartTime: converter.MustInt64(ci.StartTime),
FinishTime: converter.MustInt64(ci.FinishTime),
SavePath: ci.SavePath,
SourceURL: ci.SourceURL,
TaskName: ci.TaskName,
OdType: converter.MustInt(ci.OdType),
Result: ci.Result,
}
ci2.FileList = make([]*CloudDlFileInfo, 0, len(ci.FileList))
for _, v := range ci.FileList {
if v == nil {
continue
}
ci2.FileList = append(ci2.FileList, &CloudDlFileInfo{
FileName: v.FileName,
FileSize: converter.MustInt64(v.FileSize),
})
}
return ci2
}
// CloudDlAddTask 添加离线下载任务
func (pcs *BaiduPCS) CloudDlAddTask(sourceURL, savePath string) (taskID int64, pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareCloudDlAddTask(sourceURL, savePath)
if pcsError != nil {
return
}
defer dataReadCloser.Close()
errInfo := pcserror.NewPCSErrorInfo(OperationCloudDlAddTask)
taskInfo := cloudDlAddTaskJSON{
PCSErrInfo: errInfo,
}
pcsError = pcserror.HandleJSONParse(OperationCloudDlAddTask, dataReadCloser, &taskInfo)
if pcsError != nil {
return
}
return taskInfo.TaskID, nil
}
func (pcs *BaiduPCS) cloudDlQueryTask(op string, taskIDs []int64) (cl CloudDlTaskList, pcsError pcserror.Error) {
errInfo := pcserror.NewPCSErrorInfo(op)
if len(taskIDs) == 0 {
errInfo.ErrType = pcserror.ErrTypeOthers
errInfo.Err = errors.New("no input any task_ids")
return nil, errInfo
}
// TODO: 支持100条以上的task_id查询
if len(taskIDs) > 100 {
taskIDs = taskIDs[:100]
}
taskStrIDs := make([]string, len(taskIDs))
for k := range taskStrIDs {
taskStrIDs[k] = strconv.FormatInt(taskIDs[k], 10)
}
dataReadCloser, pcsError := pcs.PrepareCloudDlQueryTask(strings.Join(taskStrIDs, ","))
if pcsError != nil {
return
}
defer dataReadCloser.Close()
taskInfo := cloudDlQueryTaskJSON{
PCSErrInfo: errInfo,
}
pcsError = pcserror.HandleJSONParse(OperationCloudDlQueryTask, dataReadCloser, &taskInfo)
if pcsError != nil {
return
}
var v2 *CloudDlTaskInfo
cl = make(CloudDlTaskList, 0, len(taskStrIDs))
for k := range taskStrIDs {
var err error
v := taskInfo.TaskInfo[taskStrIDs[k]]
if v == nil {
continue
}
v2 = v.convert()
v2.TaskID, err = strconv.ParseInt(taskStrIDs[k], 10, 64)
if err != nil {
continue
}
v2.ParseText()
cl = append(cl, v2)
}
return cl, nil
}
// CloudDlQueryTask 精确查询离线下载任务
func (pcs *BaiduPCS) CloudDlQueryTask(taskIDs []int64) (cl CloudDlTaskList, pcsError pcserror.Error) {
return pcs.cloudDlQueryTask(OperationCloudDlQueryTask, taskIDs)
}
// CloudDlListTask 查询离线下载任务列表
func (pcs *BaiduPCS) CloudDlListTask() (cl CloudDlTaskList, pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareCloudDlListTask()
if pcsError != nil {
return
}
defer dataReadCloser.Close()
errInfo := pcserror.NewPCSErrorInfo(OperationCloudDlListTask)
taskInfo := cloudDlListTaskJSON{
PCSErrInfo: errInfo,
}
pcsError = pcserror.HandleJSONParse(OperationCloudDlListTask, dataReadCloser, &taskInfo)
if pcsError != nil {
return
}
// 没有任务
if len(taskInfo.TaskInfo) <= 0 {
return CloudDlTaskList{}, nil
}
var (
taskID int64
taskIDs = make([]int64, 0, len(taskInfo.TaskInfo))
)
for _, v := range taskInfo.TaskInfo {
if v == nil {
continue
}
var err error
if taskID, err = strconv.ParseInt(v.TaskID, 10, 64); err == nil {
taskIDs = append(taskIDs, taskID)
}
}
cl, pcsError = pcs.cloudDlQueryTask(OperationCloudDlListTask, taskIDs)
if pcsError != nil {
return nil, pcsError
}
return cl, nil
}
func (pcs *BaiduPCS) cloudDlManipTask(op string, taskID int64) (pcsError pcserror.Error) {
var dataReadCloser io.ReadCloser
switch op {
case OperationCloudDlCancelTask:
dataReadCloser, pcsError = pcs.PrepareCloudDlCancelTask(taskID)
case OperationCloudDlDeleteTask:
dataReadCloser, pcsError = pcs.PrepareCloudDlDeleteTask(taskID)
default:
panic("unknown op, " + op)
}
if pcsError != nil {
return
}
defer dataReadCloser.Close()
errInfo := pcserror.DecodePCSJSONError(op, dataReadCloser)
return errInfo
}
// CloudDlCancelTask 取消离线下载任务
func (pcs *BaiduPCS) CloudDlCancelTask(taskID int64) (pcsError pcserror.Error) {
return pcs.cloudDlManipTask(OperationCloudDlCancelTask, taskID)
}
// CloudDlDeleteTask 删除离线下载任务
func (pcs *BaiduPCS) CloudDlDeleteTask(taskID int64) (pcsError pcserror.Error) {
return pcs.cloudDlManipTask(OperationCloudDlDeleteTask, taskID)
}
// CloudDlClearTask 清空离线下载任务记录
func (pcs *BaiduPCS) CloudDlClearTask() (total int, pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareCloudDlClearTask()
if pcsError != nil {
return
}
defer dataReadCloser.Close()
errInfo := pcserror.NewPCSErrorInfo(OperationCloudDlClearTask)
clearInfo := cloudDlClearJSON{
PCSErrInfo: errInfo,
}
pcsError = pcserror.HandleJSONParse(OperationCloudDlClearTask, dataReadCloser, &clearInfo)
if pcsError != nil {
return
}
return clearInfo.Total, nil
}
// ParseText 解析状态码
func (ci *CloudDlTaskInfo) ParseText() {
switch ci.Status {
case 0:
ci.StatusText = "下载成功"
case 1:
ci.StatusText = "下载进行中"
case 2:
ci.StatusText = "系统错误"
case 3:
ci.StatusText = "资源不存在"
case 4:
ci.StatusText = "下载超时"
case 5:
ci.StatusText = "资源存在但下载失败"
case 6:
ci.StatusText = "存储空间不足"
case 7:
ci.StatusText = "任务取消"
default:
ci.StatusText = "未知状态码: " + strconv.Itoa(ci.Status)
}
}
func (cl CloudDlTaskList) String() string {
builder := &strings.Builder{}
tb := pcstable.NewTable(builder)
tb.SetHeader([]string{"#", "任务ID", "任务名称", "文件大小", "创建日期", "保存路径", "资源地址", "状态"})
for k, v := range cl {
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(v.TaskID, 10), v.TaskName, converter.ConvertFileSize(v.FileSize), pcstime.FormatTime(v.CreateTime), path.Clean(v.SavePath), v.SourceURL, v.StatusText})
}
tb.Render()
return builder.String()
}