-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
driver.go
200 lines (184 loc) · 5.15 KB
/
driver.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
package _189
import (
"context"
"net/http"
"strings"
"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
)
type Cloud189 struct {
model.Storage
Addition
client *resty.Client
rsa Rsa
sessionKey string
}
func (d *Cloud189) Config() driver.Config {
return config
}
func (d *Cloud189) GetAddition() driver.Additional {
return &d.Addition
}
func (d *Cloud189) Init(ctx context.Context) error {
d.client = resty.New().
SetTimeout(base.DefaultTimeout).
SetRetryCount(3).
SetHeader("Referer", "https://cloud.189.cn/").
SetHeader("User-Agent", base.UserAgent)
return d.login()
}
func (d *Cloud189) Drop(ctx context.Context) error {
return nil
}
func (d *Cloud189) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
return d.getFiles(dir.GetID())
}
func (d *Cloud189) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
var resp DownResp
u := "https://cloud.189.cn/api/portal/getFileInfo.action"
_, err := d.request(u, http.MethodGet, func(req *resty.Request) {
req.SetQueryParam("fileId", file.GetID())
}, &resp)
if err != nil {
return nil, err
}
client := resty.NewWithClient(d.client.GetClient()).SetRedirectPolicy(
resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}))
res, err := client.R().SetHeader("User-Agent", base.UserAgent).Get("https:" + resp.FileDownloadUrl)
if err != nil {
return nil, err
}
log.Debugln(res.Status())
log.Debugln(res.String())
link := model.Link{}
log.Debugln("first url:", resp.FileDownloadUrl)
if res.StatusCode() == 302 {
link.URL = res.Header().Get("location")
log.Debugln("second url:", link.URL)
_, _ = client.R().Get(link.URL)
if res.StatusCode() == 302 {
link.URL = res.Header().Get("location")
}
log.Debugln("third url:", link.URL)
} else {
link.URL = resp.FileDownloadUrl
}
link.URL = strings.Replace(link.URL, "http://", "https://", 1)
return &link, nil
}
func (d *Cloud189) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
form := map[string]string{
"parentFolderId": parentDir.GetID(),
"folderName": dirName,
}
_, err := d.request("https://cloud.189.cn/api/open/file/createFolder.action", http.MethodPost, func(req *resty.Request) {
req.SetFormData(form)
}, nil)
return err
}
func (d *Cloud189) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
isFolder := 0
if srcObj.IsDir() {
isFolder = 1
}
taskInfos := []base.Json{
{
"fileId": srcObj.GetID(),
"fileName": srcObj.GetName(),
"isFolder": isFolder,
},
}
taskInfosBytes, err := utils.Json.Marshal(taskInfos)
if err != nil {
return err
}
form := map[string]string{
"type": "MOVE",
"targetFolderId": dstDir.GetID(),
"taskInfos": string(taskInfosBytes),
}
_, err = d.request("https://cloud.189.cn/api/open/batch/createBatchTask.action", http.MethodPost, func(req *resty.Request) {
req.SetFormData(form)
}, nil)
return err
}
func (d *Cloud189) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
url := "https://cloud.189.cn/api/open/file/renameFile.action"
idKey := "fileId"
nameKey := "destFileName"
if srcObj.IsDir() {
url = "https://cloud.189.cn/api/open/file/renameFolder.action"
idKey = "folderId"
nameKey = "destFolderName"
}
form := map[string]string{
idKey: srcObj.GetID(),
nameKey: newName,
}
_, err := d.request(url, http.MethodPost, func(req *resty.Request) {
req.SetFormData(form)
}, nil)
return err
}
func (d *Cloud189) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
isFolder := 0
if srcObj.IsDir() {
isFolder = 1
}
taskInfos := []base.Json{
{
"fileId": srcObj.GetID(),
"fileName": srcObj.GetName(),
"isFolder": isFolder,
},
}
taskInfosBytes, err := utils.Json.Marshal(taskInfos)
if err != nil {
return err
}
form := map[string]string{
"type": "COPY",
"targetFolderId": dstDir.GetID(),
"taskInfos": string(taskInfosBytes),
}
_, err = d.request("https://cloud.189.cn/api/open/batch/createBatchTask.action", http.MethodPost, func(req *resty.Request) {
req.SetFormData(form)
}, nil)
return err
}
func (d *Cloud189) Remove(ctx context.Context, obj model.Obj) error {
isFolder := 0
if obj.IsDir() {
isFolder = 1
}
taskInfos := []base.Json{
{
"fileId": obj.GetID(),
"fileName": obj.GetName(),
"isFolder": isFolder,
},
}
taskInfosBytes, err := utils.Json.Marshal(taskInfos)
if err != nil {
return err
}
form := map[string]string{
"type": "DELETE",
"targetFolderId": "",
"taskInfos": string(taskInfosBytes),
}
_, err = d.request("https://cloud.189.cn/api/open/batch/createBatchTask.action", http.MethodPost, func(req *resty.Request) {
req.SetFormData(form)
}, nil)
return err
}
func (d *Cloud189) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
return d.newUpload(ctx, dstDir, stream, up)
}
var _ driver.Driver = (*Cloud189)(nil)