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
pull/6496/head
j2rong4cn 2024-05-22 09:27:48 +08:00 committed by GitHub
parent bbe3d4e19f
commit 037850bbd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 70 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
"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/model"
"github.com/alist-org/alist/v3/pkg/utils"
)
@ -45,6 +46,9 @@ func (d *Alias) Init(ctx context.Context) error {
d.oneKey = k
}
d.autoFlatten = true
} else {
d.oneKey = ""
d.autoFlatten = false
}
return nil
}
@ -111,4 +115,26 @@ func (d *Alias) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (
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)

View File

@ -10,6 +10,7 @@ type Addition struct {
// driver.RootPath
// define other
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{
@ -22,6 +23,10 @@ var config = driver.Config{
func init() {
op.RegisterDriver(func() driver.Driver {
return &Alias{}
return &Alias{
Addition: Addition{
ProtectSameName: true,
},
}
})
}

View File

@ -6,6 +6,7 @@ import (
stdpath "path"
"strings"
"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/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)
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
}

View File

@ -3,6 +3,7 @@ package errs
import (
"errors"
"fmt"
pkgerr "github.com/pkg/errors"
)
@ -33,3 +34,6 @@ func IsNotFoundError(err error) bool {
func IsNotSupportError(err error) bool {
return errors.Is(pkgerr.Cause(err), NotSupport)
}
func IsNotImplement(err error) bool {
return errors.Is(pkgerr.Cause(err), NotImplement)
}