Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/189/driver.go
1986 views
1
package _189
2
3
import (
4
"context"
5
"net/http"
6
"strings"
7
8
"github.com/alist-org/alist/v3/drivers/base"
9
"github.com/alist-org/alist/v3/internal/driver"
10
"github.com/alist-org/alist/v3/internal/model"
11
"github.com/alist-org/alist/v3/pkg/utils"
12
"github.com/go-resty/resty/v2"
13
log "github.com/sirupsen/logrus"
14
)
15
16
type Cloud189 struct {
17
model.Storage
18
Addition
19
client *resty.Client
20
rsa Rsa
21
sessionKey string
22
}
23
24
func (d *Cloud189) Config() driver.Config {
25
return config
26
}
27
28
func (d *Cloud189) GetAddition() driver.Additional {
29
return &d.Addition
30
}
31
32
func (d *Cloud189) Init(ctx context.Context) error {
33
d.client = base.NewRestyClient().
34
SetHeader("Referer", "https://cloud.189.cn/")
35
return d.newLogin()
36
}
37
38
func (d *Cloud189) Drop(ctx context.Context) error {
39
return nil
40
}
41
42
func (d *Cloud189) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
43
return d.getFiles(dir.GetID())
44
}
45
46
func (d *Cloud189) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
47
var resp DownResp
48
u := "https://cloud.189.cn/api/portal/getFileInfo.action"
49
_, err := d.request(u, http.MethodGet, func(req *resty.Request) {
50
req.SetQueryParam("fileId", file.GetID())
51
}, &resp)
52
if err != nil {
53
return nil, err
54
}
55
client := resty.NewWithClient(d.client.GetClient()).SetRedirectPolicy(
56
resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
57
return http.ErrUseLastResponse
58
}))
59
res, err := client.R().SetHeader("User-Agent", base.UserAgent).Get("https:" + resp.FileDownloadUrl)
60
if err != nil {
61
return nil, err
62
}
63
log.Debugln(res.Status())
64
log.Debugln(res.String())
65
link := model.Link{}
66
log.Debugln("first url:", resp.FileDownloadUrl)
67
if res.StatusCode() == 302 {
68
link.URL = res.Header().Get("location")
69
log.Debugln("second url:", link.URL)
70
_, _ = client.R().Get(link.URL)
71
if res.StatusCode() == 302 {
72
link.URL = res.Header().Get("location")
73
}
74
log.Debugln("third url:", link.URL)
75
} else {
76
link.URL = resp.FileDownloadUrl
77
}
78
link.URL = strings.Replace(link.URL, "http://", "https://", 1)
79
return &link, nil
80
}
81
82
func (d *Cloud189) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
83
safeName := d.sanitizeName(dirName)
84
form := map[string]string{
85
"parentFolderId": parentDir.GetID(),
86
"folderName": safeName,
87
}
88
_, err := d.request("https://cloud.189.cn/api/open/file/createFolder.action", http.MethodPost, func(req *resty.Request) {
89
req.SetFormData(form)
90
}, nil)
91
return err
92
}
93
94
func (d *Cloud189) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
95
isFolder := 0
96
if srcObj.IsDir() {
97
isFolder = 1
98
}
99
taskInfos := []base.Json{
100
{
101
"fileId": srcObj.GetID(),
102
"fileName": srcObj.GetName(),
103
"isFolder": isFolder,
104
},
105
}
106
taskInfosBytes, err := utils.Json.Marshal(taskInfos)
107
if err != nil {
108
return err
109
}
110
form := map[string]string{
111
"type": "MOVE",
112
"targetFolderId": dstDir.GetID(),
113
"taskInfos": string(taskInfosBytes),
114
}
115
_, err = d.request("https://cloud.189.cn/api/open/batch/createBatchTask.action", http.MethodPost, func(req *resty.Request) {
116
req.SetFormData(form)
117
}, nil)
118
return err
119
}
120
121
func (d *Cloud189) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
122
url := "https://cloud.189.cn/api/open/file/renameFile.action"
123
idKey := "fileId"
124
nameKey := "destFileName"
125
if srcObj.IsDir() {
126
url = "https://cloud.189.cn/api/open/file/renameFolder.action"
127
idKey = "folderId"
128
nameKey = "destFolderName"
129
}
130
safeName := d.sanitizeName(newName)
131
form := map[string]string{
132
idKey: srcObj.GetID(),
133
nameKey: safeName,
134
}
135
_, err := d.request(url, http.MethodPost, func(req *resty.Request) {
136
req.SetFormData(form)
137
}, nil)
138
return err
139
}
140
141
func (d *Cloud189) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
142
isFolder := 0
143
if srcObj.IsDir() {
144
isFolder = 1
145
}
146
taskInfos := []base.Json{
147
{
148
"fileId": srcObj.GetID(),
149
"fileName": srcObj.GetName(),
150
"isFolder": isFolder,
151
},
152
}
153
taskInfosBytes, err := utils.Json.Marshal(taskInfos)
154
if err != nil {
155
return err
156
}
157
form := map[string]string{
158
"type": "COPY",
159
"targetFolderId": dstDir.GetID(),
160
"taskInfos": string(taskInfosBytes),
161
}
162
_, err = d.request("https://cloud.189.cn/api/open/batch/createBatchTask.action", http.MethodPost, func(req *resty.Request) {
163
req.SetFormData(form)
164
}, nil)
165
return err
166
}
167
168
func (d *Cloud189) Remove(ctx context.Context, obj model.Obj) error {
169
isFolder := 0
170
if obj.IsDir() {
171
isFolder = 1
172
}
173
taskInfos := []base.Json{
174
{
175
"fileId": obj.GetID(),
176
"fileName": obj.GetName(),
177
"isFolder": isFolder,
178
},
179
}
180
taskInfosBytes, err := utils.Json.Marshal(taskInfos)
181
if err != nil {
182
return err
183
}
184
form := map[string]string{
185
"type": "DELETE",
186
"targetFolderId": "",
187
"taskInfos": string(taskInfosBytes),
188
}
189
_, err = d.request("https://cloud.189.cn/api/open/batch/createBatchTask.action", http.MethodPost, func(req *resty.Request) {
190
req.SetFormData(form)
191
}, nil)
192
return err
193
}
194
195
func (d *Cloud189) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
196
return d.newUpload(ctx, dstDir, stream, up)
197
}
198
199
var _ driver.Driver = (*Cloud189)(nil)
200
201