diff --git a/backend/handlers/files.go b/backend/handlers/files.go index dfead3b..deee6e3 100644 --- a/backend/handlers/files.go +++ b/backend/handlers/files.go @@ -403,6 +403,10 @@ func (h *FSHandler) Move(c *fiber.Ctx) error { return c.Status(fiber.StatusForbidden).JSON(fiber.Map{"error": err.Error()}) } + if srcFull == dstFull || filepath.Dir(srcFull) == dstFull { + return c.JSON(fiber.Map{"message": "moved successfully"}) + } + // If destination is a directory, move into it if info, err := os.Stat(dstFull); err == nil && info.IsDir() { uniqueName := GetUniquePath(dstFull, filepath.Base(srcFull)) diff --git a/backend/handlers/utils.go b/backend/handlers/utils.go index 0d64875..3432bbb 100644 --- a/backend/handlers/utils.go +++ b/backend/handlers/utils.go @@ -4,8 +4,11 @@ import ( "fmt" "os" "path/filepath" + "regexp" ) +var numberSuffixRegex = regexp.MustCompile(` \(\d+\)$`) + // GetUniquePath checks if a file or folder exists at the given baseDir with the given name. // If it does, it systematically appends " (1)", " (2)", etc., until it finds an available name. // It returns the unique filename. @@ -18,6 +21,8 @@ func GetUniquePath(baseDir, name string) string { return name } + base = numberSuffixRegex.ReplaceAllString(base, "") + for i := 1; ; i++ { newName := fmt.Sprintf("%s (%d)%s", base, i, ext) finalPath = filepath.Join(baseDir, newName)