2023-07-12 09:19:33 +02:00
|
|
|
|
using System.Text.RegularExpressions;
|
2023-05-14 22:35:06 -04:00
|
|
|
|
using Serilog;
|
2023-05-11 23:11:39 -04:00
|
|
|
|
using SPTInstaller.Models;
|
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
namespace SPTInstaller.Helpers;
|
|
|
|
|
|
|
|
|
|
public static class FileHelper
|
2023-05-11 23:11:39 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
private static Result IterateDirectories(DirectoryInfo sourceDir, DirectoryInfo targetDir)
|
2023-05-11 23:11:39 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
try
|
2023-05-11 23:11:39 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
foreach (var dir in sourceDir.GetDirectories("*", SearchOption.AllDirectories))
|
2023-05-11 23:11:39 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
Directory.CreateDirectory(dir.FullName.Replace(sourceDir.FullName, targetDir.FullName));
|
2023-05-11 23:11:39 -04:00
|
|
|
|
}
|
2023-07-12 09:19:33 +02:00
|
|
|
|
return Result.FromSuccess();
|
2023-05-11 23:11:39 -04:00
|
|
|
|
}
|
2023-07-12 09:19:33 +02:00
|
|
|
|
catch (Exception ex)
|
2023-05-11 23:11:39 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
Log.Error(ex, "Error while creating directories");
|
|
|
|
|
return Result.FromError(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-11 23:11:39 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
private static Result IterateFiles(DirectoryInfo sourceDir, DirectoryInfo targetDir, Action<string, int> updateCallback = null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int totalFiles = sourceDir.GetFiles("*.*", SearchOption.AllDirectories).Length;
|
|
|
|
|
int processedFiles = 0;
|
2023-05-11 23:11:39 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
foreach (var file in sourceDir.GetFiles("*.*", SearchOption.AllDirectories))
|
2023-05-11 23:11:39 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
updateCallback?.Invoke(file.Name, (int)Math.Floor(((double)processedFiles / totalFiles) * 100));
|
|
|
|
|
|
|
|
|
|
File.Copy(file.FullName, file.FullName.Replace(sourceDir.FullName, targetDir.FullName), true);
|
|
|
|
|
processedFiles++;
|
2023-05-11 23:11:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
return Result.FromSuccess();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2023-05-22 15:09:00 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
Log.Error(ex, "Error while copying files");
|
|
|
|
|
return Result.FromError(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-22 15:09:00 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
public static string GetRedactedPath(string path)
|
|
|
|
|
{
|
|
|
|
|
var nameMatched = Regex.Match(path, @".:\\[uU]sers\\(?<NAME>[^\\]+)");
|
2023-05-22 15:09:00 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
if (nameMatched.Success)
|
|
|
|
|
{
|
|
|
|
|
var name = nameMatched.Groups["NAME"].Value;
|
|
|
|
|
return path.Replace(name, "-REDACTED-");
|
2023-05-22 15:09:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Result CopyDirectoryWithProgress(DirectoryInfo sourceDir, DirectoryInfo targetDir, IProgress<double> progress = null) =>
|
|
|
|
|
CopyDirectoryWithProgress(sourceDir, targetDir, (msg, prog) => progress?.Report(prog));
|
2023-05-11 23:11:39 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
public static Result CopyDirectoryWithProgress(DirectoryInfo sourceDir, DirectoryInfo targetDir, Action<string, int> updateCallback = null)
|
|
|
|
|
{
|
|
|
|
|
try
|
2023-05-11 23:11:39 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
var iterateDirectoriesResult = IterateDirectories(sourceDir, targetDir);
|
2023-05-11 23:11:39 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
if(!iterateDirectoriesResult.Succeeded) return iterateDirectoriesResult;
|
2023-05-11 23:11:39 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
var iterateFilesResult = IterateFiles(sourceDir, targetDir, updateCallback);
|
2023-05-11 23:11:39 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
if (!iterateFilesResult.Succeeded) return iterateDirectoriesResult;
|
2023-05-11 23:11:39 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
return Result.FromSuccess();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(ex, "Error during directory copy");
|
|
|
|
|
return Result.FromError(ex.Message);
|
2023-05-11 23:11:39 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-12 09:19:33 +02:00
|
|
|
|
}
|