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