Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/offline_download/115/client.go
1562 views
1
package _115
2
3
import (
4
"context"
5
"fmt"
6
"github.com/alist-org/alist/v3/internal/conf"
7
"github.com/alist-org/alist/v3/internal/setting"
8
9
"github.com/alist-org/alist/v3/drivers/115"
10
"github.com/alist-org/alist/v3/internal/errs"
11
"github.com/alist-org/alist/v3/internal/model"
12
"github.com/alist-org/alist/v3/internal/offline_download/tool"
13
"github.com/alist-org/alist/v3/internal/op"
14
)
15
16
type Cloud115 struct {
17
refreshTaskCache bool
18
}
19
20
func (p *Cloud115) Name() string {
21
return "115 Cloud"
22
}
23
24
func (p *Cloud115) Items() []model.SettingItem {
25
return nil
26
}
27
28
func (p *Cloud115) Run(task *tool.DownloadTask) error {
29
return errs.NotSupport
30
}
31
32
func (p *Cloud115) Init() (string, error) {
33
p.refreshTaskCache = false
34
return "ok", nil
35
}
36
37
func (p *Cloud115) IsReady() bool {
38
tempDir := setting.GetStr(conf.Pan115TempDir)
39
if tempDir == "" {
40
return false
41
}
42
storage, _, err := op.GetStorageAndActualPath(tempDir)
43
if err != nil {
44
return false
45
}
46
if _, ok := storage.(*_115.Pan115); !ok {
47
return false
48
}
49
return true
50
}
51
52
func (p *Cloud115) AddURL(args *tool.AddUrlArgs) (string, error) {
53
// 添加新任务刷新缓存
54
p.refreshTaskCache = true
55
storage, actualPath, err := op.GetStorageAndActualPath(args.TempDir)
56
if err != nil {
57
return "", err
58
}
59
driver115, ok := storage.(*_115.Pan115)
60
if !ok {
61
return "", fmt.Errorf("unsupported storage driver for offline download, only 115 Cloud is supported")
62
}
63
64
ctx := context.Background()
65
66
if err := op.MakeDir(ctx, storage, actualPath); err != nil {
67
return "", err
68
}
69
70
parentDir, err := op.GetUnwrap(ctx, storage, actualPath)
71
if err != nil {
72
return "", err
73
}
74
75
hashs, err := driver115.OfflineDownload(ctx, []string{args.Url}, parentDir)
76
if err != nil || len(hashs) < 1 {
77
return "", fmt.Errorf("failed to add offline download task: %w", err)
78
}
79
80
return hashs[0], nil
81
}
82
83
func (p *Cloud115) Remove(task *tool.DownloadTask) error {
84
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
85
if err != nil {
86
return err
87
}
88
driver115, ok := storage.(*_115.Pan115)
89
if !ok {
90
return fmt.Errorf("unsupported storage driver for offline download, only 115 Cloud is supported")
91
}
92
93
ctx := context.Background()
94
if err := driver115.DeleteOfflineTasks(ctx, []string{task.GID}, false); err != nil {
95
return err
96
}
97
return nil
98
}
99
100
func (p *Cloud115) Status(task *tool.DownloadTask) (*tool.Status, error) {
101
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
102
if err != nil {
103
return nil, err
104
}
105
driver115, ok := storage.(*_115.Pan115)
106
if !ok {
107
return nil, fmt.Errorf("unsupported storage driver for offline download, only 115 Cloud is supported")
108
}
109
110
tasks, err := driver115.OfflineList(context.Background())
111
if err != nil {
112
return nil, err
113
}
114
115
s := &tool.Status{
116
Progress: 0,
117
NewGID: "",
118
Completed: false,
119
Status: "the task has been deleted",
120
Err: nil,
121
}
122
for _, t := range tasks {
123
if t.InfoHash == task.GID {
124
s.Progress = t.Percent
125
s.Status = t.GetStatus()
126
s.Completed = t.IsDone()
127
s.TotalBytes = t.Size
128
if t.IsFailed() {
129
s.Err = fmt.Errorf(t.GetStatus())
130
}
131
return s, nil
132
}
133
}
134
s.Err = fmt.Errorf("the task has been deleted")
135
return nil, nil
136
}
137
138
var _ tool.Tool = (*Cloud115)(nil)
139
140
func init() {
141
tool.Tools.Add(&Cloud115{})
142
}
143
144