alist/drivers/onedrive/driver.go

279 lines
6.6 KiB
Go
Raw Normal View History

2021-12-06 07:55:05 +00:00
package onedrive
2021-11-27 12:20:14 +00:00
import (
"fmt"
"github.com/Xhofe/alist/conf"
2021-12-06 07:55:05 +00:00
"github.com/Xhofe/alist/drivers/base"
2021-11-27 12:20:14 +00:00
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
2021-11-28 09:01:27 +00:00
"path/filepath"
2021-11-27 12:20:14 +00:00
)
type Onedrive struct{}
2021-12-06 07:55:05 +00:00
func (driver Onedrive) Config() base.DriverConfig {
return base.DriverConfig{
Name: "Onedrive",
NoNeedSetLink: true,
2021-11-29 08:42:46 +00:00
}
}
2021-12-06 07:55:05 +00:00
func (driver Onedrive) Items() []base.Item {
return []base.Item{
2021-11-27 12:20:14 +00:00
{
Name: "zone",
Label: "zone",
2021-12-06 07:55:05 +00:00
Type: base.TypeSelect,
2021-11-27 12:20:14 +00:00
Required: true,
Values: "global,cn,us,de",
Description: "",
},
{
2021-12-16 07:44:18 +00:00
Name: "internal_type",
2021-11-27 12:20:14 +00:00
Label: "onedrive type",
2021-12-06 07:55:05 +00:00
Type: base.TypeSelect,
2021-11-27 12:20:14 +00:00
Required: true,
Values: "onedrive,sharepoint",
},
{
Name: "client_id",
Label: "client id",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-27 12:20:14 +00:00
Required: true,
},
{
Name: "client_secret",
Label: "client secret",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-27 12:20:14 +00:00
Required: true,
},
{
Name: "redirect_uri",
Label: "redirect uri",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-27 12:20:14 +00:00
Required: true,
},
{
Name: "refresh_token",
Label: "refresh token",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-27 12:20:14 +00:00
Required: true,
},
{
Name: "site_id",
Label: "site id",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-27 12:20:14 +00:00
Required: false,
},
{
Name: "root_folder",
Label: "root folder path",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-27 12:20:14 +00:00
Required: false,
},
{
Name: "order_by",
Label: "order_by",
2021-12-06 07:55:05 +00:00
Type: base.TypeSelect,
2021-11-27 12:20:14 +00:00
Values: "name,size,lastModifiedDateTime",
Required: false,
},
{
Name: "order_direction",
Label: "order_direction",
2021-12-06 07:55:05 +00:00
Type: base.TypeSelect,
2021-11-27 12:20:14 +00:00
Values: "asc,desc",
Required: false,
},
}
}
func (driver Onedrive) Save(account *model.Account, old *model.Account) error {
//if old != nil {
// conf.Cron.Remove(cron.EntryID(old.CronId))
//}
if account == nil {
return nil
}
2021-11-27 12:20:14 +00:00
_, ok := onedriveHostMap[account.Zone]
if !ok {
return fmt.Errorf("no [%s] zone", account.Zone)
}
account.RootFolder = utils.ParsePath(account.RootFolder)
err := driver.RefreshToken(account)
_ = model.SaveAccount(account)
2021-11-27 12:20:14 +00:00
if err != nil {
return err
}
//cronId, err := conf.Cron.AddFunc("@every 1h", func() {
// name := account.Name
// log.Debugf("onedrive account name: %s", name)
// newAccount, ok := model.GetAccount(name)
// log.Debugf("onedrive account: %+v", newAccount)
// if !ok {
// return
// }
// err = driver.RefreshToken(&newAccount)
// _ = model.SaveAccount(&newAccount)
//})
//if err != nil {
// return err
//}
//account.CronId = int(cronId)
2021-11-27 12:20:14 +00:00
return nil
}
func (driver Onedrive) File(path string, account *model.Account) (*model.File, error) {
path = utils.ParsePath(path)
2021-11-28 09:01:27 +00:00
if path == "/" {
return &model.File{
Id: account.RootFolder,
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
2021-11-30 01:37:51 +00:00
Driver: driver.Config().Name,
2021-11-28 09:01:27 +00:00
UpdatedAt: account.UpdatedAt,
}, nil
}
dir, name := filepath.Split(path)
files, err := driver.Files(dir, account)
2021-11-27 12:20:14 +00:00
if err != nil {
return nil, err
}
2021-11-28 09:01:27 +00:00
for _, file := range files {
if file.Name == name {
return &file, nil
}
}
2021-12-06 07:55:05 +00:00
return nil, base.ErrPathNotFound
2021-11-27 12:20:14 +00:00
}
func (driver Onedrive) Files(path string, account *model.Account) ([]model.File, error) {
path = utils.ParsePath(path)
2021-12-08 14:58:44 +00:00
cache, err := base.GetCache(path, account)
2021-11-27 12:20:14 +00:00
if err == nil {
files, _ := cache.([]model.File)
return files, nil
}
rawFiles, err := driver.GetFiles(account, path)
if err != nil {
return nil, err
}
files := make([]model.File, 0)
for _, file := range rawFiles {
files = append(files, *driver.FormatFile(&file))
}
2021-12-01 03:25:33 +00:00
if len(files) > 0 {
2021-12-08 14:58:44 +00:00
_ = base.SetCache(path, files, account)
2021-12-01 03:25:33 +00:00
}
2021-11-27 12:20:14 +00:00
return files, nil
}
2021-12-19 12:00:53 +00:00
func (driver Onedrive) Link(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.GetFile(account, args.Path)
2021-11-27 12:20:14 +00:00
if err != nil {
2021-12-09 11:24:34 +00:00
return nil, err
2021-11-27 12:20:14 +00:00
}
if file.File == nil {
2021-12-09 11:24:34 +00:00
return nil, base.ErrNotFile
}
link := base.Link{
Url: file.Url,
2021-11-27 12:20:14 +00:00
}
2021-12-09 11:24:34 +00:00
return &link, nil
2021-11-27 12:20:14 +00:00
}
func (driver Onedrive) Path(path string, account *model.Account) (*model.File, []model.File, error) {
log.Debugf("onedrive path: %s", path)
file, err := driver.File(path, account)
if err != nil {
return nil, nil, err
}
2021-12-09 11:24:34 +00:00
if !file.IsDir() {
2021-11-27 12:20:14 +00:00
return file, nil, nil
}
files, err := driver.Files(path, account)
if err != nil {
return nil, nil, err
}
return nil, files, nil
}
2022-02-02 10:03:54 +00:00
//func (driver Onedrive) Proxy(r *http.Request, account *model.Account) {
// r.Header.Del("Origin")
//}
2021-11-27 12:20:14 +00:00
func (driver Onedrive) Preview(path string, account *model.Account) (interface{}, error) {
2021-12-06 07:55:05 +00:00
return nil, base.ErrNotSupport
2021-12-05 07:22:19 +00:00
}
func (driver Onedrive) MakeDir(path string, account *model.Account) error {
2021-12-28 12:34:45 +00:00
url := driver.GetMetaUrl(account, false, utils.Dir(path)) + "/children"
data := base.Json{
"name": utils.Base(path),
"folder": base.Json{},
"@microsoft.graph.conflictBehavior": "rename",
}
_, err := driver.Request(url, base.Post, nil, nil, nil, &data, nil, account)
return err
2021-12-05 07:22:19 +00:00
}
func (driver Onedrive) Move(src string, dst string, account *model.Account) error {
2021-12-28 12:34:45 +00:00
dstParentFile, err := driver.GetFile(account, utils.Dir(dst))
if err != nil {
return err
}
data := base.Json{
"parentReference": base.Json{
"id": dstParentFile.Id,
},
"name": utils.Base(dst),
}
url := driver.GetMetaUrl(account, false, src)
_, err = driver.Request(url, base.Patch, nil, nil, nil, &data, nil, account)
return err
2021-12-05 07:22:19 +00:00
}
2022-01-03 12:06:36 +00:00
func (driver Onedrive) Rename(src string, dst string, account *model.Account) error {
return driver.Move(src, dst, account)
}
2021-12-05 07:22:19 +00:00
func (driver Onedrive) Copy(src string, dst string, account *model.Account) error {
2021-12-28 12:34:45 +00:00
dstParentFile, err := driver.GetFile(account, utils.Dir(dst))
if err != nil {
return err
}
data := base.Json{
"parentReference": base.Json{
"driveId": dstParentFile.ParentReference.DriveId,
"id": dstParentFile.Id,
},
"name": utils.Base(dst),
}
url := driver.GetMetaUrl(account, false, src) + "/copy"
_, err = driver.Request(url, base.Post, nil, nil, nil, &data, nil, account)
return err
2021-12-05 07:22:19 +00:00
}
func (driver Onedrive) Delete(path string, account *model.Account) error {
2021-12-28 12:34:45 +00:00
url := driver.GetMetaUrl(account, false, path)
_, err := driver.Request(url, base.Delete, nil, nil, nil, nil, nil, account)
return err
2021-12-05 07:22:19 +00:00
}
func (driver Onedrive) Upload(file *model.FileStream, account *model.Account) error {
2021-12-31 06:05:35 +00:00
if file == nil {
return base.ErrEmptyFile
}
2021-12-28 12:34:45 +00:00
var err error
if file.GetSize() <= 4*1024*1024 {
err = driver.UploadSmall(file, account)
} else {
err = driver.UploadBig(file, account)
}
return err
2021-11-27 12:20:14 +00:00
}
2021-11-30 02:00:06 +00:00
2021-12-19 12:00:53 +00:00
var _ base.Driver = (*Onedrive)(nil)