alist/internal/operations/fs.go

191 lines
5.7 KiB
Go
Raw Normal View History

2022-06-10 12:20:45 +00:00
package operations
import (
"context"
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-15 12:31:23 +00:00
"github.com/alist-org/alist/v3/conf"
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"
"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-15 12:41:17 +00:00
func List(ctx context.Context, account driver.Driver, path string) ([]model.Obj, error) {
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-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)
if files, ok := filesCache.Get(key); ok {
return files, nil
}
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-15 12:31:23 +00:00
// Get get object from list of files
// TODO: maybe should set object ID with path here
2022-06-15 12:41:17 +00:00
func Get(ctx context.Context, account driver.Driver, path string) (model.Obj, error) {
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
}
if r, ok := account.GetAddition().(driver.IRootFolderPath); ok && utils.PathEqual(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)
files, err := List(ctx, account, dir)
if err != nil {
return nil, errors.WithMessage(err, "failed get parent list")
}
for _, f := range files {
if f.GetName() == name {
return f, nil
}
}
return nil, errors.WithStack(driver.ErrorObjectNotFound)
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")
}
link, err := account.Link(ctx, file, args)
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))
}
return link, nil
2022-06-13 11:56:33 +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-15 12:31:23 +00:00
if driver.IsErrObjectNotFound(err) {
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
}
func Move(ctx context.Context, account driver.Driver, srcPath, dstPath 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")
}
dstDir, err := Get(ctx, account, stdpath.Dir(dstPath))
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
func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath 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")
}
dstDir, err := Get(ctx, account, stdpath.Dir(dstPath))
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
if driver.IsErrObjectNotFound(err) {
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-15 10:06:42 +00:00
func Put(ctx context.Context, account driver.Driver, parentPath string, file model.FileStreamer) error {
2022-06-15 12:31:23 +00:00
err := MakeDir(ctx, account, parentPath)
2022-06-15 11:20:36 +00:00
if err != nil {
2022-06-15 12:31:23 +00:00
return errors.WithMessagef(err, "failed to make dir [%s]", parentPath)
}
parentDir, err := Get(ctx, account, parentPath)
// this should not happen
if err != nil {
return errors.WithMessagef(err, "failed to get dir [%s]", parentPath)
2022-06-15 11:20:36 +00:00
}
2022-06-15 12:31:23 +00:00
return account.Put(ctx, parentDir, file)
2022-06-10 13:00:51 +00:00
}