forked from Angey40/BaiduPCS-Go
/
file_directory.go
346 lines (290 loc) · 8.35 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
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
package baidupcs
import (
"errors"
"github.com/iikira/BaiduPCS-Go/baidupcs/pcserror"
"github.com/iikira/BaiduPCS-Go/pcstable"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/pcsutil/pcstime"
"github.com/olekukonko/tablewriter"
"strconv"
"strings"
"unsafe"
)
type (
// OrderBy 排序字段
OrderBy string
// Order 升序降序
Order string
)
const (
// OrderByName 根据文件名排序
OrderByName OrderBy = "name"
// OrderByTime 根据时间排序
OrderByTime OrderBy = "time"
// OrderBySize 根据大小排序, 注意目录无大小
OrderBySize OrderBy = "size"
// OrderAsc 升序
OrderAsc Order = "asc"
// OrderDesc 降序
OrderDesc Order = "desc"
)
type (
// HandleFileDirectoryFunc 处理文件或目录的元信息, 返回值控制是否退出递归
HandleFileDirectoryFunc func(depth int, fdPath string, fd *FileDirectory, pcsError pcserror.Error) bool
// FileDirectory 文件或目录的元信息
FileDirectory struct {
FsID int64 // fs_id
AppID int64 // app_id
Path string // 路径
Filename string // 文件名 或 目录名
Ctime int64 // 创建日期
Mtime int64 // 修改日期
MD5 string // md5 值
BlockListJSON
Size int64 // 文件大小 (目录为0)
Isdir bool // 是否为目录
Ifhassubdir bool // 是否含有子目录 (只对目录有效)
Parent *FileDirectory // 父目录信息
Children FileDirectoryList // 子目录信息
}
// FileDirectoryList FileDirectory 的 指针数组
FileDirectoryList []*FileDirectory
// fdJSON 用于解析远程JSON数据
fdJSON struct {
FsID int64 `json:"fs_id"` // fs_id
AppID int64 `json:"app_id"`
Path string `json:"path"` // 路径
Filename string `json:"server_filename"` // 文件名 或 目录名
Ctime int64 `json:"ctime"` // 创建日期
Mtime int64 `json:"mtime"` // 修改日期
MD5 string `json:"md5"` // md5 值
BlockListJSON
Size int64 `json:"size"` // 文件大小 (目录为0)
IsdirInt int8 `json:"isdir"`
IfhassubdirInt int8 `json:"ifhassubdir"`
// 对齐
_ *fdJSON
_ []*fdJSON
}
fdData struct {
*pcserror.PCSErrInfo
List []*FileDirectory
}
fdDataJSONExport struct {
*pcserror.PCSErrInfo
List []*fdJSON `json:"list"`
}
// OrderOptions 列文件/目录可选项
OrderOptions struct {
By OrderBy
Order Order
}
)
// DefaultOrderOptions 默认的排序
var DefaultOrderOptions = &OrderOptions{
By: OrderByName,
Order: OrderAsc,
}
// FilesDirectoriesMeta 获取单个文件/目录的元信息
func (pcs *BaiduPCS) FilesDirectoriesMeta(path string) (data *FileDirectory, pcsError pcserror.Error) {
if path == "" {
path = "/"
}
fds, err := pcs.FilesDirectoriesBatchMeta(path)
if err != nil {
return nil, err
}
// 返回了多条元信息
if len(fds) != 1 {
return nil, &pcserror.PCSErrInfo{
Operation: OperationFilesDirectoriesMeta,
ErrType: pcserror.ErrTypeOthers,
Err: errors.New("未知返回数据"),
}
}
return fds[0], nil
}
// FilesDirectoriesBatchMeta 获取多个文件/目录的元信息
func (pcs *BaiduPCS) FilesDirectoriesBatchMeta(paths ...string) (data FileDirectoryList, pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareFilesDirectoriesBatchMeta(paths...)
if pcsError != nil {
return nil, pcsError
}
defer dataReadCloser.Close()
errInfo := pcserror.NewPCSErrorInfo(OperationFilesDirectoriesMeta)
// 服务器返回数据进行处理
jsonData := fdData{
PCSErrInfo: errInfo,
}
pcsError = handleJSONParse(OperationFilesDirectoriesMeta, dataReadCloser, (*fdDataJSONExport)(unsafe.Pointer(&jsonData)))
if pcsError != nil {
return
}
// 结果处理
data = jsonData.List
return
}
// FilesDirectoriesList 获取目录下的文件和目录列表
func (pcs *BaiduPCS) FilesDirectoriesList(path string, options *OrderOptions) (data FileDirectoryList, pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareFilesDirectoriesList(path, options)
if pcsError != nil {
return nil, pcsError
}
defer dataReadCloser.Close()
jsonData := fdData{
PCSErrInfo: pcserror.NewPCSErrorInfo(OperationFilesDirectoriesList),
}
pcsError = handleJSONParse(OperationFilesDirectoriesList, dataReadCloser, (*fdDataJSONExport)(unsafe.Pointer(&jsonData)))
if pcsError != nil {
return
}
// 可能是一个文件
if len(jsonData.List) == 0 {
var fd *FileDirectory
fd, pcsError = pcs.FilesDirectoriesMeta(path)
if pcsError != nil {
return
}
if fd.Isdir {
return
}
return FileDirectoryList{fd}, nil
}
data = jsonData.List
return
}
// Search 按文件名搜索文件, 不支持查找目录
func (pcs *BaiduPCS) Search(targetPath, keyword string, recursive bool) (fdl FileDirectoryList, pcsError pcserror.Error) {
if targetPath == "" {
targetPath = "/"
}
dataReadCloser, pcsError := pcs.PrepareSearch(targetPath, keyword, recursive)
if pcsError != nil {
return nil, pcsError
}
defer dataReadCloser.Close()
errInfo := pcserror.NewPCSErrorInfo(OperationSearch)
jsonData := fdData{
PCSErrInfo: errInfo,
}
pcsError = handleJSONParse(OperationSearch, dataReadCloser, (*fdDataJSONExport)(unsafe.Pointer(&jsonData)))
if pcsError != nil {
return
}
// 结果处理
fdl = jsonData.List
return
}
func (pcs *BaiduPCS) recurseList(path string, depth int, options *OrderOptions, handleFileDirectoryFunc HandleFileDirectoryFunc) (fdl FileDirectoryList, ok bool) {
fdl, pcsError := pcs.FilesDirectoriesList(path, options)
if pcsError != nil {
ok := handleFileDirectoryFunc(depth, path, nil, pcsError)
return nil, ok
}
for k := range fdl {
ok = handleFileDirectoryFunc(depth+1, fdl[k].Path, fdl[k], nil)
if !ok {
return
}
if !fdl[k].Isdir {
continue
}
fdl[k].Children, ok = pcs.recurseList(fdl[k].Path, depth+1, options, handleFileDirectoryFunc)
if !ok {
return
}
}
return fdl, true
}
// FilesDirectoriesRecurseList 递归获取目录下的文件和目录列表
func (pcs *BaiduPCS) FilesDirectoriesRecurseList(path string, options *OrderOptions, handleFileDirectoryFunc HandleFileDirectoryFunc) (data FileDirectoryList) {
data, _ = pcs.recurseList(path, 0, options, handleFileDirectoryFunc)
return data
}
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 {
var md5info string
if len(f.BlockList) > 1 {
md5info = "md5 (可能不正确)"
} else {
md5info = "md5 (截图请打码)"
}
tb.AppendBulk([][]string{
[]string{"类型", "文件"},
[]string{"文件路径", f.Path},
[]string{"文件名称", f.Filename},
[]string{"文件大小", strconv.FormatInt(f.Size, 10) + ", " + converter.ConvertFileSize(f.Size)},
[]string{md5info, f.MD5},
})
}
tb.Append([]string{"app_id", strconv.FormatInt(f.AppID, 10)})
tb.Append([]string{"fs_id", strconv.FormatInt(f.FsID, 10)})
tb.AppendBulk([][]string{
[]string{"创建日期", pcstime.FormatTime(f.Ctime)},
[]string{"修改日期", pcstime.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, 0, 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
}