forked from qjfoidnh/BaiduPCS-Go
/
jsontable.go
114 lines (94 loc) · 2.31 KB
/
jsontable.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
package baidupcs
import (
"errors"
"github.com/qjfoidnh/BaiduPCS-Go/pcstable"
"github.com/qjfoidnh/BaiduPCS-Go/pcsutil"
"github.com/json-iterator/go"
"path"
"strconv"
"strings"
)
type (
// PathJSON 网盘路径
PathJSON struct {
Path string `json:"path"`
}
// PathsListJSON 网盘路径列表
PathsListJSON struct {
List []*PathJSON `json:"list"`
}
// FsIDJSON 文件或目录ID
FsIDJSON struct {
FsID int64 `json:"fs_id"` // fs_id
}
// FsIDListJSON fs_id 列表
FsIDListJSON struct {
List []*FsIDJSON `json:"list"`
}
// CpMvJSON 源文件目录的地址和目标文件目录的地址
CpMvJSON struct {
From string `json:"from"` // 源文件或目录
To string `json:"to"` // 目标文件或目录
}
// CpMvJSONList CpMvJSON 列表
CpMvJSONList []*CpMvJSON
// CpMvListJSON []*CpMvJSON 对象数组
CpMvListJSON struct {
List CpMvJSONList `json:"list"`
}
// BlockListJSON 文件分块信息JSON
BlockListJSON struct {
BlockList []string `json:"block_list"`
}
)
var (
// ErrNilJSONValue 解析出的json值为空
ErrNilJSONValue = errors.New("json value is nil")
)
// JSON json 数据构造
func (plj *PathsListJSON) JSON(paths ...string) (data []byte, err error) {
plj.List = make([]*PathJSON, len(paths))
for k := range paths {
plj.List[k] = &PathJSON{
Path: paths[k],
}
}
data, err = jsoniter.Marshal(plj)
return
}
// JSON json 数据构造
func (cj *CpMvJSON) JSON() (data []byte, err error) {
data, err = jsoniter.Marshal(cj)
return
}
// JSON json 数据构造
func (clj *CpMvListJSON) JSON() (data []byte, err error) {
data, err = jsoniter.Marshal(clj)
return
}
func (clj *CpMvListJSON) String() string {
builder := &strings.Builder{}
tb := pcstable.NewTable(builder)
tb.SetHeader([]string{"#", "原路径", "目标路径"})
for k := range clj.List {
if clj.List[k] == nil {
continue
}
tb.Append([]string{strconv.Itoa(k), clj.List[k].From, clj.List[k].To})
}
tb.Render()
return builder.String()
}
// AllRelatedDir 获取所有相关的目录
func (cjl *CpMvJSONList) AllRelatedDir() (dirs []string) {
for _, cj := range *cjl {
fromDir, toDir := path.Dir(cj.From), path.Dir(cj.To)
if !pcsutil.ContainsString(dirs, fromDir) {
dirs = append(dirs, fromDir)
}
if !pcsutil.ContainsString(dirs, toDir) {
dirs = append(dirs, toDir)
}
}
return
}