forked from Angey40/BaiduPCS-Go
/
share.go
141 lines (114 loc) · 2.91 KB
/
share.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
package baidupcs
import (
"errors"
"github.com/iikira/BaiduPCS-Go/baidupcs/pcserror"
)
type (
// ShareOption 分享可选项
ShareOption struct {
Password string // 密码
Period int // 有效期
}
// Shared 分享信息
Shared struct {
Link string `json:"link"`
ShareID int64 `json:"shareid"`
}
// ShareRecordInfo 分享信息
ShareRecordInfo struct {
ShareID int64 `json:"shareId"`
FsIds []string `json:"fsIds"`
Passwd string `json:"passwd"`
Shortlink string `json:"shortlink"`
TypicalPath string `json:"typicalPath"`
}
sharePSetJSON struct {
*Shared
*pcserror.PanErrorInfo
}
shareListJSON struct {
List ShareRecordInfoList `json:"list"`
*pcserror.PanErrorInfo
}
)
// Clean 清理
func (sri *ShareRecordInfo) Clean() {
if sri.Passwd == "0" {
sri.Passwd = ""
}
}
// HasPasswd 是否需要提取码
func (sri *ShareRecordInfo) HasPasswd() bool {
return sri.Passwd != "" && sri.Passwd != "0"
}
// ShareRecordInfoList 分享信息列表
type ShareRecordInfoList []*ShareRecordInfo
// Clean 清理
func (sril *ShareRecordInfoList) Clean() {
for _, sri := range *sril {
if sri == nil {
continue
}
sri.Clean()
}
}
// ShareSet 分享文件
func (pcs *BaiduPCS) ShareSet(paths []string, option *ShareOption) (s *Shared, pcsError pcserror.Error) {
if option == nil {
option = &ShareOption{}
}
dataReadCloser, pcsError := pcs.PrepareSharePSet(paths, option.Period)
if pcsError != nil {
return
}
defer dataReadCloser.Close()
errInfo := pcserror.NewPanErrorInfo(OperationShareSet)
jsonData := sharePSetJSON{
Shared: &Shared{},
PanErrorInfo: errInfo,
}
pcsError = handleJSONParse(OperationShareSet, dataReadCloser, &jsonData)
if pcsError != nil {
return
}
if jsonData.Link == "" {
errInfo.ErrType = pcserror.ErrTypeOthers
errInfo.Err = errors.New("未找到分享链接")
return nil, errInfo
}
return jsonData.Shared, nil
}
// ShareCancel 取消分享
func (pcs *BaiduPCS) ShareCancel(shareIDs []int64) (pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareShareCancel(shareIDs)
if pcsError != nil {
return
}
defer dataReadCloser.Close()
pcsError = pcserror.DecodePanJSONError(OperationShareCancel, dataReadCloser)
return
}
// ShareList 列出分享列表
func (pcs *BaiduPCS) ShareList(page int) (records ShareRecordInfoList, pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareShareList(page)
if pcsError != nil {
return
}
defer dataReadCloser.Close()
errInfo := pcserror.NewPanErrorInfo(OperationShareList)
jsonData := shareListJSON{
List: records,
PanErrorInfo: errInfo,
}
pcsError = handleJSONParse(OperationShareList, dataReadCloser, &jsonData)
if pcsError != nil {
return
}
if jsonData.List == nil {
errInfo.ErrType = pcserror.ErrTypeOthers
errInfo.Err = errors.New("shared list is nil")
return nil, errInfo
}
jsonData.List.Clean()
return jsonData.List, nil
}