mirror of https://github.com/Xhofe/alist
chore: move errors
parent
2612cd7f1c
commit
c3040fdfc3
|
@ -4,8 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/alist-org/alist/v3/conf"
|
"github.com/alist-org/alist/v3/conf"
|
||||||
"github.com/alist-org/alist/v3/internal/driver"
|
"github.com/alist-org/alist/v3/internal/errs"
|
||||||
"github.com/alist-org/alist/v3/internal/fs"
|
|
||||||
"github.com/alist-org/alist/v3/internal/operations"
|
"github.com/alist-org/alist/v3/internal/operations"
|
||||||
"github.com/alist-org/alist/v3/pkg/task"
|
"github.com/alist-org/alist/v3/pkg/task"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -21,18 +20,18 @@ func AddURI(ctx context.Context, uri string, dstDirPath string) error {
|
||||||
}
|
}
|
||||||
// check is it could upload
|
// check is it could upload
|
||||||
if account.Config().NoUpload {
|
if account.Config().NoUpload {
|
||||||
return errors.WithStack(fs.ErrUploadNotSupported)
|
return errors.WithStack(errs.ErrUploadNotSupported)
|
||||||
}
|
}
|
||||||
// check path is valid
|
// check path is valid
|
||||||
obj, err := operations.Get(ctx, account, dstDirActualPath)
|
obj, err := operations.Get(ctx, account, dstDirActualPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(errors.Cause(err), driver.ErrorObjectNotFound) {
|
if !errs.IsErrObjectNotFound(err) {
|
||||||
return errors.WithMessage(err, "failed get object")
|
return errors.WithMessage(err, "failed get object")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !obj.IsDir() {
|
if !obj.IsDir() {
|
||||||
// can't add to a file
|
// can't add to a file
|
||||||
return errors.WithStack(fs.ErrNotFolder)
|
return errors.WithStack(errs.ErrNotFolder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// call aria2 rpc
|
// call aria2 rpc
|
||||||
|
@ -44,8 +43,7 @@ func AddURI(ctx context.Context, uri string, dstDirPath string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to add uri %s", uri)
|
return errors.Wrapf(err, "failed to add uri %s", uri)
|
||||||
}
|
}
|
||||||
// TODO add to task manager
|
downTaskManager.Submit(task.WithCancelCtx(&task.Task[string]{
|
||||||
TaskManager.Submit(task.WithCancelCtx(&task.Task[string]{
|
|
||||||
ID: gid,
|
ID: gid,
|
||||||
Name: fmt.Sprintf("download %s to [%s](%s)", uri, account.GetAccount().VirtualPath, dstDirActualPath),
|
Name: fmt.Sprintf("download %s to [%s](%s)", uri, account.GetAccount().VirtualPath, dstDirActualPath),
|
||||||
Func: func(tsk *task.Task[string]) error {
|
Func: func(tsk *task.Task[string]) error {
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var TaskManager = task.NewTaskManager[string](3)
|
var downTaskManager = task.NewTaskManager[string](3)
|
||||||
var notify = NewNotify()
|
var notify = NewNotify()
|
||||||
var client rpc.Client
|
var client rpc.Client
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
package driver
|
package errs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
pkgerr "github.com/pkg/errors"
|
pkgerr "github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -11,6 +10,10 @@ var (
|
||||||
ErrNotImplement = errors.New("not implement")
|
ErrNotImplement = errors.New("not implement")
|
||||||
ErrNotSupport = errors.New("not support")
|
ErrNotSupport = errors.New("not support")
|
||||||
ErrRelativePath = errors.New("access using relative path is not allowed")
|
ErrRelativePath = errors.New("access using relative path is not allowed")
|
||||||
|
|
||||||
|
ErrMoveBetweenTwoAccounts = errors.New("can't move files between two account, try to copy")
|
||||||
|
ErrUploadNotSupported = errors.New("upload not supported")
|
||||||
|
ErrNotFolder = errors.New("not a folder")
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsErrObjectNotFound(err error) bool {
|
func IsErrObjectNotFound(err error) bool {
|
|
@ -1,9 +0,0 @@
|
||||||
package fs
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrMoveBetweenTwoAccounts = errors.New("can't move files between two account, try to copy")
|
|
||||||
ErrUploadNotSupported = errors.New("upload not supported")
|
|
||||||
ErrNotFolder = errors.New("not a folder")
|
|
||||||
)
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/alist-org/alist/v3/internal/driver"
|
"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/model"
|
||||||
"github.com/alist-org/alist/v3/internal/operations"
|
"github.com/alist-org/alist/v3/internal/operations"
|
||||||
"github.com/alist-org/alist/v3/pkg/task"
|
"github.com/alist-org/alist/v3/pkg/task"
|
||||||
|
@ -19,7 +20,7 @@ var UploadTaskManager = task.NewTaskManager[uint64](3, func(tid *uint64) {
|
||||||
func Put(ctx context.Context, account driver.Driver, dstDirPath string, file model.FileStreamer) error {
|
func Put(ctx context.Context, account driver.Driver, dstDirPath string, file model.FileStreamer) error {
|
||||||
account, dstDirActualPath, err := operations.GetAccountAndActualPath(dstDirPath)
|
account, dstDirActualPath, err := operations.GetAccountAndActualPath(dstDirPath)
|
||||||
if account.Config().NoUpload {
|
if account.Config().NoUpload {
|
||||||
return errors.WithStack(ErrUploadNotSupported)
|
return errors.WithStack(errs.ErrUploadNotSupported)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithMessage(err, "failed get account")
|
return errors.WithMessage(err, "failed get account")
|
||||||
|
|
|
@ -3,6 +3,7 @@ package fs
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/alist-org/alist/v3/internal/driver"
|
"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/alist-org/alist/v3/internal/operations"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
@ -25,7 +26,7 @@ func Move(ctx context.Context, account driver.Driver, srcPath, dstDirPath string
|
||||||
return errors.WithMessage(err, "failed get dst account")
|
return errors.WithMessage(err, "failed get dst account")
|
||||||
}
|
}
|
||||||
if srcAccount.GetAccount() != dstAccount.GetAccount() {
|
if srcAccount.GetAccount() != dstAccount.GetAccount() {
|
||||||
return errors.WithStack(ErrMoveBetweenTwoAccounts)
|
return errors.WithStack(errs.ErrMoveBetweenTwoAccounts)
|
||||||
}
|
}
|
||||||
return operations.Move(ctx, account, srcActualPath, dstDirActualPath)
|
return operations.Move(ctx, account, srcActualPath, dstDirActualPath)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
package operations
|
|
||||||
|
|
||||||
var ()
|
|
|
@ -2,6 +2,7 @@ package operations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/alist-org/alist/v3/internal/errs"
|
||||||
stdpath "path"
|
stdpath "path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -85,7 +86,7 @@ func Get(ctx context.Context, account driver.Driver, path string) (model.Obj, er
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, errors.WithStack(driver.ErrorObjectNotFound)
|
return nil, errors.WithStack(errs.ErrorObjectNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
var linkCache = cache.NewMemCache(cache.WithShards[*model.Link](16))
|
var linkCache = cache.NewMemCache(cache.WithShards[*model.Link](16))
|
||||||
|
@ -102,6 +103,9 @@ func Link(ctx context.Context, account driver.Driver, path string, args model.Li
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.WithMessage(err, "failed to get file")
|
return nil, errors.WithMessage(err, "failed to get file")
|
||||||
}
|
}
|
||||||
|
if file.IsDir() {
|
||||||
|
return nil, errors.New("file is dir")
|
||||||
|
}
|
||||||
link, err := account.Link(ctx, file, args)
|
link, err := account.Link(ctx, file, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.WithMessage(err, "failed get link")
|
return nil, errors.WithMessage(err, "failed get link")
|
||||||
|
@ -119,7 +123,7 @@ func MakeDir(ctx context.Context, account driver.Driver, path string) error {
|
||||||
// check if dir exists
|
// check if dir exists
|
||||||
f, err := Get(ctx, account, path)
|
f, err := Get(ctx, account, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if driver.IsErrObjectNotFound(err) {
|
if errs.IsErrObjectNotFound(err) {
|
||||||
parentPath, dirName := stdpath.Split(path)
|
parentPath, dirName := stdpath.Split(path)
|
||||||
err = MakeDir(ctx, account, parentPath)
|
err = MakeDir(ctx, account, parentPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -179,7 +183,7 @@ func Remove(ctx context.Context, account driver.Driver, path string) error {
|
||||||
obj, err := Get(ctx, account, path)
|
obj, err := Get(ctx, account, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// if object not found, it's ok
|
// if object not found, it's ok
|
||||||
if driver.IsErrObjectNotFound(err) {
|
if errs.IsErrObjectNotFound(err) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.WithMessage(err, "failed to get object")
|
return errors.WithMessage(err, "failed to get object")
|
||||||
|
|
Loading…
Reference in New Issue