Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/doubao/driver.go
1986 views
1
package doubao
2
3
import (
4
"context"
5
"errors"
6
"net/http"
7
"strconv"
8
"strings"
9
"time"
10
11
"github.com/alist-org/alist/v3/drivers/base"
12
"github.com/alist-org/alist/v3/internal/driver"
13
"github.com/alist-org/alist/v3/internal/errs"
14
"github.com/alist-org/alist/v3/internal/model"
15
"github.com/alist-org/alist/v3/pkg/utils"
16
"github.com/go-resty/resty/v2"
17
"github.com/google/uuid"
18
)
19
20
type Doubao struct {
21
model.Storage
22
Addition
23
*UploadToken
24
UserId string
25
uploadThread int
26
}
27
28
func (d *Doubao) Config() driver.Config {
29
return config
30
}
31
32
func (d *Doubao) GetAddition() driver.Additional {
33
return &d.Addition
34
}
35
36
func (d *Doubao) Init(ctx context.Context) error {
37
// TODO login / refresh token
38
//op.MustSaveDriverStorage(d)
39
uploadThread, err := strconv.Atoi(d.UploadThread)
40
if err != nil || uploadThread < 1 {
41
d.uploadThread, d.UploadThread = 3, "3" // Set default value
42
} else {
43
d.uploadThread = uploadThread
44
}
45
46
if d.UserId == "" {
47
userInfo, err := d.getUserInfo()
48
if err != nil {
49
return err
50
}
51
52
d.UserId = strconv.FormatInt(userInfo.UserID, 10)
53
}
54
55
if d.UploadToken == nil {
56
uploadToken, err := d.initUploadToken()
57
if err != nil {
58
return err
59
}
60
61
d.UploadToken = uploadToken
62
}
63
64
return nil
65
}
66
67
func (d *Doubao) Drop(ctx context.Context) error {
68
return nil
69
}
70
71
func (d *Doubao) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
72
var files []model.Obj
73
fileList, err := d.getFiles(dir.GetID(), "")
74
if err != nil {
75
return nil, err
76
}
77
78
for _, child := range fileList {
79
files = append(files, &Object{
80
Object: model.Object{
81
ID: child.ID,
82
Path: child.ParentID,
83
Name: child.Name,
84
Size: child.Size,
85
Modified: time.Unix(child.UpdateTime, 0),
86
Ctime: time.Unix(child.CreateTime, 0),
87
IsFolder: child.NodeType == 1,
88
},
89
Key: child.Key,
90
NodeType: child.NodeType,
91
})
92
}
93
94
return files, nil
95
}
96
97
func (d *Doubao) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
98
var downloadUrl string
99
100
if u, ok := file.(*Object); ok {
101
switch d.DownloadApi {
102
case "get_download_info":
103
var r GetDownloadInfoResp
104
_, err := d.request("/samantha/aispace/get_download_info", http.MethodPost, func(req *resty.Request) {
105
req.SetBody(base.Json{
106
"requests": []base.Json{{"node_id": file.GetID()}},
107
})
108
}, &r)
109
if err != nil {
110
return nil, err
111
}
112
113
downloadUrl = r.Data.DownloadInfos[0].MainURL
114
case "get_file_url":
115
switch u.NodeType {
116
case VideoType, AudioType:
117
var r GetVideoFileUrlResp
118
_, err := d.request("/samantha/media/get_play_info", http.MethodPost, func(req *resty.Request) {
119
req.SetBody(base.Json{
120
"key": u.Key,
121
"node_id": file.GetID(),
122
})
123
}, &r)
124
if err != nil {
125
return nil, err
126
}
127
128
downloadUrl = r.Data.OriginalMediaInfo.MainURL
129
default:
130
var r GetFileUrlResp
131
_, err := d.request("/alice/message/get_file_url", http.MethodPost, func(req *resty.Request) {
132
req.SetBody(base.Json{
133
"uris": []string{u.Key},
134
"type": FileNodeType[u.NodeType],
135
})
136
}, &r)
137
if err != nil {
138
return nil, err
139
}
140
141
downloadUrl = r.Data.FileUrls[0].MainURL
142
}
143
default:
144
return nil, errs.NotImplement
145
}
146
147
// 生成标准的Content-Disposition
148
contentDisposition := generateContentDisposition(u.Name)
149
150
return &model.Link{
151
URL: downloadUrl,
152
Header: http.Header{
153
"User-Agent": []string{UserAgent},
154
"Content-Disposition": []string{contentDisposition},
155
},
156
}, nil
157
}
158
159
return nil, errors.New("can't convert obj to URL")
160
}
161
162
func (d *Doubao) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
163
var r UploadNodeResp
164
_, err := d.request("/samantha/aispace/upload_node", http.MethodPost, func(req *resty.Request) {
165
req.SetBody(base.Json{
166
"node_list": []base.Json{
167
{
168
"local_id": uuid.New().String(),
169
"name": dirName,
170
"parent_id": parentDir.GetID(),
171
"node_type": 1,
172
},
173
},
174
})
175
}, &r)
176
return err
177
}
178
179
func (d *Doubao) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
180
var r UploadNodeResp
181
_, err := d.request("/samantha/aispace/move_node", http.MethodPost, func(req *resty.Request) {
182
req.SetBody(base.Json{
183
"node_list": []base.Json{
184
{"id": srcObj.GetID()},
185
},
186
"current_parent_id": srcObj.GetPath(),
187
"target_parent_id": dstDir.GetID(),
188
})
189
}, &r)
190
return err
191
}
192
193
func (d *Doubao) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
194
var r BaseResp
195
_, err := d.request("/samantha/aispace/rename_node", http.MethodPost, func(req *resty.Request) {
196
req.SetBody(base.Json{
197
"node_id": srcObj.GetID(),
198
"node_name": newName,
199
})
200
}, &r)
201
return err
202
}
203
204
func (d *Doubao) Copy(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
205
// TODO copy obj, optional
206
return nil, errs.NotImplement
207
}
208
209
func (d *Doubao) Remove(ctx context.Context, obj model.Obj) error {
210
var r BaseResp
211
_, err := d.request("/samantha/aispace/delete_node", http.MethodPost, func(req *resty.Request) {
212
req.SetBody(base.Json{"node_list": []base.Json{{"id": obj.GetID()}}})
213
}, &r)
214
return err
215
}
216
217
func (d *Doubao) Put(ctx context.Context, dstDir model.Obj, file model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) {
218
// 根据MIME类型确定数据类型
219
mimetype := file.GetMimetype()
220
dataType := FileDataType
221
222
switch {
223
case strings.HasPrefix(mimetype, "video/"):
224
dataType = VideoDataType
225
case strings.HasPrefix(mimetype, "audio/"):
226
dataType = VideoDataType // 音频与视频使用相同的处理方式
227
case strings.HasPrefix(mimetype, "image/"):
228
dataType = ImgDataType
229
}
230
231
// 获取上传配置
232
uploadConfig := UploadConfig{}
233
if err := d.getUploadConfig(&uploadConfig, dataType, file); err != nil {
234
return nil, err
235
}
236
237
// 根据文件大小选择上传方式
238
if file.GetSize() <= 1*utils.MB { // 小于1MB,使用普通模式上传
239
return d.Upload(&uploadConfig, dstDir, file, up, dataType)
240
}
241
// 大文件使用分片上传
242
return d.UploadByMultipart(ctx, &uploadConfig, file.GetSize(), dstDir, file, up, dataType)
243
}
244
245
func (d *Doubao) GetArchiveMeta(ctx context.Context, obj model.Obj, args model.ArchiveArgs) (model.ArchiveMeta, error) {
246
// TODO get archive file meta-info, return errs.NotImplement to use an internal archive tool, optional
247
return nil, errs.NotImplement
248
}
249
250
func (d *Doubao) ListArchive(ctx context.Context, obj model.Obj, args model.ArchiveInnerArgs) ([]model.Obj, error) {
251
// TODO list args.InnerPath in the archive obj, return errs.NotImplement to use an internal archive tool, optional
252
return nil, errs.NotImplement
253
}
254
255
func (d *Doubao) Extract(ctx context.Context, obj model.Obj, args model.ArchiveInnerArgs) (*model.Link, error) {
256
// TODO return link of file args.InnerPath in the archive obj, return errs.NotImplement to use an internal archive tool, optional
257
return nil, errs.NotImplement
258
}
259
260
func (d *Doubao) ArchiveDecompress(ctx context.Context, srcObj, dstDir model.Obj, args model.ArchiveDecompressArgs) ([]model.Obj, error) {
261
// TODO extract args.InnerPath path in the archive srcObj to the dstDir location, optional
262
// a folder with the same name as the archive file needs to be created to store the extracted results if args.PutIntoNewDir
263
// return errs.NotImplement to use an internal archive tool
264
return nil, errs.NotImplement
265
}
266
267
//func (d *Doubao) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
268
// return nil, errs.NotSupport
269
//}
270
271
var _ driver.Driver = (*Doubao)(nil)
272
273