Skip to content

Commit 0483e0f

Browse files
RedSTAROj2rong4cn
andauthored
feat(driver_strm): also shown some files with strm (#969)
* feat(driver_strm): Also shown some files with strm Allow user set some file types that need to shown with strm, usually subtitles Most of code was copy and managed from drivers/alias * 优化 * 优化 * 。 * 添加注释 --------- Co-authored-by: j2rong4cn <[email protected]> Co-authored-by: j2rong4cn <[email protected]>
1 parent 08dae4f commit 0483e0f

File tree

4 files changed

+142
-99
lines changed

4 files changed

+142
-99
lines changed

drivers/strm/driver.go

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ package strm
33
import (
44
"context"
55
"errors"
6+
"fmt"
7+
stdpath "path"
68
"strings"
79

810
"github.com/OpenListTeam/OpenList/v4/internal/driver"
911
"github.com/OpenListTeam/OpenList/v4/internal/errs"
1012
"github.com/OpenListTeam/OpenList/v4/internal/fs"
1113
"github.com/OpenListTeam/OpenList/v4/internal/model"
14+
"github.com/OpenListTeam/OpenList/v4/internal/sign"
1215
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
16+
"github.com/OpenListTeam/OpenList/v4/server/common"
1317
)
1418

1519
type Strm struct {
@@ -18,6 +22,9 @@ type Strm struct {
1822
pathMap map[string][]string
1923
autoFlatten bool
2024
oneKey string
25+
26+
supportSuffix map[string]struct{}
27+
downloadSuffix map[string]struct{}
2128
}
2229

2330
func (d *Strm) Config() driver.Config {
@@ -51,12 +58,24 @@ func (d *Strm) Init(ctx context.Context) error {
5158
d.autoFlatten = false
5259
}
5360

61+
d.supportSuffix = supportSuffix()
5462
if d.FilterFileTypes != "" {
5563
types := strings.Split(d.FilterFileTypes, ",")
5664
for _, ext := range types {
5765
ext = strings.ToLower(strings.TrimSpace(ext))
5866
if ext != "" {
59-
supportSuffix[ext] = struct{}{}
67+
d.supportSuffix[ext] = struct{}{}
68+
}
69+
}
70+
}
71+
72+
d.downloadSuffix = downloadSuffix()
73+
if d.DownloadFileTypes != "" {
74+
downloadTypes := strings.Split(d.DownloadFileTypes, ",")
75+
for _, ext := range downloadTypes {
76+
ext = strings.ToLower(strings.TrimSpace(ext))
77+
if ext != "" {
78+
d.downloadSuffix[ext] = struct{}{}
6079
}
6180
}
6281
}
@@ -65,6 +84,8 @@ func (d *Strm) Init(ctx context.Context) error {
6584

6685
func (d *Strm) Drop(ctx context.Context) error {
6786
d.pathMap = nil
87+
d.downloadSuffix = nil
88+
d.supportSuffix = nil
6889
return nil
6990
}
7091

@@ -82,10 +103,25 @@ func (d *Strm) Get(ctx context.Context, path string) (model.Obj, error) {
82103
return nil, errs.ObjectNotFound
83104
}
84105
for _, dst := range dsts {
85-
obj, err := d.get(ctx, path, dst, sub)
86-
if err == nil {
87-
return obj, nil
106+
reqPath := stdpath.Join(dst, sub)
107+
obj, err := fs.Get(ctx, reqPath, &fs.GetArgs{NoLog: true})
108+
if err != nil {
109+
continue
88110
}
111+
// fs.Get 没报错,说明不是strm生成的路径,需要直接返回
112+
size := int64(0)
113+
if !obj.IsDir() {
114+
size = obj.GetSize()
115+
path = reqPath //把路径设置为真实的,供Link直接读取
116+
}
117+
return &model.Object{
118+
Path: path,
119+
Name: obj.GetName(),
120+
Size: size,
121+
Modified: obj.ModTime(),
122+
IsFolder: obj.IsDir(),
123+
HashInfo: obj.GetHash(),
124+
}, nil
89125
}
90126
return nil, errs.ObjectNotFound
91127
}
@@ -112,34 +148,33 @@ func (d *Strm) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]
112148
}
113149

114150
func (d *Strm) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
115-
link := d.getLink(ctx, file.GetPath())
116-
return &model.Link{
117-
MFile: strings.NewReader(link),
118-
}, nil
119-
}
120-
121-
func (d *Strm) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
122-
return errors.New("strm Driver cannot make dir")
123-
}
124-
125-
func (d *Strm) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
126-
return errors.New("strm Driver cannot move file")
127-
}
128-
129-
func (d *Strm) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
130-
return errors.New("strm Driver cannot rename file")
131-
}
132-
133-
func (d *Strm) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
134-
return errors.New("strm Driver cannot copy file")
135-
}
151+
if file.GetID() == "strm" {
152+
link := d.getLink(ctx, file.GetPath())
153+
return &model.Link{
154+
MFile: strings.NewReader(link),
155+
}, nil
156+
}
157+
// ftp,s3
158+
if common.GetApiUrl(ctx) == "" {
159+
args.Redirect = false
160+
}
161+
reqPath := file.GetPath()
162+
link, _, err := d.link(ctx, reqPath, args)
163+
if err != nil {
164+
return nil, err
165+
}
136166

137-
func (d *Strm) Remove(ctx context.Context, obj model.Obj) error {
138-
return errors.New("strm Driver cannot remove file")
139-
}
167+
if link == nil {
168+
return &model.Link{
169+
URL: fmt.Sprintf("%s/p%s?sign=%s",
170+
common.GetApiUrl(ctx),
171+
utils.EncodePath(reqPath, true),
172+
sign.Sign(reqPath)),
173+
}, nil
174+
}
140175

141-
func (d *Strm) Put(ctx context.Context, dstDir model.Obj, s model.FileStreamer, up driver.UpdateProgress) error {
142-
return errors.New("strm Driver cannot put file")
176+
// 没有修改link的字段,可直接返回
177+
return link, nil
143178
}
144179

145180
var _ driver.Driver = (*Strm)(nil)

drivers/strm/meta.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
)
77

88
type Addition struct {
9-
Paths string `json:"paths" required:"true" type:"text"`
10-
SiteUrl string `json:"siteUrl" type:"text" required:"false" help:"The prefix URL of the strm file"`
11-
FilterFileTypes string `json:"filterFileTypes" type:"text" default:"strm" required:"false" help:"Supports suffix name of strm file"`
12-
EncodePath bool `json:"encodePath" default:"true" required:"true" help:"encode the path in the strm file"`
13-
LocalModel bool `json:"localModel" default:"false" help:"enable local mode"`
9+
Paths string `json:"paths" required:"true" type:"text"`
10+
SiteUrl string `json:"siteUrl" type:"text" required:"false" help:"The prefix URL of the strm file"`
11+
FilterFileTypes string `json:"filterFileTypes" type:"text" default:"strm" required:"false" help:"Supports suffix name of strm file"`
12+
DownloadFileTypes string `json:"downloadFileTypes" type:"text" default:"ass" required:"false" help:"Files need to download with strm (usally subtitles)"`
13+
EncodePath bool `json:"encodePath" default:"true" required:"true" help:"encode the path in the strm file"`
14+
LocalModel bool `json:"localModel" default:"false" help:"enable local mode"`
1415
}
1516

1617
var config = driver.Config{

drivers/strm/types.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
package strm
22

3-
var supportSuffix = map[string]struct{}{
4-
// video
5-
"mp4": {},
6-
"mkv": {},
7-
"flv": {},
8-
"avi": {},
9-
"wmv": {},
10-
"ts": {},
11-
"rmvb": {},
12-
"webm": {},
13-
// audio
14-
"mp3": {},
15-
"flac": {},
16-
"aac": {},
17-
"wav": {},
18-
"ogg": {},
19-
"m4a": {},
20-
"wma": {},
21-
"alac": {},
3+
func supportSuffix() map[string]struct{} {
4+
return map[string]struct{}{
5+
// video
6+
"mp4": {},
7+
"mkv": {},
8+
"flv": {},
9+
"avi": {},
10+
"wmv": {},
11+
"ts": {},
12+
"rmvb": {},
13+
"webm": {},
14+
// audio
15+
"mp3": {},
16+
"flac": {},
17+
"aac": {},
18+
"wav": {},
19+
"ogg": {},
20+
"m4a": {},
21+
"wma": {},
22+
"alac": {},
23+
}
24+
}
25+
26+
func downloadSuffix() map[string]struct{} {
27+
return map[string]struct{}{
28+
// strm
29+
"strm": {},
30+
// subtitles
31+
"ass": {},
32+
"srt": {},
33+
"vtt": {},
34+
"sub": {},
35+
}
2236
}

drivers/strm/util.go

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/OpenListTeam/OpenList/v4/internal/fs"
1111
"github.com/OpenListTeam/OpenList/v4/internal/model"
12+
"github.com/OpenListTeam/OpenList/v4/internal/op"
1213
"github.com/OpenListTeam/OpenList/v4/internal/sign"
1314
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
1415
"github.com/OpenListTeam/OpenList/v4/server/common"
@@ -51,31 +52,6 @@ func (d *Strm) getRootAndPath(path string) (string, string) {
5152
return parts[0], parts[1]
5253
}
5354

54-
func (d *Strm) get(ctx context.Context, path string, dst, sub string) (model.Obj, error) {
55-
reqPath := stdpath.Join(dst, sub)
56-
obj, err := fs.Get(ctx, reqPath, &fs.GetArgs{NoLog: true})
57-
if err != nil {
58-
return nil, err
59-
}
60-
size := int64(0)
61-
if !obj.IsDir() {
62-
if utils.Ext(obj.GetName()) == "strm" {
63-
size = obj.GetSize()
64-
} else {
65-
file := stdpath.Join(reqPath, obj.GetName())
66-
size = int64(len(d.getLink(ctx, file)))
67-
}
68-
}
69-
return &model.Object{
70-
Path: path,
71-
Name: obj.GetName(),
72-
Size: size,
73-
Modified: obj.ModTime(),
74-
IsFolder: obj.IsDir(),
75-
HashInfo: obj.GetHash(),
76-
}, nil
77-
}
78-
7955
func (d *Strm) list(ctx context.Context, dst, sub string, args *fs.ListArgs) ([]model.Obj, error) {
8056
reqPath := stdpath.Join(dst, sub)
8157
objs, err := fs.List(ctx, reqPath, args)
@@ -85,45 +61,44 @@ func (d *Strm) list(ctx context.Context, dst, sub string, args *fs.ListArgs) ([]
8561

8662
var validObjs []model.Obj
8763
for _, obj := range objs {
88-
if !obj.IsDir() {
89-
ext := strings.ToLower(utils.Ext(obj.GetName()))
90-
if _, ok := supportSuffix[ext]; !ok {
91-
continue
92-
}
93-
}
94-
validObjs = append(validObjs, obj)
95-
}
96-
return utils.SliceConvert(validObjs, func(obj model.Obj) (model.Obj, error) {
97-
name := obj.GetName()
64+
id, name, path := "", obj.GetName(), ""
9865
size := int64(0)
9966
if !obj.IsDir() {
100-
ext := utils.Ext(name)
101-
name = strings.TrimSuffix(name, ext) + "strm"
102-
if ext == "strm" {
67+
path = stdpath.Join(reqPath, obj.GetName())
68+
ext := strings.ToLower(utils.Ext(name))
69+
if _, ok := d.supportSuffix[ext]; ok {
70+
id = "strm"
71+
name = strings.TrimSuffix(name, ext) + "strm"
72+
size = int64(len(d.getLink(ctx, path)))
73+
} else if _, ok := d.downloadSuffix[ext]; ok {
10374
size = obj.GetSize()
10475
} else {
105-
file := stdpath.Join(reqPath, obj.GetName())
106-
size = int64(len(d.getLink(ctx, file)))
76+
continue
10777
}
10878
}
10979
objRes := model.Object{
80+
ID: id,
81+
Path: path,
11082
Name: name,
11183
Size: size,
11284
Modified: obj.ModTime(),
11385
IsFolder: obj.IsDir(),
114-
Path: stdpath.Join(reqPath, obj.GetName()),
11586
}
87+
11688
thumb, ok := model.GetThumb(obj)
11789
if !ok {
118-
return &objRes, nil
90+
validObjs = append(validObjs, &objRes)
91+
continue
11992
}
120-
return &model.ObjThumb{
93+
94+
validObjs = append(validObjs, &model.ObjThumb{
12195
Object: objRes,
12296
Thumbnail: model.Thumbnail{
12397
Thumbnail: thumb,
12498
},
125-
}, nil
126-
})
99+
})
100+
}
101+
return validObjs, nil
127102
}
128103

129104
func (d *Strm) getLink(ctx context.Context, path string) string {
@@ -149,3 +124,21 @@ func (d *Strm) getLink(ctx context.Context, path string) string {
149124
apiUrl,
150125
finalPath)
151126
}
127+
128+
func (d *Strm) link(ctx context.Context, reqPath string, args model.LinkArgs) (*model.Link, model.Obj, error) {
129+
storage, reqActualPath, err := op.GetStorageAndActualPath(reqPath)
130+
if err != nil {
131+
return nil, nil, err
132+
}
133+
if !args.Redirect {
134+
return op.Link(ctx, storage, reqActualPath, args)
135+
}
136+
obj, err := fs.Get(ctx, reqPath, &fs.GetArgs{NoLog: true})
137+
if err != nil {
138+
return nil, nil, err
139+
}
140+
if common.ShouldProxy(storage, stdpath.Base(reqPath)) {
141+
return nil, obj, nil
142+
}
143+
return op.Link(ctx, storage, reqActualPath, args)
144+
}

0 commit comments

Comments
 (0)