Philipp Heenemann
a8b91f4ee6
Updated the existing C# code into a more modern, imperative and top-level statements style. This involves shortening the code by removing unnecessary parts like additional brackets and explicit namespace declarations. It's done to improve clarity and readability.
86 lines
2.8 KiB
C#
86 lines
2.8 KiB
C#
using System.Text.RegularExpressions;
|
|
using Serilog;
|
|
using SPTInstaller.Models;
|
|
|
|
namespace SPTInstaller.Helpers;
|
|
|
|
public static class FileHelper
|
|
{
|
|
private static Result IterateDirectories(DirectoryInfo sourceDir, DirectoryInfo targetDir)
|
|
{
|
|
try
|
|
{
|
|
foreach (var dir in sourceDir.GetDirectories("*", SearchOption.AllDirectories))
|
|
{
|
|
Directory.CreateDirectory(dir.FullName.Replace(sourceDir.FullName, targetDir.FullName));
|
|
}
|
|
return Result.FromSuccess();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error while creating directories");
|
|
return Result.FromError(ex.Message);
|
|
}
|
|
}
|
|
|
|
private static Result IterateFiles(DirectoryInfo sourceDir, DirectoryInfo targetDir, Action<string, int> updateCallback = null)
|
|
{
|
|
try
|
|
{
|
|
int totalFiles = sourceDir.GetFiles("*.*", SearchOption.AllDirectories).Length;
|
|
int processedFiles = 0;
|
|
|
|
foreach (var file in sourceDir.GetFiles("*.*", SearchOption.AllDirectories))
|
|
{
|
|
updateCallback?.Invoke(file.Name, (int)Math.Floor(((double)processedFiles / totalFiles) * 100));
|
|
|
|
File.Copy(file.FullName, file.FullName.Replace(sourceDir.FullName, targetDir.FullName), true);
|
|
processedFiles++;
|
|
}
|
|
|
|
return Result.FromSuccess();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error while copying files");
|
|
return Result.FromError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public static string GetRedactedPath(string path)
|
|
{
|
|
var nameMatched = Regex.Match(path, @".:\\[uU]sers\\(?<NAME>[^\\]+)");
|
|
|
|
if (nameMatched.Success)
|
|
{
|
|
var name = nameMatched.Groups["NAME"].Value;
|
|
return path.Replace(name, "-REDACTED-");
|
|
}
|
|
|
|
return path;
|
|
}
|
|
|
|
public static Result CopyDirectoryWithProgress(DirectoryInfo sourceDir, DirectoryInfo targetDir, IProgress<double> progress = null) =>
|
|
CopyDirectoryWithProgress(sourceDir, targetDir, (msg, prog) => progress?.Report(prog));
|
|
|
|
public static Result CopyDirectoryWithProgress(DirectoryInfo sourceDir, DirectoryInfo targetDir, Action<string, int> updateCallback = null)
|
|
{
|
|
try
|
|
{
|
|
var iterateDirectoriesResult = IterateDirectories(sourceDir, targetDir);
|
|
|
|
if(!iterateDirectoriesResult.Succeeded) return iterateDirectoriesResult;
|
|
|
|
var iterateFilesResult = IterateFiles(sourceDir, targetDir, updateCallback);
|
|
|
|
if (!iterateFilesResult.Succeeded) return iterateDirectoriesResult;
|
|
|
|
return Result.FromSuccess();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error during directory copy");
|
|
return Result.FromError(ex.Message);
|
|
}
|
|
}
|
|
} |