mirror of https://github.com/Xhofe/alist
perf: extract fs func and add error log
parent
40b7ecc845
commit
956a5ae906
|
@ -99,7 +99,6 @@ var TransferTaskManager = task.NewTaskManager[uint64](3, func(k *uint64) {
|
|||
func (m *Monitor) Complete() error {
|
||||
// check dstDir again
|
||||
account, dstDirActualPath, err := operations.GetAccountAndActualPath(m.dstDirPath)
|
||||
println("dstDirActualPath:", dstDirActualPath)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get account")
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ var CopyTaskManager = task.NewTaskManager[uint64](3, func(tid *uint64) {
|
|||
|
||||
// Copy if in an account, call move method
|
||||
// if not, add copy task
|
||||
func Copy(ctx context.Context, account driver.Driver, srcObjPath, dstDirPath string) (bool, error) {
|
||||
func _copy(ctx context.Context, srcObjPath, dstDirPath string) (bool, error) {
|
||||
srcAccount, srcObjActualPath, err := operations.GetAccountAndActualPath(srcObjPath)
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "failed get src account")
|
||||
|
@ -32,19 +32,19 @@ func Copy(ctx context.Context, account driver.Driver, srcObjPath, dstDirPath str
|
|||
}
|
||||
// copy if in an account, just call driver.Copy
|
||||
if srcAccount.GetAccount() == dstAccount.GetAccount() {
|
||||
return false, operations.Copy(ctx, account, srcObjActualPath, dstDirActualPath)
|
||||
return false, operations.Copy(ctx, srcAccount, srcObjActualPath, dstDirActualPath)
|
||||
}
|
||||
// not in an account
|
||||
CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
|
||||
Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcObjActualPath, dstAccount.GetAccount().VirtualPath, dstDirActualPath),
|
||||
Func: func(task *task.Task[uint64]) error {
|
||||
return CopyBetween2Accounts(task, srcAccount, dstAccount, srcObjActualPath, dstDirActualPath)
|
||||
return copyBetween2Accounts(task, srcAccount, dstAccount, srcObjActualPath, dstDirActualPath)
|
||||
},
|
||||
}))
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func CopyBetween2Accounts(t *task.Task[uint64], srcAccount, dstAccount driver.Driver, srcObjPath, dstDirPath string) error {
|
||||
func copyBetween2Accounts(t *task.Task[uint64], srcAccount, dstAccount driver.Driver, srcObjPath, dstDirPath string) error {
|
||||
t.SetStatus("getting src object")
|
||||
srcObj, err := operations.Get(t.Ctx, srcAccount, srcObjPath)
|
||||
if err != nil {
|
||||
|
@ -65,7 +65,7 @@ func CopyBetween2Accounts(t *task.Task[uint64], srcAccount, dstAccount driver.Dr
|
|||
CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
|
||||
Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcObjPath, dstAccount.GetAccount().VirtualPath, dstObjPath),
|
||||
Func: func(t *task.Task[uint64]) error {
|
||||
return CopyBetween2Accounts(t, srcAccount, dstAccount, srcObjPath, dstObjPath)
|
||||
return copyBetween2Accounts(t, srcAccount, dstAccount, srcObjPath, dstObjPath)
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
@ -73,14 +73,14 @@ func CopyBetween2Accounts(t *task.Task[uint64], srcAccount, dstAccount driver.Dr
|
|||
CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
|
||||
Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcObjPath, dstAccount.GetAccount().VirtualPath, dstDirPath),
|
||||
Func: func(t *task.Task[uint64]) error {
|
||||
return CopyFileBetween2Accounts(t, srcAccount, dstAccount, srcObjPath, dstDirPath)
|
||||
return copyFileBetween2Accounts(t, srcAccount, dstAccount, srcObjPath, dstDirPath)
|
||||
},
|
||||
}))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CopyFileBetween2Accounts(tsk *task.Task[uint64], srcAccount, dstAccount driver.Driver, srcFilePath, dstDirPath string) error {
|
||||
func copyFileBetween2Accounts(tsk *task.Task[uint64], srcAccount, dstAccount driver.Driver, srcFilePath, dstDirPath string) error {
|
||||
srcFile, err := operations.Get(tsk.Ctx, srcAccount, srcFilePath)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "failed get src [%s] file", srcFilePath)
|
||||
|
|
|
@ -1,5 +1,86 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// the param named path of functions in this package is a virtual path
|
||||
// So, the purpose of this package is to convert virtual path to actual path
|
||||
// then pass the actual path to the operations package
|
||||
|
||||
func List(ctx context.Context, path string) ([]model.Obj, error) {
|
||||
res, err := list(ctx, path)
|
||||
if err != nil {
|
||||
log.Errorf("failed list %s: %+v", path, err)
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func Get(ctx context.Context, path string) (model.Obj, error) {
|
||||
res, err := get(ctx, path)
|
||||
if err != nil {
|
||||
log.Errorf("failed get %s: %+v", path, err)
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error) {
|
||||
res, err := link(ctx, path, args)
|
||||
if err != nil {
|
||||
log.Errorf("failed link %s: %+v", path, err)
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func MakeDir(ctx context.Context, path string) error {
|
||||
err := makeDir(ctx, path)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
if err != nil {
|
||||
log.Errorf("failed rename %s to %s: %+v", srcPath, dstName, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func Remove(ctx context.Context, path string) error {
|
||||
err := remove(ctx, path)
|
||||
if err != nil {
|
||||
log.Errorf("failed remove %s: %+v", path, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func Put(ctx context.Context, dstDirPath string, file model.FileStreamer) error {
|
||||
err := put(ctx, dstDirPath, file)
|
||||
if err != nil {
|
||||
log.Errorf("failed put %s: %+v", dstDirPath, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/internal/operations"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
"github.com/pkg/errors"
|
||||
stdpath "path"
|
||||
"time"
|
||||
)
|
||||
|
||||
func get(ctx context.Context, path string) (model.Obj, error) {
|
||||
path = utils.StandardizePath(path)
|
||||
// maybe a virtual file
|
||||
if path != "/" {
|
||||
virtualFiles := operations.GetAccountVirtualFilesByPath(stdpath.Dir(path))
|
||||
for _, f := range virtualFiles {
|
||||
if f.GetName() == stdpath.Base(path) {
|
||||
return f, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
account, actualPath, err := operations.GetAccountAndActualPath(path)
|
||||
if err != nil {
|
||||
// if there are no account prefix with path, maybe root folder
|
||||
if path == "/" {
|
||||
return model.Object{
|
||||
Name: "root",
|
||||
Size: 0,
|
||||
Modified: time.Time{},
|
||||
IsFolder: true,
|
||||
}, nil
|
||||
}
|
||||
return nil, errors.WithMessage(err, "failed get account")
|
||||
}
|
||||
return operations.Get(ctx, account, actualPath)
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/internal/operations"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error) {
|
||||
account, actualPath, err := operations.GetAccountAndActualPath(path)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "failed get account")
|
||||
}
|
||||
return operations.Link(ctx, account, actualPath, args)
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/internal/operations"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// List files
|
||||
// TODO: hide
|
||||
// TODO: sort
|
||||
func list(ctx context.Context, path string) ([]model.Obj, error) {
|
||||
account, actualPath, err := operations.GetAccountAndActualPath(path)
|
||||
virtualFiles := operations.GetAccountVirtualFilesByPath(path)
|
||||
if err != nil {
|
||||
if len(virtualFiles) != 0 {
|
||||
return virtualFiles, nil
|
||||
}
|
||||
return nil, errors.WithMessage(err, "failed get account")
|
||||
}
|
||||
files, err := operations.List(ctx, account, actualPath)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
if len(virtualFiles) != 0 {
|
||||
return virtualFiles, nil
|
||||
}
|
||||
return nil, errors.WithMessage(err, "failed get files")
|
||||
}
|
||||
for _, accountFile := range virtualFiles {
|
||||
if !containsByName(files, accountFile) {
|
||||
files = append(files, accountFile)
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
|
@ -3,7 +3,6 @@ package fs
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/alist-org/alist/v3/internal/driver"
|
||||
"github.com/alist-org/alist/v3/internal/errs"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/internal/operations"
|
||||
|
@ -17,7 +16,7 @@ var UploadTaskManager = task.NewTaskManager[uint64](3, func(tid *uint64) {
|
|||
})
|
||||
|
||||
// Put add as a put task
|
||||
func Put(ctx context.Context, account driver.Driver, dstDirPath string, file model.FileStreamer) error {
|
||||
func put(ctx context.Context, dstDirPath string, file model.FileStreamer) error {
|
||||
account, dstDirActualPath, err := operations.GetAccountAndActualPath(dstDirPath)
|
||||
if account.Config().NoUpload {
|
||||
return errors.WithStack(errs.UploadNotSupported)
|
||||
|
|
|
@ -1,76 +0,0 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"context"
|
||||
stdpath "path"
|
||||
"time"
|
||||
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/internal/operations"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// List files
|
||||
// TODO: hide
|
||||
// TODO: sort
|
||||
func List(ctx context.Context, path string) ([]model.Obj, error) {
|
||||
account, actualPath, err := operations.GetAccountAndActualPath(path)
|
||||
virtualFiles := operations.GetAccountVirtualFilesByPath(path)
|
||||
if err != nil {
|
||||
if len(virtualFiles) != 0 {
|
||||
return virtualFiles, nil
|
||||
}
|
||||
return nil, errors.WithMessage(err, "failed get account")
|
||||
}
|
||||
files, err := operations.List(ctx, account, actualPath)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
if len(virtualFiles) != 0 {
|
||||
return virtualFiles, nil
|
||||
}
|
||||
return nil, errors.WithMessage(err, "failed get files")
|
||||
}
|
||||
for _, accountFile := range virtualFiles {
|
||||
if !containsByName(files, accountFile) {
|
||||
files = append(files, accountFile)
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func Get(ctx context.Context, path string) (model.Obj, error) {
|
||||
path = utils.StandardizePath(path)
|
||||
// maybe a virtual file
|
||||
if path != "/" {
|
||||
virtualFiles := operations.GetAccountVirtualFilesByPath(stdpath.Dir(path))
|
||||
for _, f := range virtualFiles {
|
||||
if f.GetName() == stdpath.Base(path) {
|
||||
return f, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
account, actualPath, err := operations.GetAccountAndActualPath(path)
|
||||
if err != nil {
|
||||
// if there are no account prefix with path, maybe root folder
|
||||
if path == "/" {
|
||||
return model.Object{
|
||||
Name: "root",
|
||||
Size: 0,
|
||||
Modified: time.Time{},
|
||||
IsFolder: true,
|
||||
}, nil
|
||||
}
|
||||
return nil, errors.WithMessage(err, "failed get account")
|
||||
}
|
||||
return operations.Get(ctx, account, actualPath)
|
||||
}
|
||||
|
||||
func Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error) {
|
||||
account, actualPath, err := operations.GetAccountAndActualPath(path)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "failed get account")
|
||||
}
|
||||
return operations.Link(ctx, account, actualPath, args)
|
||||
}
|
|
@ -2,13 +2,12 @@ package fs
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/alist-org/alist/v3/internal/driver"
|
||||
"github.com/alist-org/alist/v3/internal/errs"
|
||||
"github.com/alist-org/alist/v3/internal/operations"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func MakeDir(ctx context.Context, account driver.Driver, path string) error {
|
||||
func makeDir(ctx context.Context, path string) error {
|
||||
account, actualPath, err := operations.GetAccountAndActualPath(path)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get account")
|
||||
|
@ -16,7 +15,7 @@ func MakeDir(ctx context.Context, account driver.Driver, path string) error {
|
|||
return operations.MakeDir(ctx, account, actualPath)
|
||||
}
|
||||
|
||||
func Move(ctx context.Context, account driver.Driver, srcPath, dstDirPath string) error {
|
||||
func move(ctx context.Context, srcPath, dstDirPath string) error {
|
||||
srcAccount, srcActualPath, err := operations.GetAccountAndActualPath(srcPath)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get src account")
|
||||
|
@ -28,10 +27,10 @@ func Move(ctx context.Context, account driver.Driver, srcPath, dstDirPath string
|
|||
if srcAccount.GetAccount() != dstAccount.GetAccount() {
|
||||
return errors.WithStack(errs.MoveBetweenTwoAccounts)
|
||||
}
|
||||
return operations.Move(ctx, account, srcActualPath, dstDirActualPath)
|
||||
return operations.Move(ctx, srcAccount, srcActualPath, dstDirActualPath)
|
||||
}
|
||||
|
||||
func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string) error {
|
||||
func rename(ctx context.Context, srcPath, dstName string) error {
|
||||
account, srcActualPath, err := operations.GetAccountAndActualPath(srcPath)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get account")
|
||||
|
@ -39,7 +38,7 @@ func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string)
|
|||
return operations.Rename(ctx, account, srcActualPath, dstName)
|
||||
}
|
||||
|
||||
func Remove(ctx context.Context, account driver.Driver, path string) error {
|
||||
func remove(ctx context.Context, path string) error {
|
||||
account, actualPath, err := operations.GetAccountAndActualPath(path)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get account")
|
||||
|
|
Loading…
Reference in New Issue