forked from Angey40/BaiduPCS-Go
/
share.go
173 lines (147 loc) · 3.96 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
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
package pcscommand
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/baidupcs"
"github.com/iikira/BaiduPCS-Go/internal/pcsconfig"
"github.com/iikira/BaiduPCS-Go/pcstable"
"github.com/iikira/BaiduPCS-Go/requester"
"github.com/iikira/baidu-tools/pan"
"net/url"
"os"
"path"
"strconv"
"strings"
)
// RunShareSet 执行分享
func RunShareSet(paths []string, option *baidupcs.ShareOption) {
pcspaths, err := getAllAbsPaths(paths...)
if err != nil {
fmt.Println(err)
return
}
shared, err := GetBaiduPCS().ShareSet(pcspaths, option)
if err != nil {
fmt.Printf("%s失败: %s\n", baidupcs.OperationShareSet, err)
return
}
fmt.Printf("shareID: %d, 链接: %s\n", shared.ShareID, shared.Link)
}
// RunShareCancel 执行取消分享
func RunShareCancel(shareIDs []int64) {
if len(shareIDs) == 0 {
fmt.Printf("%s失败, 没有任何 shareid\n", baidupcs.OperationShareCancel)
return
}
err := GetBaiduPCS().ShareCancel(shareIDs)
if err != nil {
fmt.Printf("%s失败: %s\n", baidupcs.OperationShareCancel, err)
return
}
fmt.Printf("%s成功\n", baidupcs.OperationShareCancel)
}
// RunShareList 执行列出分享列表
func RunShareList() {
records, err := GetBaiduPCS().ShareList(1)
if err != nil {
fmt.Printf("%s失败: %s\n", baidupcs.OperationShareList, err)
return
}
tb := pcstable.NewTable(os.Stdout)
tb.SetHeader([]string{"#", "ShareID", "分享链接", "提取密码", "根目录", "路径"})
for k, record := range records {
if record == nil {
continue
}
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(record.ShareID, 10), record.Shortlink, record.Passwd, record.TypicalPath[:strings.LastIndex(record.TypicalPath, "/")+1], record.TypicalPath})
}
tb.Render()
}
func getShareDLink(pcspath string) (dlink string) {
var (
pcs = GetBaiduPCS()
)
for page := 1; ; page++ {
records, pcsError := pcs.ShareList(page)
if pcsError != nil {
pcsCommandVerbose.Warn(pcsError.Error())
break
}
// 完成
if len(records) == 0 {
break
}
for _, record := range records {
if record == nil {
continue
}
rootSharePath := path.Dir(record.TypicalPath)
if len(record.FsIds) == 1 {
if strings.HasPrefix(pcspath, record.TypicalPath) {
dlink = getLink(record.ShareID, record.Shortlink, record.Passwd, rootSharePath, strings.TrimPrefix(pcspath, rootSharePath))
return
}
continue
}
// 尝试获取
if strings.HasPrefix(pcspath, rootSharePath) {
dlink = getLink(record.ShareID, record.Shortlink, record.Passwd, rootSharePath, strings.TrimPrefix(pcspath, rootSharePath))
if dlink != "" {
return
}
continue
}
}
}
pcsCommandVerbose.Infof("%s: 未在已分享列表中找到分享信息\n", pcspath)
s, pcsError := pcs.ShareSet([]string{pcspath}, nil)
if pcsError != nil {
pcsCommandVerbose.Warn(pcsError.Error())
return ""
}
// 取消分享
defer pcs.ShareCancel([]int64{s.ShareID})
dlink = getLink(s.ShareID, s.Link, "", path.Dir(pcspath), path.Base(pcspath))
return
}
func getLink(shareID int64, shareLink, passwd, rootSharePath, filePath string) (dlink string) {
sInfo := pan.NewSharedInfo(shareLink)
sInfo.Client = requester.NewHTTPClient()
sInfo.Client.SetHTTPSecure(pcsconfig.Config.EnableHTTPS())
if passwd != "" {
err := sInfo.Auth(passwd)
if err != nil {
pcsCommandVerbose.Warn(err.Error())
return ""
}
}
uk, pcsError := GetBaiduPCS().UK()
if pcsError != nil {
// 获取UK失败, 从分享链接获取信息
pcsCommandVerbose.Warn(pcsError.Error())
err := sInfo.InitInfo()
if err != nil {
pcsCommandVerbose.Warn(err.Error())
return ""
}
} else {
sInfo.UK = uk
sInfo.ShareID = shareID
sInfo.RootSharePath = rootSharePath
}
fd, err := sInfo.Meta(filePath)
if err != nil {
pcsCommandVerbose.Warn(err.Error())
return ""
}
u, err := url.Parse(fd.Dlink)
if err != nil {
pcsCommandVerbose.Warn(err.Error())
return ""
}
if pcsconfig.Config.EnableHTTPS() {
u.Scheme = "https"
} else {
u.Scheme = "http"
}
return u.String()
}