mirror of https://github.com/Xhofe/alist
fix(onedrive): rename object in root folder (close #5468)
parent
e91c42c9dc
commit
ab216ed170
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/drivers/base"
|
"github.com/alist-org/alist/v3/drivers/base"
|
||||||
"github.com/alist-org/alist/v3/internal/driver"
|
"github.com/alist-org/alist/v3/internal/driver"
|
||||||
|
@ -19,6 +20,8 @@ type Onedrive struct {
|
||||||
model.Storage
|
model.Storage
|
||||||
Addition
|
Addition
|
||||||
AccessToken string
|
AccessToken string
|
||||||
|
root *Object
|
||||||
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Onedrive) Config() driver.Config {
|
func (d *Onedrive) Config() driver.Config {
|
||||||
|
@ -40,6 +43,42 @@ func (d *Onedrive) Drop(ctx context.Context) error {
|
||||||
return nil
|
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) {
|
func (d *Onedrive) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
|
||||||
files, err := d.getFiles(dir.GetPath())
|
files, err := d.getFiles(dir.GetPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/drivers/base"
|
"github.com/alist-org/alist/v3/drivers/base"
|
||||||
"github.com/alist-org/alist/v3/internal/driver"
|
"github.com/alist-org/alist/v3/internal/driver"
|
||||||
|
@ -19,6 +20,8 @@ type OnedriveAPP struct {
|
||||||
model.Storage
|
model.Storage
|
||||||
Addition
|
Addition
|
||||||
AccessToken string
|
AccessToken string
|
||||||
|
root *Object
|
||||||
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *OnedriveAPP) Config() driver.Config {
|
func (d *OnedriveAPP) Config() driver.Config {
|
||||||
|
@ -40,6 +43,42 @@ func (d *OnedriveAPP) Drop(ctx context.Context) error {
|
||||||
return nil
|
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) {
|
func (d *OnedriveAPP) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
|
||||||
files, err := d.getFiles(dir.GetPath())
|
files, err := d.getFiles(dir.GetPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -177,30 +177,32 @@ func Get(ctx context.Context, storage driver.Driver, path string) (model.Obj, er
|
||||||
// is root folder
|
// is root folder
|
||||||
if utils.PathEqual(path, "/") {
|
if utils.PathEqual(path, "/") {
|
||||||
var rootObj model.Obj
|
var rootObj model.Obj
|
||||||
switch r := storage.GetAddition().(type) {
|
if getRooter, ok := storage.(driver.GetRooter); ok {
|
||||||
case driver.IRootId:
|
obj, err := getRooter.GetRoot(ctx)
|
||||||
rootObj = &model.Object{
|
if err != nil {
|
||||||
ID: r.GetRootId(),
|
return nil, errors.WithMessage(err, "failed get root obj")
|
||||||
Name: RootName,
|
|
||||||
Size: 0,
|
|
||||||
Modified: storage.GetStorage().Modified,
|
|
||||||
IsFolder: true,
|
|
||||||
}
|
}
|
||||||
case driver.IRootPath:
|
rootObj = obj
|
||||||
rootObj = &model.Object{
|
} else {
|
||||||
Path: r.GetRootPath(),
|
switch r := storage.GetAddition().(type) {
|
||||||
Name: RootName,
|
case driver.IRootId:
|
||||||
Size: 0,
|
rootObj = &model.Object{
|
||||||
Modified: storage.GetStorage().Modified,
|
ID: r.GetRootId(),
|
||||||
IsFolder: true,
|
Name: RootName,
|
||||||
}
|
Size: 0,
|
||||||
default:
|
Modified: storage.GetStorage().Modified,
|
||||||
if storage, ok := storage.(driver.GetRooter); ok {
|
IsFolder: true,
|
||||||
obj, err := storage.GetRoot(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.WithMessage(err, "failed get root obj")
|
|
||||||
}
|
}
|
||||||
rootObj = obj
|
case driver.IRootPath:
|
||||||
|
rootObj = &model.Object{
|
||||||
|
Path: r.GetRootPath(),
|
||||||
|
Name: RootName,
|
||||||
|
Size: 0,
|
||||||
|
Modified: storage.GetStorage().Modified,
|
||||||
|
IsFolder: true,
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return nil, errors.Errorf("please implement IRootPath or IRootId or GetRooter method")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if rootObj == nil {
|
if rootObj == nil {
|
||||||
|
|
Loading…
Reference in New Issue