Skip to content

Commit

Permalink
feat(drivers): alias a new storage with multi path (close #3248)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Mar 13, 2023
1 parent 3b2703a commit 301756b
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 16 deletions.
106 changes: 106 additions & 0 deletions drivers/alias/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package alias

import (
"context"
"errors"
"strings"

"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
)

type Alias struct {
model.Storage
Addition
pathMap map[string][]string
}

func (d *Alias) Config() driver.Config {
return config
}

func (d *Alias) GetAddition() driver.Additional {
return &d.Addition
}

func (d *Alias) Init(ctx context.Context) error {
if d.Paths == "" {
return errors.New("paths is required")
}
d.pathMap = make(map[string][]string)
for _, path := range strings.Split(d.Paths, "\n") {
path = strings.TrimSpace(path)
if path == "" {
continue
}
k, v := getPair(path)
d.pathMap[k] = append(d.pathMap[k], v)
}
return nil
}

func (d *Alias) Drop(ctx context.Context) error {
d.pathMap = nil
return nil
}

func (d *Alias) Get(ctx context.Context, path string) (model.Obj, error) {
if utils.PathEqual(path, "/") {
return &model.Object{
Name: "Root",
IsFolder: true,
Path: "/",
}, nil
}
root, sub := getRootAndPath(path)
dsts, ok := d.pathMap[root]
if !ok {
return nil, errs.ObjectNotFound
}
for _, dst := range dsts {
obj, err := d.get(ctx, path, dst, sub)
if err == nil {
return obj, nil
}
}
return nil, errs.ObjectNotFound
}

func (d *Alias) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
path := dir.GetPath()
if utils.PathEqual(path, "/") {
return d.listRoot(), nil
}
root, sub := getRootAndPath(path)
dsts, ok := d.pathMap[root]
if !ok {
return nil, errs.ObjectNotFound
}
var objs []model.Obj
for _, dst := range dsts {
tmp, err := d.list(ctx, dst, sub)
if err == nil {
objs = append(objs, tmp...)
}
}
return objs, nil
}

func (d *Alias) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
root, sub := getRootAndPath(file.GetPath())
dsts, ok := d.pathMap[root]
if !ok {
return nil, errs.ObjectNotFound
}
for _, dst := range dsts {
link, err := d.link(ctx, dst, sub, args)
if err == nil {
return link, nil
}
}
return nil, errs.ObjectNotFound
}

var _ driver.Driver = (*Alias)(nil)
27 changes: 27 additions & 0 deletions drivers/alias/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package alias

import (
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/op"
)

type Addition struct {
// Usually one of two
// driver.RootPath
// define other
Paths string `json:"paths" required:"true" type:"text"`

This comment has been minimized.

Copy link
@anwen-anyi

anwen-anyi Mar 13, 2023

Contributor

可以添加一个help提示302的不要和本地代理的混合在一起不然 本地代理的无法播放QAQ

}

var config = driver.Config{
Name: "Alias",
LocalSort: true,
NoCache: true,
NoUpload: true,
DefaultRoot: "/",
}

func init() {
op.RegisterDriver(func() driver.Driver {
return &Alias{}
})
}
1 change: 1 addition & 0 deletions drivers/alias/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package alias
81 changes: 81 additions & 0 deletions drivers/alias/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package alias

import (
"context"
stdpath "path"
"strings"

"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
)

func (d *Alias) listRoot() []model.Obj {
var objs []model.Obj
for k, _ := range d.pathMap {
obj := model.Object{
Name: k,
IsFolder: true,
Modified: d.Modified,
}
objs = append(objs, &obj)
}
return objs
}

// do others that not defined in Driver interface
func getPair(path string) (string, string) {
//path = strings.TrimSpace(path)
if strings.Contains(path, ":") {
pair := strings.SplitN(path, ":", 2)
if !strings.Contains(pair[0], "/") {
return pair[0], pair[1]
}
}
return stdpath.Base(path), path
}

func getRootAndPath(path string) (string, string) {
path = strings.TrimPrefix(path, "/")
parts := strings.SplitN(path, "/", 2)
if len(parts) == 1 {
return parts[0], ""
}
return parts[0], parts[1]
}

func (d *Alias) get(ctx context.Context, path string, dst, sub string) (model.Obj, error) {
obj, err := fs.Get(ctx, stdpath.Join(dst, sub))
if err != nil {
return nil, err
}
return &model.Object{
Path: path,
Name: obj.GetName(),
Size: obj.GetSize(),
Modified: obj.ModTime(),
IsFolder: obj.IsDir(),
}, nil
}

func (d *Alias) list(ctx context.Context, dst, sub string) ([]model.Obj, error) {
objs, err := fs.List(ctx, stdpath.Join(dst, sub))
// the obj must implement the model.SetPath interface
// return objs, err
if err != nil {
return nil, err
}
return utils.SliceConvert(objs, func(obj model.Obj) (model.Obj, error) {
return &model.Object{
Name: obj.GetName(),
Size: obj.GetSize(),
Modified: obj.ModTime(),
IsFolder: obj.IsDir(),
}, nil
})
}

func (d *Alias) link(ctx context.Context, dst, sub string, args model.LinkArgs) (*model.Link, error) {
link, _, err := fs.Link(ctx, stdpath.Join(dst, sub), args)
return link, err
}
1 change: 1 addition & 0 deletions drivers/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
_ "github.com/alist-org/alist/v3/drivers/139"
_ "github.com/alist-org/alist/v3/drivers/189"
_ "github.com/alist-org/alist/v3/drivers/189pc"
_ "github.com/alist-org/alist/v3/drivers/alias"
_ "github.com/alist-org/alist/v3/drivers/alist_v2"
_ "github.com/alist-org/alist/v3/drivers/alist_v3"
_ "github.com/alist-org/alist/v3/drivers/aliyundrive"
Expand Down
16 changes: 8 additions & 8 deletions drivers/template/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,42 @@ func (d *Template) Drop(ctx context.Context) error {
}

func (d *Template) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
// TODO return the files list
// TODO return the files list, required
return nil, errs.NotImplement
}

func (d *Template) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
// TODO return link of file
// TODO return link of file, required
return nil, errs.NotImplement
}

func (d *Template) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
// TODO create folder
// TODO create folder, optional
return errs.NotImplement
}

func (d *Template) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
// TODO move obj
// TODO move obj, optional
return errs.NotImplement
}

func (d *Template) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
// TODO rename obj
// TODO rename obj, optional
return errs.NotImplement
}

func (d *Template) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
// TODO copy obj
// TODO copy obj, optional
return errs.NotImplement
}

func (d *Template) Remove(ctx context.Context, obj model.Obj) error {
// TODO remove obj
// TODO remove obj, optional
return errs.NotImplement
}

func (d *Template) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
// TODO upload file
// TODO upload file, optional
return errs.NotImplement
}

Expand Down
19 changes: 11 additions & 8 deletions drivers/template/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ type Addition struct {
}

var config = driver.Config{
Name: "Template",
LocalSort: false,
OnlyLocal: false,
OnlyProxy: false,
NoCache: false,
NoUpload: false,
NeedMs: false,
DefaultRoot: "root, / or other",
Name: "Template",
LocalSort: false,
OnlyLocal: false,
OnlyProxy: false,
NoCache: false,
NoUpload: false,
NeedMs: false,
DefaultRoot: "root, / or other",
CheckStatus: false,
Alert: "",
NoOverwriteUpload: false,
}

func init() {
Expand Down

4 comments on commit 301756b

@anwen-anyi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不知道是否写完啦,现在经过测试 有几个疑问~

一、
现在似乎是只支持 根文件夹里面的二级文件夹
例如将两个 根文件夹路径写进去出现的还是两个没有合并的,

#No            #OK
Paths          Paths
/本地1         /本地1/本地
/本地2         /本地2/本地
#No            #OK
/本地1         本地
/本地2         

反正最后出现的文件夹名字一样就可以(最后结尾也不能是挂载时候显示的根文件夹)

就算是使用虚拟路径也不行QAQ 好像是还是会检测到是根文件夹 例如下面这样

#输入
/本地测试/本地1
/本地测试/本地2

#输出
/本地1
/本地2

二、
不同策略的方式不能组合在一起,否则无法播放,
例如:

  1. 默认302代理的阿里云盘和默认夸克云盘相同文件合并在一起了,在创建 alias 存储没有勾选web代理(302播放)
    • 阿里云盘的可以播放,夸克的不可以
  2. 默认302代理的阿里云盘和默认夸克云盘相同文件合并在一起了,在创建 alias 存储勾选web代理(本地代理播放)
    • 阿里可以播放,夸克也可以播放

302的优先权 大于 本地代理和代理URL链接 例如例2那样(查了一下F12会自动中转)

三、
如果是出现一样的文件合并后的
点击下载的时候调用是否是像负载均衡那样轮询的还是固定的QAQ

@xhofe
Copy link
Collaborator Author

@xhofe xhofe commented on 301756b Mar 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不知道是否写完啦,现在经过测试 有几个疑问~

一、 现在似乎是只支持 根文件夹里面的二级文件夹 例如将两个 根文件夹路径写进去出现的还是两个没有合并的,

#No            #OK
Paths          Paths
/本地1         /本地1/本地
/本地2         /本地2/本地
#No            #OK
/本地1         本地
/本地2         

反正最后出现的文件夹名字一样就可以(最后结尾也不能是挂载时候显示的根文件夹)

就算是使用虚拟路径也不行QAQ 好像是还是会检测到是根文件夹 例如下面这样

#输入
/本地测试/本地1
/本地测试/本地2

#输出
/本地1
/本地2

二、 不同策略的方式不能组合在一起,否则无法播放, 例如:

  1. 默认302代理的阿里云盘和默认夸克云盘相同文件合并在一起了,在创建 alias 存储没有勾选web代理(302播放)

    • 阿里云盘的可以播放,夸克的不可以
  2. 默认302代理的阿里云盘和默认夸克云盘相同文件合并在一起了,在创建 alias 存储勾选web代理(本地代理播放)

    • 阿里可以播放,夸克也可以播放

302的优先权 大于 本地代理和代理URL链接 例如例2那样(查了一下F12会自动中转)

三、 如果是出现一样的文件合并后的 点击下载的时候调用是否是像负载均衡那样轮询的还是固定的QAQ

  1. 你可以对路径重新命名
本地:/本地测试/本地1
本地:/本地测试/本地2

即可合并
2. 这个应该可以被修复,之后会修
3. 是固定的,取最前面的

@anwen-anyi
Copy link
Contributor

@anwen-anyi anwen-anyi commented on 301756b Mar 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一、
按照重新命名方式命名后 在 首页会出现一个新的
New

二、固定的也就是说
例1、 根据下面路径填写访问

  • 1,一个视频 1 2 3 4 都有的话每次访问的是 1
  • 2,一个视频 3 4 有 那么每次访问的都是3
本地:/本地测试/本地1
本地:/本地测试/本地2
本地:/本地测试/本地3
本地:/本地测试/本地4

例2、根据下面的路径填写访问

  • 1,一个视频 1 2 3 4 都有的话每次访问的是 4
  • 2,一个视频 3 4 有 那么每次访问的都是4
本地:/本地测试/本地4
本地:/本地测试/本地3
本地:/本地测试/本地2
本地:/本地测试/本地1

@anwen-anyi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

呃呃呃.... 抱歉 😭 😭 😭
我忘记了 哪个 在首页会出现一个新的 是我的锅
我忘记了我用虚拟路径的方式添加了一下.....

Please sign in to comment.