From a1524af7a9e84b304a6ae0604378394004a64289 Mon Sep 17 00:00:00 2001 From: Thomas Miceli <27960254+thomiceli@users.noreply.github.com> Date: Fri, 5 Jan 2024 22:56:04 +0100 Subject: [PATCH] Fix directory renaming on username change (#205) * src/dest dirs have to be lowercase * if the src dir doesn't exist, don't rename --- internal/web/settings.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/web/settings.go b/internal/web/settings.go index 1007f77..69cdbb9 100644 --- a/internal/web/settings.go +++ b/internal/web/settings.go @@ -4,6 +4,7 @@ import ( "crypto/md5" "fmt" "github.com/thomiceli/opengist/internal/config" + "github.com/thomiceli/opengist/internal/git" "os" "path/filepath" "strconv" @@ -162,11 +163,14 @@ func usernameProcess(ctx echo.Context) error { return redirect(ctx, "/settings") } - err := os.Rename( - filepath.Join(config.C.OpengistHome, "repos", user.Username), - filepath.Join(config.C.OpengistHome, "repos", dto.Username)) - if err != nil { - return errorRes(500, "Cannot rename user directory", err) + sourceDir := filepath.Join(config.C.OpengistHome, git.ReposDirectory, strings.ToLower(user.Username)) + destinationDir := filepath.Join(config.C.OpengistHome, git.ReposDirectory, strings.ToLower(dto.Username)) + + if _, err := os.Stat(sourceDir); !os.IsNotExist(err) { + err := os.Rename(sourceDir, destinationDir) + if err != nil { + return errorRes(500, "Cannot rename user directory", err) + } } user.Username = dto.Username