2022-06-10 12:20:45 +00:00
|
|
|
package operations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-25 12:38:02 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
2022-06-23 07:57:10 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
2022-06-23 09:05:03 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-06-13 13:14:01 +00:00
|
|
|
stdpath "path"
|
|
|
|
"time"
|
|
|
|
|
2022-06-13 11:56:33 +00:00
|
|
|
"github.com/Xhofe/go-cache"
|
2022-06-10 12:20:45 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/driver"
|
2022-06-11 06:43:03 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
2022-06-13 12:24:13 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/singleflight"
|
2022-06-11 06:43:03 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
|
|
|
"github.com/pkg/errors"
|
2022-06-10 12:20:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// In order to facilitate adding some other things before and after file operations
|
|
|
|
|
2022-06-15 12:41:17 +00:00
|
|
|
var filesCache = cache.NewMemCache(cache.WithShards[[]model.Obj](64))
|
|
|
|
var filesG singleflight.Group[[]model.Obj]
|
2022-06-13 13:14:01 +00:00
|
|
|
|
2022-06-10 12:20:45 +00:00
|
|
|
// List files in storage, not contains virtual file
|
2022-06-20 14:29:52 +00:00
|
|
|
func List(ctx context.Context, account driver.Driver, path string, refresh ...bool) ([]model.Obj, error) {
|
2022-06-27 11:51:23 +00:00
|
|
|
log.Debugf("operations.List %s", path)
|
2022-06-15 12:31:23 +00:00
|
|
|
dir, err := Get(ctx, account, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed get dir")
|
|
|
|
}
|
2022-06-23 08:09:22 +00:00
|
|
|
if !dir.IsDir() {
|
|
|
|
return nil, errors.WithStack(errs.NotFolder)
|
|
|
|
}
|
2022-06-13 13:15:58 +00:00
|
|
|
if account.Config().NoCache {
|
2022-06-15 12:31:23 +00:00
|
|
|
return account.List(ctx, dir)
|
2022-06-13 13:15:58 +00:00
|
|
|
}
|
2022-06-13 13:14:01 +00:00
|
|
|
key := stdpath.Join(account.GetAccount().VirtualPath, path)
|
2022-06-20 14:29:52 +00:00
|
|
|
if len(refresh) == 0 || !refresh[0] {
|
|
|
|
if files, ok := filesCache.Get(key); ok {
|
|
|
|
return files, nil
|
|
|
|
}
|
2022-06-13 13:14:01 +00:00
|
|
|
}
|
2022-06-15 12:41:17 +00:00
|
|
|
files, err, _ := filesG.Do(key, func() ([]model.Obj, error) {
|
2022-06-15 12:31:23 +00:00
|
|
|
files, err := account.List(ctx, dir)
|
2022-06-13 13:14:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed to list files")
|
|
|
|
}
|
2022-06-15 12:31:23 +00:00
|
|
|
// TODO: maybe can get duration from account's config
|
2022-06-15 12:41:17 +00:00
|
|
|
filesCache.Set(key, files, cache.WithEx[[]model.Obj](time.Minute*time.Duration(conf.Conf.CaCheExpiration)))
|
2022-06-13 13:14:01 +00:00
|
|
|
return files, nil
|
|
|
|
})
|
|
|
|
return files, err
|
2022-06-10 12:20:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 11:51:23 +00:00
|
|
|
func isRoot(path, rootFolderPath string) bool {
|
|
|
|
if utils.PathEqual(path, rootFolderPath) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// relative path
|
|
|
|
if utils.PathEqual(path, "/") && rootFolderPath == "." {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-16 12:25:33 +00:00
|
|
|
// Get object from list of files
|
2022-06-15 12:41:17 +00:00
|
|
|
func Get(ctx context.Context, account driver.Driver, path string) (model.Obj, error) {
|
2022-06-27 11:51:23 +00:00
|
|
|
log.Debugf("operations.Get %s", path)
|
2022-06-15 12:31:23 +00:00
|
|
|
// is root folder
|
|
|
|
if r, ok := account.GetAddition().(driver.IRootFolderId); ok && utils.PathEqual(path, "/") {
|
2022-06-15 12:41:17 +00:00
|
|
|
return model.Object{
|
2022-06-15 12:31:23 +00:00
|
|
|
ID: r.GetRootFolderId(),
|
|
|
|
Name: "root",
|
|
|
|
Size: 0,
|
|
|
|
Modified: account.GetAccount().Modified,
|
|
|
|
IsFolder: true,
|
2022-06-11 06:43:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2022-06-27 11:51:23 +00:00
|
|
|
if r, ok := account.GetAddition().(driver.IRootFolderPath); ok && isRoot(path, r.GetRootFolderPath()) {
|
2022-06-15 12:41:17 +00:00
|
|
|
return model.Object{
|
2022-06-15 12:31:23 +00:00
|
|
|
ID: r.GetRootFolderPath(),
|
2022-06-11 06:43:03 +00:00
|
|
|
Name: "root",
|
|
|
|
Size: 0,
|
|
|
|
Modified: account.GetAccount().Modified,
|
|
|
|
IsFolder: true,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-06-15 12:31:23 +00:00
|
|
|
// not root folder
|
2022-06-11 06:43:03 +00:00
|
|
|
dir, name := stdpath.Split(path)
|
2022-06-27 12:37:05 +00:00
|
|
|
files, err := List(ctx, account, utils.StandardizePath(dir))
|
2022-06-11 06:43:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed get parent list")
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
if f.GetName() == name {
|
2022-06-16 12:25:33 +00:00
|
|
|
// use path as id, why don't set id in List function?
|
|
|
|
// because files maybe cache, set id here can reduce memory usage
|
|
|
|
if f.GetID() == "" {
|
|
|
|
if s, ok := f.(model.SetID); ok {
|
|
|
|
s.SetID(path)
|
|
|
|
}
|
|
|
|
}
|
2022-06-11 06:43:03 +00:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
}
|
2022-06-23 08:03:27 +00:00
|
|
|
return nil, errors.WithStack(errs.ObjectNotFound)
|
2022-06-10 12:20:45 +00:00
|
|
|
}
|
2022-06-10 13:00:51 +00:00
|
|
|
|
2022-06-15 10:06:42 +00:00
|
|
|
var linkCache = cache.NewMemCache(cache.WithShards[*model.Link](16))
|
|
|
|
var linkG singleflight.Group[*model.Link]
|
2022-06-13 11:56:33 +00:00
|
|
|
|
|
|
|
// Link get link, if is an url. should have an expiry time
|
2022-06-15 10:06:42 +00:00
|
|
|
func Link(ctx context.Context, account driver.Driver, path string, args model.LinkArgs) (*model.Link, error) {
|
2022-06-13 11:56:33 +00:00
|
|
|
key := stdpath.Join(account.GetAccount().VirtualPath, path)
|
|
|
|
if link, ok := linkCache.Get(key); ok {
|
|
|
|
return link, nil
|
|
|
|
}
|
2022-06-15 10:06:42 +00:00
|
|
|
fn := func() (*model.Link, error) {
|
2022-06-15 12:31:23 +00:00
|
|
|
file, err := Get(ctx, account, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed to get file")
|
|
|
|
}
|
2022-06-23 07:57:10 +00:00
|
|
|
if file.IsDir() {
|
2022-06-23 08:03:27 +00:00
|
|
|
return nil, errors.WithStack(errs.NotFile)
|
2022-06-23 07:57:10 +00:00
|
|
|
}
|
2022-06-15 12:31:23 +00:00
|
|
|
link, err := account.Link(ctx, file, args)
|
2022-06-13 12:24:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed get link")
|
|
|
|
}
|
|
|
|
if link.Expiration != nil {
|
2022-06-15 10:06:42 +00:00
|
|
|
linkCache.Set(key, link, cache.WithEx[*model.Link](*link.Expiration))
|
2022-06-13 12:24:13 +00:00
|
|
|
}
|
|
|
|
return link, nil
|
2022-06-13 11:56:33 +00:00
|
|
|
}
|
2022-06-13 12:24:13 +00:00
|
|
|
link, err, _ := linkG.Do(key, fn)
|
|
|
|
return link, err
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MakeDir(ctx context.Context, account driver.Driver, path string) error {
|
2022-06-15 11:10:11 +00:00
|
|
|
// check if dir exists
|
|
|
|
f, err := Get(ctx, account, path)
|
|
|
|
if err != nil {
|
2022-06-23 08:03:27 +00:00
|
|
|
if errs.IsObjectNotFound(err) {
|
2022-06-15 12:31:23 +00:00
|
|
|
parentPath, dirName := stdpath.Split(path)
|
|
|
|
err = MakeDir(ctx, account, parentPath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessagef(err, "failed to make parent dir [%s]", parentPath)
|
|
|
|
}
|
|
|
|
parentDir, err := Get(ctx, account, parentPath)
|
|
|
|
// this should not happen
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessagef(err, "failed to get parent dir [%s]", parentPath)
|
|
|
|
}
|
|
|
|
return account.MakeDir(ctx, parentDir, dirName)
|
|
|
|
} else {
|
|
|
|
return errors.WithMessage(err, "failed to check if dir exists")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// dir exists
|
|
|
|
if f.IsDir() {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
// dir to make is a file
|
|
|
|
return errors.New("file exists")
|
|
|
|
}
|
2022-06-15 11:10:11 +00:00
|
|
|
}
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 08:37:51 +00:00
|
|
|
func Move(ctx context.Context, account driver.Driver, srcPath, dstDirPath string) error {
|
2022-06-15 12:41:17 +00:00
|
|
|
srcObj, err := Get(ctx, account, srcPath)
|
2022-06-15 12:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, "failed to get src object")
|
|
|
|
}
|
2022-06-21 08:37:51 +00:00
|
|
|
dstDir, err := Get(ctx, account, dstDirPath)
|
2022-06-21 08:25:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, "failed to get dst dir")
|
|
|
|
}
|
2022-06-15 12:41:17 +00:00
|
|
|
return account.Move(ctx, srcObj, dstDir)
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string) error {
|
2022-06-15 12:41:17 +00:00
|
|
|
srcObj, err := Get(ctx, account, srcPath)
|
2022-06-15 12:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, "failed to get src object")
|
|
|
|
}
|
2022-06-15 12:41:17 +00:00
|
|
|
return account.Rename(ctx, srcObj, dstName)
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy Just copy file[s] in an account
|
2022-06-21 08:37:51 +00:00
|
|
|
func Copy(ctx context.Context, account driver.Driver, srcPath, dstDirPath string) error {
|
2022-06-15 12:41:17 +00:00
|
|
|
srcObj, err := Get(ctx, account, srcPath)
|
2022-06-15 12:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, "failed to get src object")
|
|
|
|
}
|
2022-06-21 08:37:51 +00:00
|
|
|
dstDir, err := Get(ctx, account, dstDirPath)
|
2022-06-15 12:41:17 +00:00
|
|
|
return account.Copy(ctx, srcObj, dstDir)
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Remove(ctx context.Context, account driver.Driver, path string) error {
|
2022-06-15 12:41:17 +00:00
|
|
|
obj, err := Get(ctx, account, path)
|
2022-06-15 12:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
// if object not found, it's ok
|
2022-06-23 08:03:27 +00:00
|
|
|
if errs.IsObjectNotFound(err) {
|
2022-06-15 12:31:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.WithMessage(err, "failed to get object")
|
|
|
|
}
|
2022-06-15 12:41:17 +00:00
|
|
|
return account.Remove(ctx, obj)
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 07:03:27 +00:00
|
|
|
func Put(ctx context.Context, account driver.Driver, dstDirPath string, file model.FileStreamer, up driver.UpdateProgress) error {
|
2022-06-23 09:05:03 +00:00
|
|
|
defer func() {
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
log.Errorf("failed to close file streamer, %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2022-06-22 07:03:27 +00:00
|
|
|
err := MakeDir(ctx, account, dstDirPath)
|
2022-06-15 11:20:36 +00:00
|
|
|
if err != nil {
|
2022-06-22 07:03:27 +00:00
|
|
|
return errors.WithMessagef(err, "failed to make dir [%s]", dstDirPath)
|
2022-06-15 12:31:23 +00:00
|
|
|
}
|
2022-06-22 07:03:27 +00:00
|
|
|
parentDir, err := Get(ctx, account, dstDirPath)
|
2022-06-15 12:31:23 +00:00
|
|
|
// this should not happen
|
|
|
|
if err != nil {
|
2022-06-22 07:03:27 +00:00
|
|
|
return errors.WithMessagef(err, "failed to get dir [%s]", dstDirPath)
|
2022-06-15 11:20:36 +00:00
|
|
|
}
|
2022-06-17 13:23:44 +00:00
|
|
|
// if up is nil, set a default to prevent panic
|
|
|
|
if up == nil {
|
2022-06-20 12:34:58 +00:00
|
|
|
up = func(p int) {}
|
2022-06-17 13:23:44 +00:00
|
|
|
}
|
2022-06-24 06:24:39 +00:00
|
|
|
err = account.Put(ctx, parentDir, file, up)
|
|
|
|
if err == nil {
|
|
|
|
// clear cache
|
|
|
|
key := stdpath.Join(account.GetAccount().VirtualPath, dstDirPath)
|
|
|
|
filesCache.Del(key)
|
|
|
|
}
|
|
|
|
return err
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|