From b92152693f1c35ffecd829e4c8982f63cc4f65c1 Mon Sep 17 00:00:00 2001 From: Ramires Viana <59319979+ramiresviana@users.noreply.github.com> Date: Fri, 16 Apr 2021 12:04:06 +0000 Subject: [PATCH] chore: split action on resource patch handler --- http/resource.go | 82 ++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/http/resource.go b/http/resource.go index 1b4e180e..12ba126f 100644 --- a/http/resource.go +++ b/http/resource.go @@ -195,7 +195,9 @@ func resourcePatchHandler(fileCache FileCache) handleFunc { if dst == "/" || src == "/" { return http.StatusForbidden, nil } - if err = checkParent(src, dst); err != nil { + + err = checkParent(src, dst) + if err != nil { return http.StatusBadRequest, err } @@ -215,44 +217,8 @@ func resourcePatchHandler(fileCache FileCache) handleFunc { return http.StatusForbidden, nil } - file, err := files.NewFileInfo(files.FileOptions{ - Fs: d.user.Fs, - Path: r.URL.Path, - Modify: d.user.Perm.Modify, - Expand: false, - ReadHeader: d.server.TypeDetectionByHeader, - Checker: d, - }) - if err != nil { - return errToStatus(err), err - } - err = d.RunHook(func() error { - switch action { - // TODO: use enum - case "copy": - if !d.user.Perm.Create { - return errors.ErrPermissionDenied - } - - return fileutils.Copy(d.user.Fs, src, dst) - case "rename": - if !d.user.Perm.Rename { - return errors.ErrPermissionDenied - } - src = path.Clean("/" + src) - dst = path.Clean("/" + dst) - - // delete thumbnails - err = delThumbs(r.Context(), fileCache, file) - if err != nil { - return err - } - - return fileutils.MoveFile(d.user.Fs, src, dst) - default: - return fmt.Errorf("unsupported action %s: %w", action, errors.ErrInvalidRequestParams) - } + return patchAction(r.Context(), action, src, dst, d, fileCache) }, action, src, dst, d.user) return errToStatus(err), err @@ -328,3 +294,43 @@ func delThumbs(ctx context.Context, fileCache FileCache, file *files.FileInfo) e return nil } + +func patchAction(ctx context.Context, action, src, dst string, d *data, fileCache FileCache) error { + switch action { + // TODO: use enum + case "copy": + if !d.user.Perm.Create { + return errors.ErrPermissionDenied + } + + return fileutils.Copy(d.user.Fs, src, dst) + case "rename": + if !d.user.Perm.Rename { + return errors.ErrPermissionDenied + } + src = path.Clean("/" + src) + dst = path.Clean("/" + dst) + + file, err := files.NewFileInfo(files.FileOptions{ + Fs: d.user.Fs, + Path: src, + Modify: d.user.Perm.Modify, + Expand: false, + ReadHeader: false, + Checker: d, + }) + if err != nil { + return err + } + + // delete thumbnails + err = delThumbs(ctx, fileCache, file) + if err != nil { + return err + } + + return fileutils.MoveFile(d.user.Fs, src, dst) + default: + return fmt.Errorf("unsupported action %s: %w", action, errors.ErrInvalidRequestParams) + } +}