Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/doubao_share/driver.go
1986 views
1
package doubao_share
2
3
import (
4
"context"
5
"errors"
6
"github.com/alist-org/alist/v3/drivers/base"
7
"github.com/alist-org/alist/v3/internal/driver"
8
"github.com/alist-org/alist/v3/internal/errs"
9
"github.com/alist-org/alist/v3/internal/model"
10
"github.com/go-resty/resty/v2"
11
"net/http"
12
)
13
14
type DoubaoShare struct {
15
model.Storage
16
Addition
17
RootFiles []RootFileList
18
}
19
20
func (d *DoubaoShare) Config() driver.Config {
21
return config
22
}
23
24
func (d *DoubaoShare) GetAddition() driver.Additional {
25
return &d.Addition
26
}
27
28
func (d *DoubaoShare) Init(ctx context.Context) error {
29
// 初始化 虚拟分享列表
30
if err := d.initShareList(); err != nil {
31
return err
32
}
33
34
return nil
35
}
36
37
func (d *DoubaoShare) Drop(ctx context.Context) error {
38
return nil
39
}
40
41
func (d *DoubaoShare) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
42
// 检查是否为根目录
43
if dir.GetID() == "" && dir.GetPath() == "/" {
44
return d.listRootDirectory(ctx)
45
}
46
47
// 非根目录,处理不同情况
48
if fo, ok := dir.(*FileObject); ok {
49
if fo.ShareID == "" {
50
// 虚拟目录,需要列出子目录
51
return d.listVirtualDirectoryContent(dir)
52
} else {
53
// 具有分享ID的目录,获取此分享下的文件
54
shareId, relativePath, err := d._findShareAndPath(dir)
55
if err != nil {
56
return nil, err
57
}
58
return d.getFilesInPath(ctx, shareId, dir.GetID(), relativePath)
59
}
60
}
61
62
// 使用通用方法
63
shareId, relativePath, err := d._findShareAndPath(dir)
64
if err != nil {
65
return nil, err
66
}
67
68
// 获取指定路径下的文件
69
return d.getFilesInPath(ctx, shareId, dir.GetID(), relativePath)
70
}
71
72
func (d *DoubaoShare) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
73
var downloadUrl string
74
75
if u, ok := file.(*FileObject); ok {
76
switch u.NodeType {
77
case VideoType, AudioType:
78
var r GetVideoFileUrlResp
79
_, err := d.request("/samantha/media/get_play_info", http.MethodPost, func(req *resty.Request) {
80
req.SetBody(base.Json{
81
"key": u.Key,
82
"share_id": u.ShareID,
83
"node_id": file.GetID(),
84
})
85
}, &r)
86
if err != nil {
87
return nil, err
88
}
89
90
downloadUrl = r.Data.OriginalMediaInfo.MainURL
91
default:
92
var r GetFileUrlResp
93
_, err := d.request("/alice/message/get_file_url", http.MethodPost, func(req *resty.Request) {
94
req.SetBody(base.Json{
95
"uris": []string{u.Key},
96
"type": FileNodeType[u.NodeType],
97
})
98
}, &r)
99
if err != nil {
100
return nil, err
101
}
102
103
downloadUrl = r.Data.FileUrls[0].MainURL
104
}
105
106
// 生成标准的Content-Disposition
107
contentDisposition := generateContentDisposition(u.Name)
108
109
return &model.Link{
110
URL: downloadUrl,
111
Header: http.Header{
112
"User-Agent": []string{UserAgent},
113
"Content-Disposition": []string{contentDisposition},
114
},
115
}, nil
116
}
117
118
return nil, errors.New("can't convert obj to URL")
119
}
120
121
func (d *DoubaoShare) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) {
122
// TODO create folder, optional
123
return nil, errs.NotImplement
124
}
125
126
func (d *DoubaoShare) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
127
// TODO move obj, optional
128
return nil, errs.NotImplement
129
}
130
131
func (d *DoubaoShare) Rename(ctx context.Context, srcObj model.Obj, newName string) (model.Obj, error) {
132
// TODO rename obj, optional
133
return nil, errs.NotImplement
134
}
135
136
func (d *DoubaoShare) Copy(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
137
// TODO copy obj, optional
138
return nil, errs.NotImplement
139
}
140
141
func (d *DoubaoShare) Remove(ctx context.Context, obj model.Obj) error {
142
// TODO remove obj, optional
143
return errs.NotImplement
144
}
145
146
func (d *DoubaoShare) Put(ctx context.Context, dstDir model.Obj, file model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) {
147
// TODO upload file, optional
148
return nil, errs.NotImplement
149
}
150
151
func (d *DoubaoShare) GetArchiveMeta(ctx context.Context, obj model.Obj, args model.ArchiveArgs) (model.ArchiveMeta, error) {
152
// TODO get archive file meta-info, return errs.NotImplement to use an internal archive tool, optional
153
return nil, errs.NotImplement
154
}
155
156
func (d *DoubaoShare) ListArchive(ctx context.Context, obj model.Obj, args model.ArchiveInnerArgs) ([]model.Obj, error) {
157
// TODO list args.InnerPath in the archive obj, return errs.NotImplement to use an internal archive tool, optional
158
return nil, errs.NotImplement
159
}
160
161
func (d *DoubaoShare) Extract(ctx context.Context, obj model.Obj, args model.ArchiveInnerArgs) (*model.Link, error) {
162
// TODO return link of file args.InnerPath in the archive obj, return errs.NotImplement to use an internal archive tool, optional
163
return nil, errs.NotImplement
164
}
165
166
func (d *DoubaoShare) ArchiveDecompress(ctx context.Context, srcObj, dstDir model.Obj, args model.ArchiveDecompressArgs) ([]model.Obj, error) {
167
// TODO extract args.InnerPath path in the archive srcObj to the dstDir location, optional
168
// a folder with the same name as the archive file needs to be created to store the extracted results if args.PutIntoNewDir
169
// return errs.NotImplement to use an internal archive tool
170
return nil, errs.NotImplement
171
}
172
173
//func (d *DoubaoShare) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
174
// return nil, errs.NotSupport
175
//}
176
177
var _ driver.Driver = (*DoubaoShare)(nil)
178
179