Skip to content

Commit

Permalink
refactor(rename): Reducing complexity of the method
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Jan 23, 2023
1 parent d9ad437 commit 57855a2
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions pkg/crud/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,20 @@ func (a App) doRename(ctx context.Context, oldPath, newPath string, oldItem abst
return newItem, nil
}

func (a App) Rename(w http.ResponseWriter, r *http.Request, request provider.Request) {
if !request.CanEdit {
a.error(w, r, request, model.WrapForbidden(ErrNotAuthorized))
return
}

func parseRenameParams(r *http.Request, request provider.Request) (string, string, string, string, bool, error) {
oldName, err := checkFormName(r, "name")
if err != nil && !errors.Is(err, ErrEmptyName) {
a.error(w, r, request, err)
return
return "", "", "", "", false, err
}

newFolder, err := getNewFolder(r)
if err != nil {
a.error(w, r, request, err)
return
return "", "", "", "", false, err
}

newName, err := getNewName(r)
if err != nil {
a.error(w, r, request, err)
return
return "", "", "", "", false, err
}

if strings.HasSuffix(oldName, "/") {
Expand All @@ -59,12 +51,27 @@ func (a App) Rename(w http.ResponseWriter, r *http.Request, request provider.Req

cover, err := getFormBool(r.Form.Get("cover"))
if err != nil {
a.error(w, r, request, err)
return
return "", "", "", "", false, err
}

oldPath := request.SubPath(oldName)
newPath := provider.GetPathname(newFolder, newName, request.Share)

return oldPath, newPath, newFolder, newName, cover, nil
}

func (a App) Rename(w http.ResponseWriter, r *http.Request, request provider.Request) {
if !request.CanEdit {
a.error(w, r, request, model.WrapForbidden(ErrNotAuthorized))
return
}

oldPath, newPath, newFolder, newName, cover, err := parseRenameParams(r, request)
if err != nil {
a.error(w, r, request, err)
return
}

ctx := r.Context()

var oldItem absto.Item
Expand Down

0 comments on commit 57855a2

Please sign in to comment.