From 3feb24be7662c3b172ad96e9284fb77bab52e4ef Mon Sep 17 00:00:00 2001 From: andreastanderen Date: Wed, 20 Nov 2024 09:27:19 +0100 Subject: [PATCH] use gitRepoMethods to move files/dirs in appGitRepo --- .../GitRepository/AltinnAppGitRepository.cs | 27 ++--------- .../GitRepository/GitRepository.cs | 45 +++++++++++++++---- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs b/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs index 7299c3c512b..93ddf76bcc4 100644 --- a/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs +++ b/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs @@ -425,20 +425,9 @@ public string[] GetLayoutSetNames() public void ChangeLayoutSetFolderName(string oldLayoutSetName, string newLayoutSetName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); - if (DirectoryExistsByRelativePath(GetPathToLayoutSet(newLayoutSetName))) - { - throw new NonUniqueLayoutSetIdException("Suggested new layout set name already exist"); - } - string destAbsolutePath = GetAbsoluteFileOrDirectoryPathSanitized(GetPathToLayoutSet(newLayoutSetName, true)); - - string sourceRelativePath = GetPathToLayoutSet(oldLayoutSetName, true); - if (!DirectoryExistsByRelativePath(sourceRelativePath)) - { - throw new NotFoundException("Layout set you are trying to change doesn't exist"); - } - - string sourceAbsolutePath = GetAbsoluteFileOrDirectoryPathSanitized(sourceRelativePath); - Directory.Move(sourceAbsolutePath, destAbsolutePath); + string currentDirectoryPath = GetPathToLayoutSet(oldLayoutSetName, true); + string newDirectoryPath = GetPathToLayoutSet(newLayoutSetName, true); + MoveDirectoryByRelativePath(currentDirectoryPath, newDirectoryPath); } /// @@ -575,15 +564,7 @@ public void UpdateFormLayoutName(string layoutSetName, string layoutFileName, st { string currentFilePath = GetPathToLayoutFile(layoutSetName, layoutFileName); string newFilePath = GetPathToLayoutFile(layoutSetName, newFileName); - if (!FileExistsByRelativePath(currentFilePath)) - { - throw new FileNotFoundException("Layout does not exist."); - } - if (FileExistsByRelativePath(newFilePath)) - { - throw new ArgumentException("New layout name must be unique."); - } - File.Move(GetAbsoluteFileOrDirectoryPathSanitized(currentFilePath), GetAbsoluteFileOrDirectoryPathSanitized(newFilePath)); + MoveFileByRelativePath(currentFilePath, newFilePath, newFileName); } public async Task GetLayoutSetsFile(CancellationToken cancellationToken = default) diff --git a/backend/src/Designer/Infrastructure/GitRepository/GitRepository.cs b/backend/src/Designer/Infrastructure/GitRepository/GitRepository.cs index 6b78fe844ba..4b999c8cc03 100644 --- a/backend/src/Designer/Infrastructure/GitRepository/GitRepository.cs +++ b/backend/src/Designer/Infrastructure/GitRepository/GitRepository.cs @@ -254,19 +254,46 @@ public void DeleteFileByAbsolutePath(string absoluteFilePath) /// FileName for the destination file protected void MoveFileByRelativePath(string sourceRelativeFilePath, string destRelativeFilePath, string destinationFileName) { - if (FileExistsByRelativePath(sourceRelativeFilePath)) + if (!FileExistsByRelativePath(sourceRelativeFilePath)) { - Guard.AssertNotNullOrEmpty(sourceRelativeFilePath, nameof(sourceRelativeFilePath)); + throw new FileNotFoundException($"File {sourceRelativeFilePath} does not exist."); + } - string sourceAbsoluteFilePath = GetAbsoluteFileOrDirectoryPathSanitized(sourceRelativeFilePath); - string destAbsoluteFilePath = GetAbsoluteFileOrDirectoryPathSanitized(destRelativeFilePath); - string destAbsoluteParentDirPath = destAbsoluteFilePath.Remove(destAbsoluteFilePath.IndexOf(destinationFileName, StringComparison.Ordinal)); - Directory.CreateDirectory(destAbsoluteParentDirPath); - Guard.AssertFilePathWithinParentDirectory(RepositoryDirectory, sourceAbsoluteFilePath); - Guard.AssertFilePathWithinParentDirectory(RepositoryDirectory, destAbsoluteFilePath); + if (FileExistsByRelativePath(destRelativeFilePath)) + { + throw new IOException($"Suggested file name {destinationFileName} already exists."); + } + string sourceAbsoluteFilePath = GetAbsoluteFileOrDirectoryPathSanitized(sourceRelativeFilePath); + string destAbsoluteFilePath = GetAbsoluteFileOrDirectoryPathSanitized(destRelativeFilePath); + string destAbsoluteParentDirPath = destAbsoluteFilePath.Remove(destAbsoluteFilePath.IndexOf(destinationFileName, StringComparison.Ordinal)); + Directory.CreateDirectory(destAbsoluteParentDirPath); + Guard.AssertFilePathWithinParentDirectory(RepositoryDirectory, sourceAbsoluteFilePath); + Guard.AssertFilePathWithinParentDirectory(RepositoryDirectory, destAbsoluteFilePath); + + File.Move(sourceAbsoluteFilePath, destAbsoluteFilePath); + } - File.Move(sourceAbsoluteFilePath, destAbsoluteFilePath); + /// + /// Move the specified folder to specified destination + /// + /// Relative path to folder to be moved. + /// Relative path to destination of moved folder. + protected void MoveDirectoryByRelativePath(string sourceRelativeDirectoryPath, string destRelativeDirectoryPath) + { + if (!DirectoryExistsByRelativePath(sourceRelativeDirectoryPath)) + { + throw new DirectoryNotFoundException($"Directory {sourceRelativeDirectoryPath} does not exist."); } + if (DirectoryExistsByRelativePath(destRelativeDirectoryPath)) + { + throw new IOException($"Suggested directory {destRelativeDirectoryPath} already exists."); + } + string sourceAbsoluteDirectoryPath = GetAbsoluteFileOrDirectoryPathSanitized(sourceRelativeDirectoryPath); + string destAbsoluteDirectoryPath = GetAbsoluteFileOrDirectoryPathSanitized(destRelativeDirectoryPath); + Guard.AssertFilePathWithinParentDirectory(RepositoryDirectory, sourceAbsoluteDirectoryPath); + Guard.AssertFilePathWithinParentDirectory(RepositoryDirectory, destAbsoluteDirectoryPath); + + Directory.Move(sourceAbsoluteDirectoryPath, destAbsoluteDirectoryPath); } ///