Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use gitRepoMethods to move files/dirs in appGitRepo #14113

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading