Skip to content

Commit

Permalink
fix(onedrive): rename object in root folder (close #5468)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Dec 17, 2023
1 parent e91c42c commit ab216ed
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 22 deletions.
39 changes: 39 additions & 0 deletions drivers/onedrive/driver.go
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"path"
"sync"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver"
Expand All @@ -19,6 +20,8 @@ type Onedrive struct {
model.Storage
Addition
AccessToken string
root *Object
mutex sync.Mutex
}

func (d *Onedrive) Config() driver.Config {
Expand All @@ -40,6 +43,42 @@ func (d *Onedrive) Drop(ctx context.Context) error {
return nil
}

func (d *Onedrive) GetRoot(ctx context.Context) (model.Obj, error) {
if d.root != nil {
return d.root, nil
}
d.mutex.Lock()
defer d.mutex.Unlock()
root := &Object{
ObjThumb: model.ObjThumb{
Object: model.Object{
ID: "root",
Path: d.RootFolderPath,
Name: "root",
Size: 0,
Modified: d.Modified,
Ctime: d.Modified,
IsFolder: true,
},
},
ParentID: "",
}
if !utils.PathEqual(d.RootFolderPath, "/") {
// get root folder id
url := d.GetMetaUrl(false, d.RootFolderPath)
var resp struct {
Id string `json:"id"`
}
_, err := d.Request(url, http.MethodGet, nil, &resp)
if err != nil {
return nil, err
}
root.ID = resp.Id
}
d.root = root
return d.root, nil
}

func (d *Onedrive) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
files, err := d.getFiles(dir.GetPath())
if err != nil {
Expand Down
39 changes: 39 additions & 0 deletions drivers/onedrive_app/driver.go
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"path"
"sync"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver"
Expand All @@ -19,6 +20,8 @@ type OnedriveAPP struct {
model.Storage
Addition
AccessToken string
root *Object
mutex sync.Mutex
}

func (d *OnedriveAPP) Config() driver.Config {
Expand All @@ -40,6 +43,42 @@ func (d *OnedriveAPP) Drop(ctx context.Context) error {
return nil
}

func (d *OnedriveAPP) GetRoot(ctx context.Context) (model.Obj, error) {
if d.root != nil {
return d.root, nil
}
d.mutex.Lock()
defer d.mutex.Unlock()
root := &Object{
ObjThumb: model.ObjThumb{
Object: model.Object{
ID: "root",
Path: d.RootFolderPath,
Name: "root",
Size: 0,
Modified: d.Modified,
Ctime: d.Modified,
IsFolder: true,
},
},
ParentID: "",
}
if !utils.PathEqual(d.RootFolderPath, "/") {
// get root folder id
url := d.GetMetaUrl(false, d.RootFolderPath)
var resp struct {
Id string `json:"id"`
}
_, err := d.Request(url, http.MethodGet, nil, &resp)
if err != nil {
return nil, err
}
root.ID = resp.Id
}
d.root = root
return d.root, nil
}

func (d *OnedriveAPP) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
files, err := d.getFiles(dir.GetPath())
if err != nil {
Expand Down
46 changes: 24 additions & 22 deletions internal/op/fs.go
Expand Up @@ -177,30 +177,32 @@ func Get(ctx context.Context, storage driver.Driver, path string) (model.Obj, er
// is root folder
if utils.PathEqual(path, "/") {
var rootObj model.Obj
switch r := storage.GetAddition().(type) {
case driver.IRootId:
rootObj = &model.Object{
ID: r.GetRootId(),
Name: RootName,
Size: 0,
Modified: storage.GetStorage().Modified,
IsFolder: true,
}
case driver.IRootPath:
rootObj = &model.Object{
Path: r.GetRootPath(),
Name: RootName,
Size: 0,
Modified: storage.GetStorage().Modified,
IsFolder: true,
if getRooter, ok := storage.(driver.GetRooter); ok {
obj, err := getRooter.GetRoot(ctx)
if err != nil {
return nil, errors.WithMessage(err, "failed get root obj")
}
default:
if storage, ok := storage.(driver.GetRooter); ok {
obj, err := storage.GetRoot(ctx)
if err != nil {
return nil, errors.WithMessage(err, "failed get root obj")
rootObj = obj
} else {
switch r := storage.GetAddition().(type) {
case driver.IRootId:
rootObj = &model.Object{
ID: r.GetRootId(),
Name: RootName,
Size: 0,
Modified: storage.GetStorage().Modified,
IsFolder: true,
}
case driver.IRootPath:
rootObj = &model.Object{
Path: r.GetRootPath(),
Name: RootName,
Size: 0,
Modified: storage.GetStorage().Modified,
IsFolder: true,
}
rootObj = obj
default:
return nil, errors.Errorf("please implement IRootPath or IRootId or GetRooter method")
}
}
if rootObj == nil {
Expand Down

0 comments on commit ab216ed

Please sign in to comment.