Skip to content

Commit d89d0a0

Browse files
authored
feat(189tv): add 189cloudTV driver (#418)
1 parent 14d57ae commit d89d0a0

File tree

6 files changed

+1353
-0
lines changed

6 files changed

+1353
-0
lines changed

drivers/189_tv/driver.go

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
package _189_tv
2+
3+
import (
4+
"container/ring"
5+
"context"
6+
"net/http"
7+
"strconv"
8+
"strings"
9+
"time"
10+
11+
"github.com/OpenListTeam/OpenList/drivers/base"
12+
"github.com/OpenListTeam/OpenList/internal/driver"
13+
"github.com/OpenListTeam/OpenList/internal/errs"
14+
"github.com/OpenListTeam/OpenList/internal/model"
15+
"github.com/go-resty/resty/v2"
16+
)
17+
18+
type Cloud189TV struct {
19+
model.Storage
20+
Addition
21+
client *resty.Client
22+
tokenInfo *AppSessionResp
23+
uploadThread int
24+
familyTransferFolder *ring.Ring
25+
cleanFamilyTransferFile func()
26+
storageConfig driver.Config
27+
}
28+
29+
func (y *Cloud189TV) Config() driver.Config {
30+
if y.storageConfig.Name == "" {
31+
y.storageConfig = config
32+
}
33+
return y.storageConfig
34+
}
35+
36+
func (y *Cloud189TV) GetAddition() driver.Additional {
37+
return &y.Addition
38+
}
39+
40+
func (y *Cloud189TV) Init(ctx context.Context) (err error) {
41+
// 兼容旧上传接口
42+
y.storageConfig.NoOverwriteUpload = y.isFamily() && y.Addition.RapidUpload
43+
44+
// 处理个人云和家庭云参数
45+
if y.isFamily() && y.RootFolderID == "-11" {
46+
y.RootFolderID = ""
47+
}
48+
if !y.isFamily() && y.RootFolderID == "" {
49+
y.RootFolderID = "-11"
50+
}
51+
52+
// 限制上传线程数
53+
y.uploadThread, _ = strconv.Atoi(y.UploadThread)
54+
if y.uploadThread < 1 || y.uploadThread > 32 {
55+
y.uploadThread, y.UploadThread = 3, "3"
56+
}
57+
58+
// 初始化请求客户端
59+
if y.client == nil {
60+
y.client = base.NewRestyClient().SetHeaders(
61+
map[string]string{
62+
"Accept": "application/json;charset=UTF-8",
63+
"User-Agent": "EcloudTV/6.5.5 (PJX110; unknown; home02) Android/35",
64+
},
65+
)
66+
}
67+
68+
// 避免重复登陆
69+
if !y.isLogin() || y.Addition.AccessToken == "" {
70+
if err = y.login(); err != nil {
71+
return
72+
}
73+
}
74+
75+
// 处理家庭云ID
76+
if y.FamilyID == "" {
77+
if y.FamilyID, err = y.getFamilyID(); err != nil {
78+
return err
79+
}
80+
}
81+
82+
return
83+
}
84+
85+
func (y *Cloud189TV) Drop(ctx context.Context) error {
86+
return nil
87+
}
88+
89+
func (y *Cloud189TV) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
90+
return y.getFiles(ctx, dir.GetID(), y.isFamily())
91+
}
92+
93+
func (y *Cloud189TV) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
94+
var downloadUrl struct {
95+
URL string `json:"fileDownloadUrl"`
96+
}
97+
98+
isFamily := y.isFamily()
99+
fullUrl := ApiUrl
100+
if isFamily {
101+
fullUrl += "/family/file"
102+
}
103+
fullUrl += "/getFileDownloadUrl.action"
104+
105+
_, err := y.get(fullUrl, func(r *resty.Request) {
106+
r.SetContext(ctx)
107+
r.SetQueryParam("fileId", file.GetID())
108+
if isFamily {
109+
r.SetQueryParams(map[string]string{
110+
"familyId": y.FamilyID,
111+
})
112+
} else {
113+
r.SetQueryParams(map[string]string{
114+
"dt": "3",
115+
"flag": "1",
116+
})
117+
}
118+
}, &downloadUrl, isFamily)
119+
if err != nil {
120+
return nil, err
121+
}
122+
123+
// 重定向获取真实链接
124+
downloadUrl.URL = strings.Replace(strings.ReplaceAll(downloadUrl.URL, "&amp;", "&"), "http://", "https://", 1)
125+
res, err := base.NoRedirectClient.R().SetContext(ctx).SetDoNotParseResponse(true).Get(downloadUrl.URL)
126+
if err != nil {
127+
return nil, err
128+
}
129+
defer res.RawBody().Close()
130+
if res.StatusCode() == 302 {
131+
downloadUrl.URL = res.Header().Get("location")
132+
}
133+
134+
like := &model.Link{
135+
URL: downloadUrl.URL,
136+
Header: http.Header{
137+
"User-Agent": []string{base.UserAgent},
138+
},
139+
}
140+
141+
return like, nil
142+
}
143+
144+
func (y *Cloud189TV) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) {
145+
isFamily := y.isFamily()
146+
fullUrl := ApiUrl
147+
if isFamily {
148+
fullUrl += "/family/file"
149+
}
150+
fullUrl += "/createFolder.action"
151+
152+
var newFolder Cloud189Folder
153+
_, err := y.post(fullUrl, func(req *resty.Request) {
154+
req.SetContext(ctx)
155+
req.SetQueryParams(map[string]string{
156+
"folderName": dirName,
157+
"relativePath": "",
158+
})
159+
if isFamily {
160+
req.SetQueryParams(map[string]string{
161+
"familyId": y.FamilyID,
162+
"parentId": parentDir.GetID(),
163+
})
164+
} else {
165+
req.SetQueryParams(map[string]string{
166+
"parentFolderId": parentDir.GetID(),
167+
})
168+
}
169+
}, &newFolder, isFamily)
170+
if err != nil {
171+
return nil, err
172+
}
173+
return &newFolder, nil
174+
}
175+
176+
func (y *Cloud189TV) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
177+
isFamily := y.isFamily()
178+
other := map[string]string{"targetFileName": dstDir.GetName()}
179+
180+
resp, err := y.CreateBatchTask("MOVE", IF(isFamily, y.FamilyID, ""), dstDir.GetID(), other, BatchTaskInfo{
181+
FileId: srcObj.GetID(),
182+
FileName: srcObj.GetName(),
183+
IsFolder: BoolToNumber(srcObj.IsDir()),
184+
})
185+
if err != nil {
186+
return nil, err
187+
}
188+
if err = y.WaitBatchTask("MOVE", resp.TaskID, time.Millisecond*400); err != nil {
189+
return nil, err
190+
}
191+
return srcObj, nil
192+
}
193+
194+
func (y *Cloud189TV) Rename(ctx context.Context, srcObj model.Obj, newName string) (model.Obj, error) {
195+
isFamily := y.isFamily()
196+
queryParam := make(map[string]string)
197+
fullUrl := ApiUrl
198+
method := http.MethodPost
199+
if isFamily {
200+
fullUrl += "/family/file"
201+
method = http.MethodGet
202+
queryParam["familyId"] = y.FamilyID
203+
}
204+
205+
var newObj model.Obj
206+
switch f := srcObj.(type) {
207+
case *Cloud189File:
208+
fullUrl += "/renameFile.action"
209+
queryParam["fileId"] = srcObj.GetID()
210+
queryParam["destFileName"] = newName
211+
newObj = &Cloud189File{Icon: f.Icon} // 复用预览
212+
case *Cloud189Folder:
213+
fullUrl += "/renameFolder.action"
214+
queryParam["folderId"] = srcObj.GetID()
215+
queryParam["destFolderName"] = newName
216+
newObj = &Cloud189Folder{}
217+
default:
218+
return nil, errs.NotSupport
219+
}
220+
221+
_, err := y.request(fullUrl, method, func(req *resty.Request) {
222+
req.SetContext(ctx).SetQueryParams(queryParam)
223+
}, nil, newObj, isFamily)
224+
if err != nil {
225+
return nil, err
226+
}
227+
return newObj, nil
228+
}
229+
230+
func (y *Cloud189TV) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
231+
isFamily := y.isFamily()
232+
other := map[string]string{"targetFileName": dstDir.GetName()}
233+
234+
resp, err := y.CreateBatchTask("COPY", IF(isFamily, y.FamilyID, ""), dstDir.GetID(), other, BatchTaskInfo{
235+
FileId: srcObj.GetID(),
236+
FileName: srcObj.GetName(),
237+
IsFolder: BoolToNumber(srcObj.IsDir()),
238+
})
239+
240+
if err != nil {
241+
return err
242+
}
243+
return y.WaitBatchTask("COPY", resp.TaskID, time.Second)
244+
}
245+
246+
func (y *Cloud189TV) Remove(ctx context.Context, obj model.Obj) error {
247+
isFamily := y.isFamily()
248+
249+
resp, err := y.CreateBatchTask("DELETE", IF(isFamily, y.FamilyID, ""), "", nil, BatchTaskInfo{
250+
FileId: obj.GetID(),
251+
FileName: obj.GetName(),
252+
IsFolder: BoolToNumber(obj.IsDir()),
253+
})
254+
if err != nil {
255+
return err
256+
}
257+
// 批量任务数量限制,过快会导致无法删除
258+
return y.WaitBatchTask("DELETE", resp.TaskID, time.Millisecond*200)
259+
}
260+
261+
func (y *Cloud189TV) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) (newObj model.Obj, err error) {
262+
overwrite := true
263+
isFamily := y.isFamily()
264+
265+
// 响应时间长,按需启用
266+
if y.Addition.RapidUpload && !stream.IsForceStreamUpload() {
267+
if newObj, err := y.RapidUpload(ctx, dstDir, stream, isFamily, overwrite); err == nil {
268+
return newObj, nil
269+
}
270+
}
271+
272+
return y.OldUpload(ctx, dstDir, stream, up, isFamily, overwrite)
273+
274+
}

0 commit comments

Comments
 (0)