Skip to content

Commit

Permalink
feat(alias): support Rename and Remove (#6478)
Browse files Browse the repository at this point in the history
* feat(alias): support `Rename` and `Remove`

* fix(alias): `autoFlatten` not updated after editing

* feat(alias): add `protect_same_name` option
  • Loading branch information
j2rong4cn authored May 22, 2024
1 parent bbe3d4e commit 037850b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
26 changes: 26 additions & 0 deletions drivers/alias/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
9 changes: 7 additions & 2 deletions drivers/alias/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ type Addition struct {
// Usually one of two
// driver.RootPath
// 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{
Expand All @@ -22,6 +23,10 @@ var config = driver.Config{

func init() {
op.RegisterDriver(func() driver.Driver {
return &Alias{}
return &Alias{
Addition: Addition{
ProtectSameName: true,
},
}
})
}
33 changes: 33 additions & 0 deletions drivers/alias/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions internal/errs/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errs
import (
"errors"
"fmt"

pkgerr "github.com/pkg/errors"
)

Expand Down Expand Up @@ -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)
}

1 comment on commit 037850b

@ww198610
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既然支持了重命名和移为,为啥不支持全部功能,比如上传,删除

Please sign in to comment.