feat: optimize file operation interface (#2757)

* feat: optimize file operation interface

* chore: fix typo

Co-authored-by: Noah Hsu <i@nn.ci>
This commit is contained in:
foxxorcat
2022-12-20 15:02:40 +08:00
committed by GitHub
parent e2bcca2fbd
commit 62a06fa0f9
13 changed files with 330 additions and 121 deletions

View File

@ -21,7 +21,7 @@ var CopyTaskManager = task.NewTaskManager(3, func(tid *uint64) {
// Copy if in the same storage, call move method
// if not, add copy task
func _copy(ctx context.Context, srcObjPath, dstDirPath string) (bool, error) {
func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (bool, error) {
srcStorage, srcObjActualPath, err := op.GetStorageAndActualPath(srcObjPath)
if err != nil {
return false, errors.WithMessage(err, "failed get src storage")
@ -32,7 +32,7 @@ func _copy(ctx context.Context, srcObjPath, dstDirPath string) (bool, error) {
}
// copy if in the same storage, just call driver.Copy
if srcStorage.GetStorage() == dstStorage.GetStorage() {
return false, op.Copy(ctx, srcStorage, srcObjActualPath, dstDirActualPath)
return false, op.Copy(ctx, srcStorage, srcObjActualPath, dstDirActualPath, lazyCache...)
}
// not in the same storage
CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
@ -95,5 +95,5 @@ func copyFileBetween2Storages(tsk *task.Task[uint64], srcStorage, dstStorage dri
if err != nil {
return errors.WithMessagef(err, "failed get [%s] stream", srcFilePath)
}
return op.Put(tsk.Ctx, dstStorage, dstDirPath, stream, tsk.SetProgress)
return op.Put(tsk.Ctx, dstStorage, dstDirPath, stream, tsk.SetProgress, true)
}

View File

@ -40,32 +40,32 @@ func Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, m
return res, file, nil
}
func MakeDir(ctx context.Context, path string) error {
err := makeDir(ctx, path)
func MakeDir(ctx context.Context, path string, lazyCache ...bool) error {
err := makeDir(ctx, path, lazyCache...)
if err != nil {
log.Errorf("failed make dir %s: %+v", path, err)
}
return err
}
func Move(ctx context.Context, srcPath, dstDirPath string) error {
err := move(ctx, srcPath, dstDirPath)
func Move(ctx context.Context, srcPath, dstDirPath string, lazyCache ...bool) error {
err := move(ctx, srcPath, dstDirPath, lazyCache...)
if err != nil {
log.Errorf("failed move %s to %s: %+v", srcPath, dstDirPath, err)
}
return err
}
func Copy(ctx context.Context, srcObjPath, dstDirPath string) (bool, error) {
res, err := _copy(ctx, srcObjPath, dstDirPath)
func Copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (bool, error) {
res, err := _copy(ctx, srcObjPath, dstDirPath, lazyCache...)
if err != nil {
log.Errorf("failed copy %s to %s: %+v", srcObjPath, dstDirPath, err)
}
return res, err
}
func Rename(ctx context.Context, srcPath, dstName string) error {
err := rename(ctx, srcPath, dstName)
func Rename(ctx context.Context, srcPath, dstName string, lazyCache ...bool) error {
err := rename(ctx, srcPath, dstName, lazyCache...)
if err != nil {
log.Errorf("failed rename %s to %s: %+v", srcPath, dstName, err)
}
@ -80,8 +80,8 @@ func Remove(ctx context.Context, path string) error {
return err
}
func PutDirectly(ctx context.Context, dstDirPath string, file *model.FileStream) error {
err := putDirectly(ctx, dstDirPath, file)
func PutDirectly(ctx context.Context, dstDirPath string, file *model.FileStream, lazyCache ...bool) error {
err := putDirectly(ctx, dstDirPath, file, lazyCache...)
if err != nil {
log.Errorf("failed put %s: %+v", dstDirPath, err)
}

View File

@ -9,15 +9,15 @@ import (
"github.com/pkg/errors"
)
func makeDir(ctx context.Context, path string) error {
func makeDir(ctx context.Context, path string, lazyCache ...bool) error {
storage, actualPath, err := op.GetStorageAndActualPath(path)
if err != nil {
return errors.WithMessage(err, "failed get storage")
}
return op.MakeDir(ctx, storage, actualPath)
return op.MakeDir(ctx, storage, actualPath, lazyCache...)
}
func move(ctx context.Context, srcPath, dstDirPath string) error {
func move(ctx context.Context, srcPath, dstDirPath string, lazyCache ...bool) error {
srcStorage, srcActualPath, err := op.GetStorageAndActualPath(srcPath)
if err != nil {
return errors.WithMessage(err, "failed get src storage")
@ -29,15 +29,15 @@ func move(ctx context.Context, srcPath, dstDirPath string) error {
if srcStorage.GetStorage() != dstStorage.GetStorage() {
return errors.WithStack(errs.MoveBetweenTwoStorages)
}
return op.Move(ctx, srcStorage, srcActualPath, dstDirActualPath)
return op.Move(ctx, srcStorage, srcActualPath, dstDirActualPath, lazyCache...)
}
func rename(ctx context.Context, srcPath, dstName string) error {
func rename(ctx context.Context, srcPath, dstName string, lazyCache ...bool) error {
storage, srcActualPath, err := op.GetStorageAndActualPath(srcPath)
if err != nil {
return errors.WithMessage(err, "failed get storage")
}
return op.Rename(ctx, storage, srcActualPath, dstName)
return op.Rename(ctx, storage, srcActualPath, dstName, lazyCache...)
}
func remove(ctx context.Context, path string) error {

View File

@ -36,14 +36,14 @@ func putAsTask(dstDirPath string, file *model.FileStream) error {
UploadTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
Name: fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), storage.GetStorage().MountPath, dstDirActualPath),
Func: func(task *task.Task[uint64]) error {
return op.Put(task.Ctx, storage, dstDirActualPath, file, nil)
return op.Put(task.Ctx, storage, dstDirActualPath, file, nil, true)
},
}))
return nil
}
// putDirect put the file and return after finish
func putDirectly(ctx context.Context, dstDirPath string, file *model.FileStream) error {
func putDirectly(ctx context.Context, dstDirPath string, file *model.FileStream, lazyCache ...bool) error {
storage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
if err != nil {
return errors.WithMessage(err, "failed get storage")
@ -51,5 +51,5 @@ func putDirectly(ctx context.Context, dstDirPath string, file *model.FileStream)
if storage.Config().NoUpload {
return errors.WithStack(errs.UploadNotSupported)
}
return op.Put(ctx, storage, dstDirActualPath, file, nil)
return op.Put(ctx, storage, dstDirActualPath, file, nil, lazyCache...)
}