2022-08-31 13:01:15 +00:00
|
|
|
package op
|
2022-06-10 12:20:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-01 07:04:02 +00:00
|
|
|
"os"
|
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-07-30 12:04:21 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
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-07-30 12:04:21 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-06-10 12:20:45 +00:00
|
|
|
)
|
|
|
|
|
2022-08-31 13:01:15 +00:00
|
|
|
// In order to facilitate adding some other things before and after file op
|
2022-06-10 12:20:45 +00:00
|
|
|
|
2022-08-31 12:58:57 +00:00
|
|
|
var listCache = cache.NewMemCache(cache.WithShards[[]model.Obj](64))
|
|
|
|
var listG singleflight.Group[[]model.Obj]
|
2022-06-13 13:14:01 +00:00
|
|
|
|
2022-07-10 06:45:39 +00:00
|
|
|
func ClearCache(storage driver.Driver, path string) {
|
2022-12-17 11:49:05 +00:00
|
|
|
listCache.Del(Key(storage, path))
|
2022-06-30 14:51:49 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 12:28:52 +00:00
|
|
|
func Key(storage driver.Driver, path string) string {
|
2022-12-17 11:49:05 +00:00
|
|
|
return stdpath.Join(storage.GetStorage().MountPath, utils.FixAndCleanPath(path))
|
2022-09-14 12:28:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 12:20:45 +00:00
|
|
|
// List files in storage, not contains virtual file
|
2022-08-11 12:32:17 +00:00
|
|
|
func List(ctx context.Context, storage driver.Driver, path string, args model.ListArgs, refresh ...bool) ([]model.Obj, error) {
|
2022-09-04 04:43:52 +00:00
|
|
|
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
|
2022-09-03 14:32:09 +00:00
|
|
|
return nil, errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
path = utils.FixAndCleanPath(path)
|
2022-08-31 13:01:15 +00:00
|
|
|
log.Debugf("op.List %s", path)
|
2022-09-28 13:19:36 +00:00
|
|
|
key := Key(storage, path)
|
|
|
|
if len(refresh) == 0 || !refresh[0] {
|
|
|
|
if files, ok := listCache.Get(key); ok {
|
|
|
|
log.Debugf("use cache when list %s", path)
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
dir, err := GetUnwrap(ctx, storage, path)
|
2022-06-15 12:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed get dir")
|
|
|
|
}
|
2022-09-02 14:46:31 +00:00
|
|
|
log.Debugf("list dir: %+v", dir)
|
2022-06-23 08:09:22 +00:00
|
|
|
if !dir.IsDir() {
|
|
|
|
return nil, errors.WithStack(errs.NotFolder)
|
|
|
|
}
|
2022-08-31 12:58:57 +00:00
|
|
|
objs, err, _ := listG.Do(key, func() ([]model.Obj, error) {
|
2022-08-11 12:32:17 +00:00
|
|
|
files, err := storage.List(ctx, dir, args)
|
2022-06-13 13:14:01 +00:00
|
|
|
if err != nil {
|
2022-08-31 12:58:57 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to list objs")
|
2022-06-13 13:14:01 +00:00
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
// set path
|
|
|
|
for _, f := range files {
|
|
|
|
if s, ok := f.(model.SetPath); ok && f.GetPath() == "" && dir.GetPath() != "" {
|
|
|
|
s.SetPath(stdpath.Join(dir.GetPath(), f.GetName()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// warp obj name
|
|
|
|
model.WrapObjsName(files)
|
2022-11-28 05:45:25 +00:00
|
|
|
// call hooks
|
2022-12-05 12:23:37 +00:00
|
|
|
go func(reqPath string, files []model.Obj) {
|
2022-11-28 05:45:25 +00:00
|
|
|
for _, hook := range objsUpdateHooks {
|
|
|
|
hook(args.ReqPath, files)
|
|
|
|
}
|
2022-12-05 12:23:37 +00:00
|
|
|
}(args.ReqPath, files)
|
2022-10-25 08:42:06 +00:00
|
|
|
if !storage.Config().NoCache {
|
|
|
|
if len(files) > 0 {
|
2022-11-15 06:38:23 +00:00
|
|
|
log.Debugf("set cache: %s => %+v", key, files)
|
2022-10-25 08:42:06 +00:00
|
|
|
listCache.Set(key, files, cache.WithEx[[]model.Obj](time.Minute*time.Duration(storage.GetStorage().CacheExpiration)))
|
|
|
|
} else {
|
2022-11-15 06:38:23 +00:00
|
|
|
log.Debugf("del cache: %s", key)
|
2022-10-25 08:42:06 +00:00
|
|
|
listCache.Del(key)
|
|
|
|
}
|
2022-09-28 13:19:36 +00:00
|
|
|
}
|
2022-06-13 13:14:01 +00:00
|
|
|
return files, nil
|
|
|
|
})
|
2022-08-31 12:58:57 +00:00
|
|
|
return objs, err
|
2022-06-10 12:20:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 12:25:33 +00:00
|
|
|
// Get object from list of files
|
2022-07-10 06:45:39 +00:00
|
|
|
func Get(ctx context.Context, storage driver.Driver, path string) (model.Obj, error) {
|
2022-12-17 11:49:05 +00:00
|
|
|
path = utils.FixAndCleanPath(path)
|
2022-08-31 13:01:15 +00:00
|
|
|
log.Debugf("op.Get %s", path)
|
2022-12-17 11:49:05 +00:00
|
|
|
|
2022-06-15 12:31:23 +00:00
|
|
|
// is root folder
|
2022-12-17 11:49:05 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if storage, ok := storage.(driver.Getter); ok {
|
|
|
|
obj, err := storage.GetRoot(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed get root obj")
|
|
|
|
}
|
|
|
|
rootObj = obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if rootObj == nil {
|
|
|
|
return nil, errors.Errorf("please implement IRootPath or IRootId or Getter method")
|
|
|
|
}
|
|
|
|
return &model.ObjWrapName{
|
|
|
|
Name: RootName,
|
|
|
|
Obj: rootObj,
|
2022-06-11 06:43:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
|
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-08-11 12:32:17 +00:00
|
|
|
files, err := List(ctx, storage, dir, model.ListArgs{})
|
2022-06-11 06:43:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed get parent list")
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
2022-09-02 14:46:31 +00:00
|
|
|
// TODO maybe copy obj here
|
2022-11-30 12:46:54 +00:00
|
|
|
if f.GetName() == name {
|
2022-06-11 06:43:03 +00:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
}
|
2022-09-28 13:19:36 +00:00
|
|
|
log.Debugf("cant find obj with name: %s", name)
|
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-12-17 11:49:05 +00:00
|
|
|
func GetUnwrap(ctx context.Context, storage driver.Driver, path string) (model.Obj, error) {
|
|
|
|
obj, err := Get(ctx, storage, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if unwrap, ok := obj.(model.UnwrapObj); ok {
|
|
|
|
obj = unwrap.Unwrap()
|
|
|
|
}
|
|
|
|
return obj, err
|
|
|
|
}
|
|
|
|
|
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-07-10 06:45:39 +00:00
|
|
|
func Link(ctx context.Context, storage driver.Driver, path string, args model.LinkArgs) (*model.Link, model.Obj, error) {
|
2022-09-04 04:43:52 +00:00
|
|
|
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
|
2022-09-03 14:32:09 +00:00
|
|
|
return nil, nil, errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
file, err := GetUnwrap(ctx, storage, path)
|
2022-06-28 13:58:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.WithMessage(err, "failed to get file")
|
|
|
|
}
|
|
|
|
if file.IsDir() {
|
|
|
|
return nil, nil, errors.WithStack(errs.NotFile)
|
|
|
|
}
|
2022-09-10 06:12:57 +00:00
|
|
|
key := stdpath.Join(storage.GetStorage().MountPath, path) + ":" + args.IP
|
2022-06-13 11:56:33 +00:00
|
|
|
if link, ok := linkCache.Get(key); ok {
|
2022-06-28 13:58:46 +00:00
|
|
|
return link, file, nil
|
2022-06-13 11:56:33 +00:00
|
|
|
}
|
2022-06-15 10:06:42 +00:00
|
|
|
fn := func() (*model.Link, error) {
|
2022-07-10 06:45:39 +00:00
|
|
|
link, err := storage.Link(ctx, file, args)
|
2022-06-13 12:24:13 +00:00
|
|
|
if err != nil {
|
2022-08-31 12:58:57 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed get link")
|
2022-06-13 12:24:13 +00:00
|
|
|
}
|
|
|
|
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)
|
2022-06-28 13:58:46 +00:00
|
|
|
return link, file, err
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 12:32:17 +00:00
|
|
|
// Other api
|
2022-08-03 06:14:37 +00:00
|
|
|
func Other(ctx context.Context, storage driver.Driver, args model.FsOtherArgs) (interface{}, error) {
|
2022-12-17 11:49:05 +00:00
|
|
|
obj, err := GetUnwrap(ctx, storage, args.Path)
|
2022-08-03 06:14:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessagef(err, "failed to get obj")
|
|
|
|
}
|
2022-09-11 10:40:19 +00:00
|
|
|
if o, ok := storage.(driver.Other); ok {
|
|
|
|
return o.Other(ctx, model.OtherArgs{
|
|
|
|
Obj: obj,
|
|
|
|
Method: args.Method,
|
|
|
|
Data: args.Data,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return nil, errs.NotImplement
|
|
|
|
}
|
2022-08-03 06:14:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 05:43:23 +00:00
|
|
|
var mkdirG singleflight.Group[interface{}]
|
|
|
|
|
2022-07-10 06:45:39 +00:00
|
|
|
func MakeDir(ctx context.Context, storage driver.Driver, path string) error {
|
2022-09-04 04:43:52 +00:00
|
|
|
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
|
2022-09-03 14:32:09 +00:00
|
|
|
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
path = utils.FixAndCleanPath(path)
|
2022-09-19 05:43:23 +00:00
|
|
|
key := Key(storage, path)
|
|
|
|
_, err, _ := mkdirG.Do(key, func() (interface{}, error) {
|
|
|
|
// check if dir exists
|
2022-12-17 11:49:05 +00:00
|
|
|
f, err := GetUnwrap(ctx, storage, path)
|
2022-09-19 05:43:23 +00:00
|
|
|
if err != nil {
|
|
|
|
if errs.IsObjectNotFound(err) {
|
|
|
|
parentPath, dirName := stdpath.Split(path)
|
|
|
|
err = MakeDir(ctx, storage, parentPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessagef(err, "failed to make parent dir [%s]", parentPath)
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
parentDir, err := GetUnwrap(ctx, storage, parentPath)
|
2022-09-19 05:43:23 +00:00
|
|
|
// this should not happen
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessagef(err, "failed to get parent dir [%s]", parentPath)
|
|
|
|
}
|
|
|
|
err = storage.MakeDir(ctx, parentDir, dirName)
|
|
|
|
if err == nil {
|
|
|
|
ClearCache(storage, parentPath)
|
|
|
|
}
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
} else {
|
|
|
|
return nil, errors.WithMessage(err, "failed to check if dir exists")
|
2022-09-13 10:34:04 +00:00
|
|
|
}
|
2022-06-15 12:31:23 +00:00
|
|
|
} else {
|
2022-09-19 05:43:23 +00:00
|
|
|
// dir exists
|
|
|
|
if f.IsDir() {
|
|
|
|
return nil, nil
|
|
|
|
} else {
|
|
|
|
// dir to make is a file
|
|
|
|
return nil, errors.New("file exists")
|
|
|
|
}
|
2022-06-15 12:31:23 +00:00
|
|
|
}
|
2022-09-19 05:43:23 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 06:45:39 +00:00
|
|
|
func Move(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string) error {
|
2022-09-04 04:43:52 +00:00
|
|
|
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
|
2022-09-03 14:32:09 +00:00
|
|
|
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
srcObj, err := GetUnwrap(ctx, storage, srcPath)
|
2022-06-15 12:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, "failed to get src object")
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
dstDir, err := GetUnwrap(ctx, storage, dstDirPath)
|
2022-06-21 08:25:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, "failed to get dst dir")
|
|
|
|
}
|
2022-08-31 12:58:57 +00:00
|
|
|
return errors.WithStack(storage.Move(ctx, srcObj, dstDir))
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 06:45:39 +00:00
|
|
|
func Rename(ctx context.Context, storage driver.Driver, srcPath, dstName string) error {
|
2022-09-04 04:43:52 +00:00
|
|
|
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
|
2022-09-03 14:32:09 +00:00
|
|
|
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
srcObj, err := GetUnwrap(ctx, storage, srcPath)
|
2022-06-15 12:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, "failed to get src object")
|
|
|
|
}
|
2022-08-31 12:58:57 +00:00
|
|
|
return errors.WithStack(storage.Rename(ctx, srcObj, dstName))
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 06:45:39 +00:00
|
|
|
// Copy Just copy file[s] in a storage
|
|
|
|
func Copy(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string) error {
|
2022-09-04 04:43:52 +00:00
|
|
|
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
|
2022-09-03 14:32:09 +00:00
|
|
|
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
srcObj, err := GetUnwrap(ctx, storage, srcPath)
|
2022-06-15 12:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, "failed to get src object")
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
dstDir, err := GetUnwrap(ctx, storage, dstDirPath)
|
2022-08-31 12:58:57 +00:00
|
|
|
return errors.WithStack(storage.Copy(ctx, srcObj, dstDir))
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 06:45:39 +00:00
|
|
|
func Remove(ctx context.Context, storage driver.Driver, path string) error {
|
2022-09-04 04:43:52 +00:00
|
|
|
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
|
2022-09-03 14:32:09 +00:00
|
|
|
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
obj, err := GetUnwrap(ctx, storage, 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-09-14 12:28:52 +00:00
|
|
|
err = storage.Remove(ctx, obj)
|
|
|
|
if err == nil {
|
|
|
|
key := Key(storage, stdpath.Dir(path))
|
|
|
|
if objs, ok := listCache.Get(key); ok {
|
|
|
|
j := -1
|
|
|
|
for i, m := range objs {
|
|
|
|
if m.GetName() == obj.GetName() {
|
|
|
|
j = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if j >= 0 && j < len(objs) {
|
|
|
|
objs = append(objs[:j], objs[j+1:]...)
|
|
|
|
listCache.Set(key, objs)
|
|
|
|
} else {
|
|
|
|
log.Debugf("not found obj")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Debugf("not found parent cache")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.WithStack(err)
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 11:49:05 +00:00
|
|
|
func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file *model.FileStream, up driver.UpdateProgress) error {
|
2022-09-04 04:43:52 +00:00
|
|
|
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
|
2022-09-03 14:32:09 +00:00
|
|
|
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
|
|
|
}
|
2022-07-01 07:04:02 +00:00
|
|
|
defer func() {
|
|
|
|
if f, ok := file.GetReadCloser().(*os.File); ok {
|
|
|
|
err := os.RemoveAll(f.Name())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to remove file [%s]", f.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
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-07-30 12:04:21 +00:00
|
|
|
// if file exist and size = 0, delete it
|
|
|
|
dstPath := stdpath.Join(dstDirPath, file.GetName())
|
2022-12-17 11:49:05 +00:00
|
|
|
fi, err := GetUnwrap(ctx, storage, dstPath)
|
2022-07-30 12:04:21 +00:00
|
|
|
if err == nil {
|
|
|
|
if fi.GetSize() == 0 {
|
|
|
|
err = Remove(ctx, storage, dstPath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessagef(err, "failed remove file that exist and have size 0")
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
} else {
|
|
|
|
file.Old = fi
|
2022-07-30 12:04:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err = MakeDir(ctx, storage, 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-12-17 11:49:05 +00:00
|
|
|
parentDir, err := GetUnwrap(ctx, storage, 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-07-10 06:45:39 +00:00
|
|
|
err = storage.Put(ctx, parentDir, file, up)
|
2022-07-01 07:04:02 +00:00
|
|
|
log.Debugf("put file [%s] done", file.GetName())
|
2022-09-15 09:58:32 +00:00
|
|
|
//if err == nil {
|
|
|
|
// //clear cache
|
|
|
|
// key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath)
|
|
|
|
// listCache.Del(key)
|
|
|
|
//}
|
2022-08-31 12:58:57 +00:00
|
|
|
return errors.WithStack(err)
|
2022-06-10 13:00:51 +00:00
|
|
|
}
|