feat(alias): support Rename and Remove (#6478)
* feat(alias): support `Rename` and `Remove` * fix(alias): `autoFlatten` not updated after editing * feat(alias): add `protect_same_name` option
This commit is contained in:
parent
bbe3d4e19f
commit
037850bbd5
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"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/errs"
|
||||||
|
"github.com/alist-org/alist/v3/internal/fs"
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
)
|
)
|
||||||
@ -45,6 +46,9 @@ func (d *Alias) Init(ctx context.Context) error {
|
|||||||
d.oneKey = k
|
d.oneKey = k
|
||||||
}
|
}
|
||||||
d.autoFlatten = true
|
d.autoFlatten = true
|
||||||
|
} else {
|
||||||
|
d.oneKey = ""
|
||||||
|
d.autoFlatten = false
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -111,4 +115,26 @@ func (d *Alias) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (
|
|||||||
return nil, errs.ObjectNotFound
|
return nil, errs.ObjectNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Alias) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
|
||||||
|
reqPath, err := d.getReqPath(ctx, srcObj)
|
||||||
|
if err == nil {
|
||||||
|
return fs.Rename(ctx, *reqPath, newName)
|
||||||
|
}
|
||||||
|
if errs.IsNotImplement(err) {
|
||||||
|
return errors.New("same-name files cannot be Rename")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Alias) Remove(ctx context.Context, obj model.Obj) error {
|
||||||
|
reqPath, err := d.getReqPath(ctx, obj)
|
||||||
|
if err == nil {
|
||||||
|
return fs.Remove(ctx, *reqPath)
|
||||||
|
}
|
||||||
|
if errs.IsNotImplement(err) {
|
||||||
|
return errors.New("same-name files cannot be Delete")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
var _ driver.Driver = (*Alias)(nil)
|
var _ driver.Driver = (*Alias)(nil)
|
||||||
|
@ -9,7 +9,8 @@ type Addition struct {
|
|||||||
// Usually one of two
|
// Usually one of two
|
||||||
// driver.RootPath
|
// driver.RootPath
|
||||||
// define other
|
// define other
|
||||||
Paths string `json:"paths" required:"true" type:"text"`
|
Paths string `json:"paths" required:"true" type:"text"`
|
||||||
|
ProtectSameName bool `json:"protect_same_name" default:"true" required:"false" help:"Protects same-name files from Delete or Rename"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var config = driver.Config{
|
var config = driver.Config{
|
||||||
@ -22,6 +23,10 @@ var config = driver.Config{
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
op.RegisterDriver(func() driver.Driver {
|
op.RegisterDriver(func() driver.Driver {
|
||||||
return &Alias{}
|
return &Alias{
|
||||||
|
Addition: Addition{
|
||||||
|
ProtectSameName: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
stdpath "path"
|
stdpath "path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/alist-org/alist/v3/internal/errs"
|
||||||
"github.com/alist-org/alist/v3/internal/fs"
|
"github.com/alist-org/alist/v3/internal/fs"
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/internal/sign"
|
"github.com/alist-org/alist/v3/internal/sign"
|
||||||
@ -112,3 +113,35 @@ func (d *Alias) link(ctx context.Context, dst, sub string, args model.LinkArgs)
|
|||||||
link, _, err := fs.Link(ctx, reqPath, args)
|
link, _, err := fs.Link(ctx, reqPath, args)
|
||||||
return link, err
|
return link, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Alias) getReqPath(ctx context.Context, obj model.Obj) (*string, error) {
|
||||||
|
root, sub := d.getRootAndPath(obj.GetPath())
|
||||||
|
if sub == "" || sub == "/" {
|
||||||
|
return nil, errs.NotSupport
|
||||||
|
}
|
||||||
|
dsts, ok := d.pathMap[root]
|
||||||
|
if !ok {
|
||||||
|
return nil, errs.ObjectNotFound
|
||||||
|
}
|
||||||
|
var reqPath string
|
||||||
|
var err error
|
||||||
|
for _, dst := range dsts {
|
||||||
|
reqPath = stdpath.Join(dst, sub)
|
||||||
|
_, err = fs.Get(ctx, reqPath, &fs.GetArgs{NoLog: true})
|
||||||
|
if err == nil {
|
||||||
|
if d.ProtectSameName {
|
||||||
|
if ok {
|
||||||
|
ok = false
|
||||||
|
} else {
|
||||||
|
return nil, errs.NotImplement
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, errs.ObjectNotFound
|
||||||
|
}
|
||||||
|
return &reqPath, nil
|
||||||
|
}
|
||||||
|
@ -3,6 +3,7 @@ package errs
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
pkgerr "github.com/pkg/errors"
|
pkgerr "github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,3 +34,6 @@ func IsNotFoundError(err error) bool {
|
|||||||
func IsNotSupportError(err error) bool {
|
func IsNotSupportError(err error) bool {
|
||||||
return errors.Is(pkgerr.Cause(err), NotSupport)
|
return errors.Is(pkgerr.Cause(err), NotSupport)
|
||||||
}
|
}
|
||||||
|
func IsNotImplement(err error) bool {
|
||||||
|
return errors.Is(pkgerr.Cause(err), NotImplement)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user