From 1ce3068a99c80c153fd41359255d173bce6e79e8 Mon Sep 17 00:00:00 2001 From: Ramires Viana <59319979+ramiresviana@users.noreply.github.com> Date: Tue, 3 Nov 2020 12:30:56 +0000 Subject: [PATCH] fix: resource rename action invalid path --- http/resource.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/http/resource.go b/http/resource.go index e223bc79..daeb344b 100644 --- a/http/resource.go +++ b/http/resource.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "os" + "path" "path/filepath" "strings" @@ -122,7 +123,7 @@ var resourcePostPutHandler = withUser(func(w http.ResponseWriter, r *http.Reques } err := d.RunHook(func() error { - dir, _ := filepath.Split(r.URL.Path) + dir, _ := path.Split(r.URL.Path) err := d.user.Fs.MkdirAll(dir, 0775) if err != nil { return err @@ -196,7 +197,8 @@ var resourcePatchHandler = withUser(func(w http.ResponseWriter, r *http.Request, if !d.user.Perm.Rename { return errors.ErrPermissionDenied } - dst = filepath.Clean("/" + dst) + src = path.Clean("/" + src) + dst = path.Clean("/" + dst) return d.user.Fs.Rename(src, dst) default: @@ -221,20 +223,20 @@ func checkParent(src, dst string) error { return nil } -func addVersionSuffix(path string, fs afero.Fs) string { +func addVersionSuffix(source string, fs afero.Fs) string { counter := 1 - dir, name := filepath.Split(path) + dir, name := path.Split(source) ext := filepath.Ext(name) base := strings.TrimSuffix(name, ext) for { - if _, err := fs.Stat(path); err != nil { + if _, err := fs.Stat(source); err != nil { break } renamed := fmt.Sprintf("%s(%d)%s", base, counter, ext) - path = filepath.ToSlash(dir) + renamed + source = path.Join(dir, renamed) counter++ } - return path + return source }