Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drivers/115_open/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (d *Open115) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
}, nil
}

func (d *Open115) GetObjInfo(ctx context.Context, path string) (model.Obj, error) {
func (d *Open115) Get(ctx context.Context, path string) (model.Obj, error) {
if err := d.WaitLimit(ctx); err != nil {
return nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions drivers/alias/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ func (d *Alias) Drop(ctx context.Context) error {
return nil
}

func (d *Alias) GetRoot(ctx context.Context) (model.Obj, error) {
return &model.Object{
Name: "Root",
IsFolder: true,
Path: "/",
}, 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 := d.getRootAndPath(path)
dsts, ok := d.pathMap[root]
if !ok {
Expand Down
49 changes: 49 additions & 0 deletions drivers/aliyundrive_open/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"errors"
"net/http"
stdpath "path"
"path/filepath"
"slices"
"time"

"github.com/OpenListTeam/OpenList/v4/drivers/base"
Expand Down Expand Up @@ -52,6 +54,34 @@ func (d *AliyundriveOpen) Init(ctx context.Context) error {
userid := utils.Json.Get(res, "user_id").ToString()
d.limiter.free()
d.limiter = getLimiterForUser(userid) // Allocate a corresponding limiter for each user.

d.buildRootPath(ctx)
return nil
}

func (d *AliyundriveOpen) buildRootPath(ctx context.Context) error {
d.RootPath = "/"
if d.RootFolderID == "root" {
return nil
}
currentFileId := d.RootFolderID
var pathSegments []string
for currentFileId != "root" {
var resp File
_, err := d.request(ctx, limiterOther, "/adrive/v1.0/openFile/get", http.MethodPost, func(req *resty.Request) {
req.SetBody(base.Json{
"drive_id": d.DriveId,
"file_id": currentFileId,
}).SetResult(&resp)
})
if err != nil {
return err
}
pathSegments = append(pathSegments, resp.Name)
currentFileId = resp.ParentFileId
}
slices.Reverse(pathSegments)
d.RootPath = stdpath.Join(d.RootPath, stdpath.Join(pathSegments...))
return nil
}

Expand Down Expand Up @@ -83,6 +113,25 @@ func (d *AliyundriveOpen) GetRoot(ctx context.Context) (model.Obj, error) {
}, nil
}

func (d *AliyundriveOpen) Get(ctx context.Context, path string) (model.Obj, error) {
if d.RootPath != "/" {
path = stdpath.Join(d.RootPath, path)
}
var resp File
_, err := d.request(ctx, limiterOther, "/adrive/v1.0/openFile/get_by_path", http.MethodPost, func(req *resty.Request) {
req.SetBody(base.Json{
"drive_id": d.DriveId,
"file_path": path,
}).SetResult(&resp)
})
if err != nil {
return nil, err
}
obj := fileToObj(resp)
obj.Path = path
return obj, nil
}

func (d *AliyundriveOpen) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
files, err := d.getFiles(ctx, dir.GetID())
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions drivers/aliyundrive_open/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Addition struct {
InternalUpload bool `json:"internal_upload" help:"If you are using Aliyun ECS is located in Beijing, you can turn it on to boost the upload speed"`
LIVPDownloadFormat string `json:"livp_download_format" type:"select" options:"jpeg,mov" default:"jpeg"`
AccessToken string
RootPath string
}

var config = driver.Config{
Expand Down
15 changes: 8 additions & 7 deletions drivers/chunk/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ func (d *Chunk) Drop(ctx context.Context) error {
return nil
}

func (d *Chunk) GetRoot(ctx context.Context) (model.Obj, error) {
return &model.Object{
Name: "Root",
IsFolder: true,
Path: "/",
}, nil
}

func (d *Chunk) Get(ctx context.Context, path string) (model.Obj, error) {
if utils.PathEqual(path, "/") {
return &model.Object{
Name: "Root",
IsFolder: true,
Path: "/",
}, nil
}
remoteStorage, remoteActualPath, err := op.GetStorageAndActualPath(d.RemotePath)
if err != nil {
return nil, err
Expand Down
15 changes: 8 additions & 7 deletions drivers/crypt/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,15 @@ func (d *Crypt) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([
return result, nil
}

func (d *Crypt) GetRoot(ctx context.Context) (model.Obj, error) {
return &model.Object{
Name: "Root",
IsFolder: true,
Path: "/",
}, nil
}

func (d *Crypt) Get(ctx context.Context, path string) (model.Obj, error) {
if utils.PathEqual(path, "/") {
return &model.Object{
Name: "Root",
IsFolder: true,
Path: "/",
}, nil
}
remoteFullPath := ""
var remoteObj model.Obj
var err, err2 error
Expand Down
14 changes: 7 additions & 7 deletions drivers/netease_music/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func (d *NeteaseMusic) Drop(ctx context.Context) error {
return nil
}

func (d *NeteaseMusic) Get(ctx context.Context, path string) (model.Obj, error) {
if path == "/" {
return &model.Object{
IsFolder: true,
Path: path,
}, nil
}
func (d *NeteaseMusic) GetRoot(ctx context.Context) (model.Obj, error) {
return &model.Object{
IsFolder: true,
Path: "/",
}, nil
}

func (d *NeteaseMusic) Get(ctx context.Context, path string) (model.Obj, error) {
fragments := strings.Split(path, "/")
if len(fragments) > 1 {
fileName := fragments[1]
Expand Down
15 changes: 8 additions & 7 deletions drivers/strm/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ func (d *Strm) Drop(ctx context.Context) error {
return nil
}

func (d *Strm) GetRoot(ctx context.Context) (model.Obj, error) {
return &model.Object{
Name: "Root",
IsFolder: true,
Path: "/",
}, nil
}

func (d *Strm) Get(ctx context.Context, path string) (model.Obj, error) {
if utils.PathEqual(path, "/") {
return &model.Object{
Name: "Root",
IsFolder: true,
Path: "/",
}, nil
}
root, sub := d.getRootAndPath(path)
dsts, ok := d.pathMap[root]
if !ok {
Expand Down
5 changes: 0 additions & 5 deletions internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ type Getter interface {
Get(ctx context.Context, path string) (model.Obj, error)
}

type GetObjInfo interface {
// GetObjInfo get file info by path
GetObjInfo(ctx context.Context, path string) (model.Obj, error)
}

//type Writer interface {
// Mkdir
// Move
Expand Down
64 changes: 26 additions & 38 deletions internal/op/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
stderrors "errors"
stdpath "path"
"slices"
"strings"
"time"

"github.com/OpenListTeam/OpenList/v4/internal/driver"
Expand Down Expand Up @@ -178,17 +177,6 @@ func Get(ctx context.Context, storage driver.Driver, path string) (model.Obj, er
path = utils.FixAndCleanPath(path)
log.Debugf("op.Get %s", path)

// get the obj directly without list so that we can reduce the io
if g, ok := storage.(driver.Getter); ok {
obj, err := g.Get(ctx, path)
if err == nil {
return model.WrapObjName(obj), nil
}
if !errs.IsNotImplementError(err) && !errs.IsNotSupportError(err) {
return nil, errors.WithMessage(err, "failed to get obj")
}
}

// is root folder
if utils.PathEqual(path, "/") {
var rootObj model.Obj
Expand Down Expand Up @@ -231,7 +219,28 @@ func Get(ctx context.Context, storage driver.Driver, path string) (model.Obj, er

// not root folder
dir, name := stdpath.Split(path)
files, err := List(ctx, storage, dir, model.ListArgs{})
key := Key(storage, dir)
if files, ok := listCache.Get(key); ok {
log.Debugf("use cache when list %s", dir)
for _, f := range files {
if f.GetName() == name {
return f, nil
}
}
}

// get the obj directly without list so that we can reduce the io
if g, ok := storage.(driver.Getter); ok {
obj, err := g.Get(ctx, path)
if err == nil {
return model.WrapObjName(obj), nil
}
if !errs.IsNotImplementError(err) && !errs.IsNotSupportError(err) {
return nil, errors.WithMessage(err, "failed to get obj")
}
}

files, err := List(ctx, storage, dir, model.ListArgs{Refresh: true})
if err != nil {
return nil, errors.WithMessage(err, "failed get parent list")
}
Expand Down Expand Up @@ -261,32 +270,11 @@ func Link(ctx context.Context, storage driver.Driver, path string, args model.Li
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
return nil, nil, errors.WithMessagef(errs.StorageNotInit, "storage status: %s", storage.GetStorage().Status)
}
var (
file model.Obj
err error
)
// use cache directly
dir, name := stdpath.Split(stdpath.Join(storage.GetStorage().MountPath, path))
if cacheFiles, ok := listCache.Get(strings.TrimSuffix(dir, "/")); ok {
for _, f := range cacheFiles {
if f.GetName() == name {
file = model.UnwrapObj(f)
break
}
}
} else {
if g, ok := storage.(driver.GetObjInfo); ok {
file, err = g.GetObjInfo(ctx, path)
} else {
file, err = GetUnwrap(ctx, storage, path)
}
}
if file == nil {
if err != nil {
return nil, nil, errors.WithMessage(err, "failed to get file")
}
return nil, nil, errors.WithStack(errs.ObjectNotFound)
file, err := GetUnwrap(ctx, storage, path)
if err != nil {
return nil, nil, errors.WithMessage(err, "failed to get file")
}

if file.IsDir() {
return nil, nil, errors.WithStack(errs.NotFile)
}
Expand Down