Skip to content

Commit

Permalink
use gitRepoMethods to move files/dirs in appGitRepo
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Nov 20, 2024
1 parent 1bd7f3f commit 3feb24b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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<LayoutSets> GetLayoutSetsFile(CancellationToken cancellationToken = default)
Expand Down
45 changes: 36 additions & 9 deletions backend/src/Designer/Infrastructure/GitRepository/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,46 @@ public void DeleteFileByAbsolutePath(string absoluteFilePath)
/// <param name="destinationFileName">FileName for the destination file</param>
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);
/// <summary>
/// Move the specified folder to specified destination
/// </summary>
/// <param name="sourceRelativeDirectoryPath">Relative path to folder to be moved.</param>
/// <param name="destRelativeDirectoryPath">Relative path to destination of moved folder.</param>
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);
}

/// <summary>
Expand Down

0 comments on commit 3feb24b

Please sign in to comment.