-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloud_dl.go
300 lines (251 loc) · 7.4 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
package baidupcs
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/pcstable"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/json-iterator/go"
"strconv"
"strings"
)
// CloudDlFileInfo 离线下载的文件信息
type CloudDlFileInfo struct {
FileName string `json:"file_name"`
FileSize int64 `json:"file_size"`
}
// CloudDlTaskInfo 离线下载的任务信息
type 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 离线下载的任务信息列表
type CloudDlTaskList []*CloudDlTaskInfo
// cloudDlTaskInfo 用于解析远程返回的JSON
type 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"`
}
func (ci *cloudDlTaskInfo) convert() *CloudDlTaskInfo {
ci2 := &CloudDlTaskInfo{
Status: pcsutil.MustInt(ci.Status),
FileSize: pcsutil.MustInt64(ci.FileSize),
FinishedSize: pcsutil.MustInt64(ci.FinishedSize),
CreateTime: pcsutil.MustInt64(ci.CreateTime),
StartTime: pcsutil.MustInt64(ci.StartTime),
FinishTime: pcsutil.MustInt64(ci.FinishTime),
SavePath: ci.SavePath,
SourceURL: ci.SourceURL,
TaskName: ci.TaskName,
OdType: pcsutil.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: pcsutil.MustInt64(v.FileSize),
})
}
return ci2
}
// CloudDlAddTask 添加离线下载任务
func (pcs *BaiduPCS) CloudDlAddTask(sourceURL, savePath string) (taskID int64, err error) {
dataReadCloser, err := pcs.PrepareCloudDlAddTask(sourceURL, savePath)
if err != nil {
return
}
defer dataReadCloser.Close()
taskInfo := &struct {
TaskID int64 `json:"task_id"`
*ErrInfo
}{
ErrInfo: NewErrorInfo(OperationCloudDlAddTask),
}
d := jsoniter.NewDecoder(dataReadCloser)
err = d.Decode(taskInfo)
if err != nil {
return 0, fmt.Errorf("%s, %s, %s", OperationCloudDlAddTask, StrJSONParseError, err)
}
if taskInfo.ErrCode != 0 {
return 0, taskInfo.ErrInfo
}
return taskInfo.TaskID, nil
}
// CloudDlQueryTask 精确查询离线下载任务
func (pcs *BaiduPCS) CloudDlQueryTask(taskIDs []int64) (cl CloudDlTaskList, err error) {
if len(taskIDs) == 0 {
return nil, fmt.Errorf("%s, no input any task_ids", OperationCloudDlQueryTask)
}
taskStrIDs := make([]string, len(taskIDs))
for k := range taskStrIDs {
taskStrIDs[k] = strconv.FormatInt(taskIDs[k], 10)
}
dataReadCloser, err := pcs.PrepareCloudDlQueryTask(strings.Join(taskStrIDs, ","))
if err != nil {
return
}
defer dataReadCloser.Close()
taskInfo := &struct {
TaskInfo map[string]*cloudDlTaskInfo `json:"task_info"`
*ErrInfo
}{
ErrInfo: NewErrorInfo(OperationCloudDlQueryTask),
}
d := jsoniter.NewDecoder(dataReadCloser)
err = d.Decode(taskInfo)
if err != nil {
return nil, fmt.Errorf("%s, %s, %s", OperationCloudDlQueryTask, StrJSONParseError, err)
}
if taskInfo.ErrCode != 0 {
return nil, taskInfo.ErrInfo
}
var v2 *CloudDlTaskInfo
cl = make(CloudDlTaskList, 0, len(taskStrIDs))
for k := range taskStrIDs {
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
}
// CloudDlListTask 查询离线下载任务列表
func (pcs *BaiduPCS) CloudDlListTask() (cl CloudDlTaskList, err error) {
dataReadCloser, err := pcs.PrepareCloudDlListTask()
if err != nil {
return
}
defer dataReadCloser.Close()
taskInfo := &struct {
TaskInfo []*struct {
TaskID string `json:"task_id"`
} `json:"task_info"`
*ErrInfo
}{
ErrInfo: NewErrorInfo(OperationCloudDlListTask),
}
d := jsoniter.NewDecoder(dataReadCloser)
err = d.Decode(taskInfo)
if err != nil {
return nil, fmt.Errorf("%s, %s, %s", OperationCloudDlListTask, StrJSONParseError, err)
}
if taskInfo.ErrCode != 0 {
return nil, taskInfo.ErrInfo
}
var (
taskID int64
taskIDs = make([]int64, 0, len(taskInfo.TaskInfo))
)
for _, v := range taskInfo.TaskInfo {
if v == nil {
continue
}
if taskID, err = strconv.ParseInt(v.TaskID, 10, 64); err == nil {
taskIDs = append(taskIDs, taskID)
}
}
return pcs.CloudDlQueryTask(taskIDs)
}
// CloudDlCancelTask 取消离线下载任务
func (pcs *BaiduPCS) CloudDlCancelTask(taskID int64) (err error) {
dataReadCloser, err := pcs.PrepareCloudDlCancelTask(taskID)
if err != nil {
return
}
defer dataReadCloser.Close()
errInfo := NewErrorInfo(OperationCloudDlCancelTask)
d := jsoniter.NewDecoder(dataReadCloser)
err = d.Decode(errInfo)
if err != nil {
return fmt.Errorf("%s, %s, %s", OperationCloudDlCancelTask, StrJSONParseError, err)
}
if errInfo.ErrCode != 0 {
return errInfo
}
return nil
}
// CloudDlDeleteTask 删除离线下载任务
func (pcs *BaiduPCS) CloudDlDeleteTask(taskID int64) (err error) {
dataReadCloser, err := pcs.PrepareCloudDlDeleteTask(taskID)
if err != nil {
return
}
defer dataReadCloser.Close()
errInfo := NewErrorInfo(OperationCloudDlDeleteTask)
d := jsoniter.NewDecoder(dataReadCloser)
err = d.Decode(errInfo)
if err != nil {
return fmt.Errorf("%s, %s, %s", OperationCloudDlDeleteTask, StrJSONParseError, err)
}
if errInfo.ErrCode != 0 {
return errInfo
}
return 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, pcsutil.ConvertFileSize(v.FileSize), pcsutil.FormatTime(v.CreateTime), v.SavePath, v.SourceURL, v.StatusText})
}
tb.Render()
return builder.String()
}