-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_directory.go
299 lines (247 loc) · 7.04 KB
/
file_directory.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
package baidupcs
import (
"bytes"
"fmt"
"github.com/iikira/BaiduPCS-Go/pcstable"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsverbose"
"github.com/iikira/BaiduPCS-Go/requester/multipartreader"
"github.com/json-iterator/go"
"github.com/olekukonko/tablewriter"
"strconv"
"strings"
)
// FileDirectory 文件或目录的详细信息
type FileDirectory struct {
FsID int64 // fs_id
Path string // 路径
Filename string // 文件名 或 目录名
Ctime int64 // 创建日期
Mtime int64 // 修改日期
MD5 string // md5 值
Size int64 // 文件大小 (目录为0)
Isdir bool // 是否为目录
Ifhassubdir bool // 是否含有子目录 (只对目录有效)
Parent *FileDirectory // 父目录信息
Children FileDirectoryList // 子目录信息
}
// FileDirectoryList FileDirectory 的 指针数组
type FileDirectoryList []*FileDirectory
// fdJSON 用于解析远程JSON数据
type fdJSON struct {
FsID int64 `json:"fs_id"` // fs_id
Path string `json:"path"` // 路径
Filename string `json:"server_filename"` // 文件名 或 目录名
Ctime int64 `json:"ctime"` // 创建日期
Mtime int64 `json:"mtime"` // 修改日期
MD5 string `json:"md5"` // md5 值
Size int64 `json:"size"` // 文件大小 (目录为0)
IsdirInt int `json:"isdir"`
IfhassubdirInt int `json:"ifhassubdir"`
}
// convert 将解析的远程JSON数据, 转换为 *FileDirectory
func (fj *fdJSON) convert() *FileDirectory {
return &FileDirectory{
FsID: fj.FsID,
Path: fj.Path,
Filename: fj.Filename,
Ctime: fj.Ctime,
Mtime: fj.Mtime,
MD5: fj.MD5,
Size: fj.Size,
Isdir: pcsutil.IntToBool(fj.IsdirInt),
Ifhassubdir: pcsutil.IntToBool(fj.IfhassubdirInt),
}
}
type fdData struct {
*ErrInfo
List []*fdJSON `json:"list"`
}
// FilesDirectoriesMeta 获取单个文件/目录的元信息
func (p *PCSApi) FilesDirectoriesMeta(path string) (data *FileDirectory, err error) {
operation := "获取单个文件/目录的元信息"
if path == "" {
path = "/"
}
fds, err := p.FilesDirectoriesBatchMeta(path)
if err != nil {
return nil, fmt.Errorf("%s, 路径: %s", err, path)
}
if len(fds) != 1 {
return nil, fmt.Errorf("%s发生错误, 未知返回数据, 路径: %s", operation, path)
}
return fds[0], nil
}
// FilesDirectoriesBatchMeta 获取多个文件/目录的元信息
func (p *PCSApi) FilesDirectoriesBatchMeta(paths ...string) (data FileDirectoryList, err error) {
operation := "获取文件/目录的元信息"
type listStr struct {
Path string `json:"path"`
}
type postStr struct {
List []listStr `json:"list"`
}
// 数据处理
post := &postStr{
List: make([]listStr, len(paths)),
}
for k := range paths {
post.List[k].Path = paths[k]
}
sendData, err := jsoniter.Marshal(post)
if err != nil {
panic(operation + ", json 数据构造失败, " + err.Error())
}
p.setAPI("file", "meta")
// 表单上传
mr := multipartreader.NewMultipartReader()
mr.AddFormFeild("param", bytes.NewReader(sendData))
resp, err := p.client.Req("POST", p.url.String(), mr, nil)
if err != nil {
return nil, fmt.Errorf("%s, 网络错误, %s", operation, err)
}
defer resp.Body.Close()
// 服务器返回数据进行处理
jsonData := &fdData{
ErrInfo: NewErrorInfo(operation),
}
d := jsoniter.NewDecoder(resp.Body)
err = d.Decode(jsonData)
if err != nil {
return nil, fmt.Errorf("%s, json 数据解析失败, %s", operation, err)
}
// 错误处理
errCode, _ := jsonData.ErrInfo.FindErr()
if errCode != 0 {
return nil, jsonData.ErrInfo
}
// 结果处理
data = make(FileDirectoryList, len(jsonData.List))
for k := range jsonData.List {
data[k] = jsonData.List[k].convert()
}
return
}
// FilesDirectoriesList 获取目录下的文件和目录列表, 可选是否递归
func (p *PCSApi) FilesDirectoriesList(path string, recurse bool) (data FileDirectoryList, err error) {
operation := "获取目录下的文件列表"
if path == "" {
path = "/"
}
p.setAPI("file", "list", map[string]string{
"path": path,
"by": "name",
"order": "asc", // 升序
"limit": "0-2147483647",
})
resp, err := p.client.Req("GET", p.url.String(), nil, nil)
if err != nil {
return
}
defer resp.Body.Close()
jsonData := &fdData{
ErrInfo: NewErrorInfo(operation),
}
d := jsoniter.NewDecoder(resp.Body)
err = d.Decode(jsonData)
if err != nil {
return nil, fmt.Errorf("%s, json 数据解析失败, %s", operation, err)
}
// 错误处理
errCode, _ := jsonData.ErrInfo.FindErr()
if errCode != 0 {
return nil, fmt.Errorf("%s, 路径: %s", jsonData.ErrInfo, path)
}
data = make(FileDirectoryList, len(jsonData.List))
for k := range jsonData.List {
data[k] = jsonData.List[k].convert()
// 递归获取子目录信息
if recurse && data[k].Isdir {
data[k].Children, err = p.FilesDirectoriesList(data[k].Path, recurse)
if err != nil {
pcsverbose.Verboseln(err)
}
}
}
return
}
func (f *FileDirectory) String() string {
builder := &strings.Builder{}
tb := pcstable.NewTable(builder)
tb.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT})
if f.Isdir {
tb.AppendBulk([][]string{
[]string{"类型", "目录"},
[]string{"目录路径", f.Path},
[]string{"目录名称", f.Filename},
})
} else {
tb.AppendBulk([][]string{
[]string{"类型", "文件"},
[]string{"文件路径", f.Path},
[]string{"文件名称", f.Filename},
[]string{"文件大小", strconv.FormatInt(f.Size, 10) + ", " + pcsutil.ConvertFileSize(f.Size)},
[]string{"md5", f.MD5},
})
}
tb.Append([]string{"fs_id", strconv.FormatInt(f.FsID, 10)})
tb.AppendBulk([][]string{
[]string{"创建日期", pcsutil.FormatTime(f.Ctime)},
[]string{"修改日期", pcsutil.FormatTime(f.Mtime)},
})
if f.Ifhassubdir {
tb.Append([]string{"是否含有子目录", "true"})
}
tb.Render()
return builder.String()
}
// TotalSize 获取目录下文件的总大小
func (fl FileDirectoryList) TotalSize() int64 {
var size int64
for k := range fl {
if fl[k] == nil {
continue
}
size += fl[k].Size
// 递归获取
if fl[k].Children != nil {
size += fl[k].Children.TotalSize()
}
}
return size
}
// Count 获取文件总数和目录总数
func (fl FileDirectoryList) Count() (fileN, directoryN int64) {
for k := range fl {
if fl[k] == nil {
continue
}
if fl[k].Isdir {
directoryN++
} else {
fileN++
}
// 递归获取
if fl[k].Children != nil {
fN, dN := fl[k].Children.Count()
fileN += fN
directoryN += dN
}
}
return
}
// AllFilePaths 返回所有的网盘路径, 包括子目录
func (fl FileDirectoryList) AllFilePaths() (pcspaths []string) {
fN, dN := fl.Count()
pcspaths = make([]string, fN+dN)
for k := range fl {
if fl[k] == nil {
continue
}
pcspaths = append(pcspaths, fl[k].Path)
if fl[k].Children != nil {
pcspaths = append(pcspaths, fl[k].Children.AllFilePaths()...)
}
}
return
}