forked from otokaze/189Cloud-Downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.go
360 lines (344 loc) · 8.7 KB
/
action.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
"context"
"flag"
"fmt"
"os"
"runtime"
"strings"
"189Cloud-Downloader/dao"
"189Cloud-Downloader/model"
"189Cloud-Downloader/utils"
"github.com/otokaze/go-kit/log"
"github.com/otokaze/go-kit/printcolor"
"github.com/peterh/liner"
"github.com/urfave/cli/v2"
)
var (
d = dao.New()
dirs = map[string]*model.Dir{}
user *model.UserInfo
share *model.ShareInfo
current *model.Dir
)
func loginAction(ctx *cli.Context) (err error) {
if cookie := ctx.String("cookie"); cookie != "" {
d.LoginWithCookie(ctx.Context, cookie)
var info interface{}
if info, err = d.GetLoginedInfo(ctx.Context); err != nil {
return
}
user = info.(*model.UserInfo)
log.Info("user(%+v)", user)
} else {
if ctx.Args().Len() < 2 {
log.Error("请输入格式正确的账号密码!格式:%s", ctx.Command.ArgsUsage)
return
}
if user, err = d.Login(ctx.Context, ctx.Args().Get(0), ctx.Args().Get(1)); err != nil {
log.Error("登陆失败!请确保账号密码正确。")
return
}
}
if current == nil {
current = &model.Dir{ID: "-11", Name: "全部文件", IsFolder: true, IsHome: true}
dirs[current.GetID()] = current
}
printcolor.Blue("登陆成功!你好,%s\n", user.GetName())
return
}
func logoutAction(ctx *cli.Context) (err error) {
return d.Logout(ctx.Context)
}
func versionAction(ctx *cli.Context) (err error) {
if version == "" {
version = "null"
}
println(ctx.App.Name, "version", version, runtime.GOOS)
return
}
func userInfoAction(ctx *cli.Context) (err error) {
var info interface{}
if info, err = d.GetLoginedInfo(ctx.Context, ctx.Bool("all")); err != nil {
printcolor.Red("%s\n", err.Error())
return
}
if ctx.Bool("all") {
println(info.(string))
return
}
var userinfo = info.(*model.UserInfo)
println("UserId:", userinfo.UserId)
println("UserAccount:", userinfo.GetName())
println("已用容量:", utils.FormatFileSize(userinfo.UsedSize))
println("可用容量:", utils.FormatFileSize(userinfo.Quota-userinfo.UsedSize))
println("总容量:", utils.FormatFileSize(userinfo.Quota))
user = userinfo
return
}
func shareAction(ctx *cli.Context) (err error) {
if ctx.Args().Len() < 1 {
log.Error("请输入格式正确的分享链接! 格式:%s", ctx.Command.ArgsUsage)
return
}
var (
shareCode string
accessCode = ctx.Args().Get(1)
)
if shareCode = utils.ParseShareCode(ctx.Args().Get(0)); shareCode == "" {
err = fmt.Errorf("没有找到ShareCode,请联系作者更新脚本!")
printcolor.Red("%v\n", err)
return
}
if share, err = d.GetShareInfo(ctx.Context, shareCode, accessCode); err != nil {
printcolor.Red("%v\n", err)
return
}
current = &model.Dir{ID: share.FileID, Name: share.FileName, IsFolder: share.IsFolder}
dirs[current.GetID()] = current
return
}
func lsAction(ctx *cli.Context) (err error) {
var isLong bool
if key := ctx.Context.Value("isLong"); key != nil {
isLong = key.(bool)
}
var (
pn, ps = ctx.Int("pn"), ctx.Int("ps")
order = ctx.String("order")
path = ctx.Args().Get(0)
)
if current == nil {
println("当前没有目录可载入!请尝试登陆或者加载分享链接。")
return
}
if path == "" && current != nil {
path = current.GetID()
}
var _dirs []*model.Dir
if path == "~" || current.IsHome {
if _dirs, err = d.GetHomeDirList(ctx.Context, pn, ps, order, path); err != nil {
log.Error("d.GetHomeDirList() pn(%d) ps(%d) folderId(%s) error(%v)", pn, ps, path, err)
return
}
} else if share != nil {
if _dirs, err = d.GetShareDirList(ctx.Context, share, pn, ps, order, path); err != nil {
log.Error("d.GetShareDirList() pn(%d) ps(%d) folderId(%s) error(%v)", pn, ps, path, err)
return
}
} else {
if path != "" {
printcolor.Red("no such file or directory: %s\n", path)
}
return
}
for idx, dir := range _dirs {
var format string
if isLong {
if dir.IsFolder {
format = fmt.Sprintf("[D]%s\t%s\t%s\t%s", dir.GetID(), utils.FormatFileSize(dir.FileListSize), dir.CreateDate, dir.Name)
} else {
format = fmt.Sprintf("[F]%s\t%s\t%s\t%s", dir.GetID(), utils.FormatFileSize(dir.Size), dir.CreateDate, dir.Name)
}
if idx != len(_dirs)-1 {
format += "\n"
}
} else {
format = dir.Name
if idx != len(_dirs)-1 {
format += "\t"
}
}
if dir.IsFolder {
printcolor.Blue(format)
} else {
print(format)
}
dirs[dir.GetID()] = dir
}
print("\n")
return
}
func llAction(ctx *cli.Context) (err error) {
var key interface{} = "isLong"
ctx.Context = context.WithValue(ctx.Context, key, true)
return lsAction(ctx)
}
func cdAction(ctx *cli.Context) (err error) {
var args []string
if argsInf := ctx.Context.Value("args"); argsInf != nil {
args = argsInf.([]string)
}
if len(args) < 1 {
log.Error("请输入正确的目标路径的ID! 格式:%s", ctx.Command.ArgsUsage)
return
}
var path = args[0]
if path == "/" {
if current.IsHome {
path = "-11"
} else {
path = share.FileID
}
} else if path == "../" || path == ".." {
if current.ParentID == "" {
log.Error("当前已在顶级目录!")
return
}
path = current.GetParentID()
} else if path == "~" {
path = "-11"
} else if path == "share" {
path = share.FileID
}
if dirs[path] == nil {
printcolor.Red("找不到目录: %s,请先尝试`ls`遍历。\n", path)
return
}
if !dirs[path].IsFolder {
printcolor.Red("%s 不是个目录,无法执行`cd`命令。\n", path)
return
}
current = dirs[path]
return
}
func pwdAction(ctx *cli.Context) (err error) {
if current == nil {
println("/")
return
}
if current.ParentID == nil {
fmt.Printf("/%s\n", current.Name)
return
}
var (
path = current.Name
parent = current.GetParentID()
)
for dirs[parent] != nil {
path = dirs[parent].Name + "/" + path
parent = dirs[parent].GetParentID()
}
fmt.Printf("/%s\n", path)
return
}
func getAction(ctx *cli.Context) (err error) {
var fileId string
if fileId = ctx.Args().Get(0); fileId == "" {
log.Error("请输入要下载的文件!格式:%s", ctx.Command.ArgsUsage)
return
}
var toPath string
if toPath = ctx.Args().Get(1); toPath == "" {
toPath, _ = os.Getwd()
} else {
toPath = strings.TrimRight(toPath, "/")
}
var c int
if c = ctx.Int("c"); c <= 0 {
if c = ctx.Int("concurrency"); c <= 0 {
c = 1
}
}
if user == nil {
log.Error("请先登陆189账号!")
return
}
var dir *model.Dir
if fileId == "." || fileId == "./" &&
current != nil {
dir = current
} else if fileId == "../" &&
dirs[current.GetParentID()] == nil {
log.Error("%s 已经是根目录!", current.GetID())
return
} else {
dir = dirs[fileId]
}
if dir == nil {
log.Error("找不到任何文件!")
return
}
var fn func(*model.Dir, string)
fn = func(dir *model.Dir, path string) {
if dir.IsFolder {
var dirs []*model.Dir
if dir.IsHome {
dirs, _ = d.GetHomeDirAll(ctx.Context, dir.GetID())
} else {
dirs, _ = d.GetShareDirAll(ctx.Context, share, dir.GetID())
}
for _, d := range dirs {
fn(d, path+"/"+dir.Name)
}
return
}
if url, _ := d.GetDownloadURL(ctx.Context, dir.GetID(), share); url != "" {
if err = d.Download(ctx.Context, url, path, c, ctx.String("tmp")); err == nil {
return
}
file, _ := os.OpenFile("./189Cloud-Downloader.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
log.Infow(file, "File(%+v), 下载失败!", dir)
}
}
fn(dir, toPath)
return
}
func afterAction(ctx *cli.Context) (err error) {
line := liner.NewLiner()
line.SetCtrlCAborts(true)
defer line.Close()
line.SetCompleter(func(line string) (cs []string) {
for _, c := range ctx.App.Commands {
if strings.HasPrefix(c.Name, strings.ToLower(line)) {
cs = append(cs, c.Name)
}
}
return
})
for {
var processName string
if current != nil {
processName = current.GetShortName()
}
var input string
if input, err = line.Prompt(processName + "> "); err != nil {
if err == liner.ErrPromptAborted {
err = nil
return
}
log.Error("line.Prompt() error(%v)", err)
continue
}
line.AppendHistory(input)
input = strings.TrimSpace(input)
args := strings.Split(input, " ")
if args[0] == "" {
continue
}
var command *cli.Command
if command = ctx.App.Command(args[0]); command == nil {
printcolor.Red("找不到指令: %s\n", args[0])
continue
}
var fset = flag.NewFlagSet(args[0], flag.ContinueOnError)
for _, f := range command.Flags {
f.Apply(fset)
}
if !command.SkipFlagParsing {
if err = fset.Parse(args[1:]); err != nil {
if err == flag.ErrHelp {
err = nil
continue
}
log.Error("fs.Parse(%v) error(%v)", args[1:], err)
continue
}
}
var key interface{} = "args"
nCtx := cli.NewContext(ctx.App, fset, ctx)
nCtx.Context = context.WithValue(nCtx.Context, key, args[1:])
nCtx.Command = command
command.Action(nCtx)
}
}