Skip to content

Commit

Permalink
Fix #248
Browse files Browse the repository at this point in the history
Move contents of old DownloadLocation and WorkingDirectory to new paths. Overwrite existing files, and add from oldPath.
  • Loading branch information
C9Glax committed Sep 16, 2024
1 parent 77bb309 commit 99df9a9
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions Tranga/TrangaSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,52 @@ public static void UpdateAprilFoolsMode(bool enabled)
public static void UpdateDownloadLocation(string newPath, bool moveFiles = true)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
Directory.CreateDirectory(newPath,
GroupRead | GroupWrite | None | OtherRead | OtherWrite | UserRead | UserWrite);
Directory.CreateDirectory(newPath, GroupRead | GroupWrite | None | OtherRead | OtherWrite | UserRead | UserWrite);
else
Directory.CreateDirectory(newPath);

if (moveFiles && Directory.Exists(TrangaSettings.downloadLocation))
Directory.Move(TrangaSettings.downloadLocation, newPath);

if (moveFiles)
MoveContentsOfDirectoryTo(TrangaSettings.downloadLocation, newPath);
TrangaSettings.downloadLocation = newPath;
ExportSettings();
}

public static void UpdateWorkingDirectory(string newPath)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
Directory.CreateDirectory(newPath,
GroupRead | GroupWrite | None | OtherRead | OtherWrite | UserRead | UserWrite);
Directory.CreateDirectory(newPath, GroupRead | GroupWrite | None | OtherRead | OtherWrite | UserRead | UserWrite);
else
Directory.CreateDirectory(newPath);
Directory.Move(TrangaSettings.workingDirectory, newPath);

MoveContentsOfDirectoryTo(TrangaSettings.workingDirectory, newPath);

TrangaSettings.workingDirectory = newPath;
ExportSettings();
}

private static void MoveContentsOfDirectoryTo(string oldDir, string newDir)
{
string[] directoryPaths = Directory.GetDirectories(oldDir);
string[] filePaths = Directory.GetFiles(oldDir);
foreach (string file in filePaths)
{
string newPath = Path.Join(newDir, Path.GetFileName(file));
File.Move(file, newPath, true);
}
foreach(string directory in directoryPaths)
{
string? dirName = Path.GetDirectoryName(directory);
if(dirName is null)
continue;
string newPath = Path.Join(newDir, dirName);
if(Directory.Exists(newPath))
MoveContentsOfDirectoryTo(directory, newPath);
else
Directory.Move(directory, newPath);
}
}

public static void UpdateUserAgent(string? customUserAgent)
{
TrangaSettings.userAgent = customUserAgent ?? DefaultUserAgent;
Expand Down

0 comments on commit 99df9a9

Please sign in to comment.