forked from jsyzchen/pan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
323 lines (265 loc) · 8.56 KB
/
file.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
package file
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
"strconv"
"github.com/qiangyt/baidu_netdisk/conf"
"github.com/qiangyt/baidu_netdisk/utils/httpclient"
)
const (
ListUri = "/rest/2.0/xpan/file?method=list"
MetasUri = "/rest/2.0/xpan/multimedia?method=filemetas"
StreamingUri = "/rest/2.0/xpan/file?method=streaming"
SearchUri = "/rest/2.0/xpan/file?method=search"
FilemanagerUri = "/rest/2.0/xpan/file?method=filemanager"
)
type ListResponse struct {
conf.CloudDiskResponseBase
List []struct {
FsID uint64 `json:"fs_id"`
Path string `json:"path"`
ServerFileName string `json:"server_filename"`
Size int `json:"size"`
IsDir int `json:"isdir"`
Category int `json:"category"`
Md5 string `json:"md5"`
DirEmpty string `json:"dir_empty"`
Thumbs map[string]string `json:"thumbs"`
LocalCtime int `json:"local_ctime"`
LocalMtime int `json:"local_mtime"`
ServerCtime int `json:"server_ctime"`
ServerMtime int `json:"server_mtime"`
}
}
type MetasResponse struct {
ErrorCode int `json:"errno"`
ErrorMsg string `json:"errmsg"`
RequestID int
RequestIDStr string `json:"request_id"`
List []struct {
FsID uint64 `json:"fs_id"`
Path string `json:"path"`
Category int `json:"category"`
FileName string `json:"filename"`
IsDir int `json:"isdir"`
Size int `json:"size"`
Md5 string `json:"md5"`
DLink string `json:"dlink"`
Thumbs map[string]string `json:"thumbs"`
ServerCtime int `json:"server_ctime"`
ServerMtime int `json:"server_mtime"`
DateTaken int `json:"date_taken"`
Width int `json:"width"`
Height int `json:"height"`
}
}
type ManagerResponse struct {
conf.CloudDiskResponseBase
Info []struct {
Path string
TaskID int
Errno int
}
}
// See https://pan.baidu.com/union/doc/zksg0sb9z
type SearchResponse struct {
conf.CloudDiskResponseBase
List []struct {
Category int `json:"category"`
FsID uint64 `json:"fs_id"`
IsDir int `json:"isdir"`
LocalCtime int `json:"local_ctime"`
LocalMtime int `json:"local_mtime"`
ServerCtime int `json:"server_ctime"`
ServerMtime int `json:"server_mtime"`
Md5 string `json:"md5"`
Size int `json:"size"`
Thumbs map[string]string `json:"thumbs"`
Path string `json:"path"`
ServerFileName string `json:"server_filename"`
}
}
type FilemanagerResponse struct {
conf.CloudDiskResponseBase
Info []map[string]interface{} `json:"info"`
}
type File struct {
AccessToken string
}
func NewFileClient(accessToken string) *File {
return &File{
AccessToken: accessToken,
}
}
// 获取文件列表
func (f *File) List(dir string, start, limit int) (ListResponse, error) {
ret := ListResponse{}
v := url.Values{}
v.Add("access_token", f.AccessToken)
v.Add("dir", dir)
v.Add("start", strconv.Itoa(start))
v.Add("limit", strconv.Itoa(limit))
query := v.Encode()
requestUrl := conf.OpenApiDomain + ListUri + "&" + query
resp, err := httpclient.Get(requestUrl, map[string]string{})
if err != nil {
log.Println("httpclient.Get failed, err:", err)
return ret, err
}
if resp.StatusCode != 200 {
return ret, errors.New(fmt.Sprintf("HttpStatusCode is not equal to 200, httpStatusCode[%d], respBody[%s]", resp.StatusCode, string(resp.Body)))
}
if err := json.Unmarshal(resp.Body, &ret); err != nil {
return ret, err
}
if ret.ErrorCode != 0 { //错误码不为0
return ret, errors.New(fmt.Sprintf("error_code:%d, error_msg:%s", ret.ErrorCode, ret.ErrorMsg))
}
return ret, nil
}
// 通过FsID获取文件信息
func (f *File) Metas(fsIDs []uint64) (MetasResponse, error) {
ret := MetasResponse{}
fsIDsByte, err := json.Marshal(fsIDs)
if err != nil {
return ret, err
}
v := url.Values{}
v.Add("access_token", f.AccessToken)
v.Add("fsids", string(fsIDsByte))
v.Add("dlink", "1")
v.Add("thumb", "1")
v.Add("extra", "1")
query := v.Encode()
requestUrl := conf.OpenApiDomain + MetasUri + "&" + query
resp, err := httpclient.Get(requestUrl, map[string]string{})
if err != nil {
log.Println("httpclient.Get failed, err:", err)
return ret, err
}
if resp.StatusCode != 200 {
return ret, errors.New(fmt.Sprintf("HttpStatusCode is not equal to 200, httpStatusCode[%d], respBody[%s]", resp.StatusCode, string(resp.Body)))
}
if err := json.Unmarshal(resp.Body, &ret); err != nil {
return ret, err
}
if ret.ErrorCode != 0 { //错误码不为0
return ret, errors.New(fmt.Sprintf("error_code:%d, error_msg:%s", ret.ErrorCode, ret.ErrorMsg))
}
ret.RequestID, err = strconv.Atoi(ret.RequestIDStr)
if err != nil {
return ret, err
}
return ret, nil
}
// 获取音视频在线播放地址,转码类型有M3U8_AUTO_480=>视频ts、M3U8_FLV_264_480=>视频flv、M3U8_MP3_128=>音频mp3、M3U8_HLS_MP3_128=>音频ts
func (f *File) Streaming(path string, transcodingType string) (string, error) {
ret := ""
v := url.Values{}
v.Add("access_token", f.AccessToken)
v.Add("path", path)
v.Add("type", transcodingType)
query := v.Encode()
requestUrl := conf.OpenApiDomain + StreamingUri + "&" + query
resp, err := httpclient.Get(requestUrl, map[string]string{})
if err != nil {
log.Println("httpclient.Get failed, err:", err)
return ret, err
}
if resp.StatusCode != 200 {
return ret, errors.New(fmt.Sprintf("HttpStatusCode is not equal to 200, httpStatusCode[%d], respBody[%s]", resp.StatusCode, string(resp.Body)))
}
return string(resp.Body), nil
}
// 删除文件
func (f *File) Delete(paths []string) error {
filelist := make([]interface{}, len(paths))
for i, v := range paths {
filelist[i] = v
}
_, err := f.Filemanager("delete", filelist, "fail")
return err
}
// see https://pan.baidu.com/union/doc/mksg0s9l4
// 搜索文件
// opera: copy/move/rename/delete
// ondup: fail/newcopy/overwrite/skip
func (f *File) Filemanager(opera string, filelist []interface{}, ondup string) (FilemanagerResponse, error) {
ret := FilemanagerResponse{}
v := url.Values{}
v.Add("access_token", f.AccessToken)
v.Add("opera", opera)
query := v.Encode()
// path urlencode
bodyV := url.Values{}
bodyV.Add("async", "0")
if len(ondup) > 0 {
bodyV.Add("ondup", ondup)
}
filelistByte, err := json.Marshal(filelist)
if err != nil {
return ret, err
}
bodyV.Add("filelist", string(filelistByte))
body := bodyV.Encode()
requestUrl := conf.OpenApiDomain + FilemanagerUri + "&" + query
headers := make(map[string]string)
resp, err := httpclient.Post(requestUrl, headers, body)
if err != nil {
log.Println("httpclient.Post failed, err:", err)
return ret, err
}
if resp.StatusCode != 200 {
return ret, fmt.Errorf("HttpStatusCode is not equal to 200, httpStatusCode[%d], respBody[%s]", resp.StatusCode, string(resp.Body))
}
if err := json.Unmarshal(resp.Body, &ret); err != nil {
return ret, err
}
if ret.ErrorCode != 0 { //错误码不为0
return ret, fmt.Errorf(fmt.Sprintf("error_code:%d, error_msg:%s", ret.ErrorCode, ret.ErrorMsg))
}
return ret, nil
}
// 搜索文件
func (f *File) Search(dir string, key string, category int, recursion bool, web bool) (SearchResponse, error) {
ret := SearchResponse{}
v := url.Values{}
v.Add("access_token", f.AccessToken)
if len(dir) > 0 {
v.Add("dir", dir)
}
v.Add("key", key)
if category > 0 {
v.Add("category", strconv.Itoa(category))
}
if recursion {
v.Add("recursion", "1")
} else {
v.Add("recursion", "0")
}
if web {
v.Add("web", "1")
} else {
v.Add("web", "0")
}
query := v.Encode()
requestUrl := conf.OpenApiDomain + SearchUri + "&" + query
resp, err := httpclient.Get(requestUrl, map[string]string{})
if err != nil {
log.Println("httpclient.Get failed, err:", err)
return ret, err
}
if resp.StatusCode != 200 {
return ret, fmt.Errorf("HttpStatusCode is not equal to 200, httpStatusCode[%d], respBody[%s]", resp.StatusCode, string(resp.Body))
}
if err := json.Unmarshal(resp.Body, &ret); err != nil {
return ret, err
}
if ret.ErrorCode != 0 { //错误码不为0
return ret, fmt.Errorf(fmt.Sprintf("error_code:%d, error_msg:%s", ret.ErrorCode, ret.ErrorMsg))
}
return ret, nil
}