192 lines
7.0 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Linq;
2023-07-30 16:15:52 -04:00
using System.Reflection;
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;
namespace SPTInstaller.Helpers;
public static class FileHelper
2023-05-11 23:11:39 -04:00
{
public static string GetRedactedPath(string path)
{
var nameMatched = Regex.Match(path, @".:\\[uU]sers\\(?<NAME>[^\\]+)");
2024-05-01 10:31:55 -04:00
if (nameMatched.Success)
{
var name = nameMatched.Groups["NAME"].Value;
return path.Replace(name, "-REDACTED-");
2023-05-22 15:09:00 -04:00
}
2024-05-01 10:31:55 -04:00
return path;
}
2024-05-01 10:31:55 -04:00
public static Result CopyDirectoryWithProgress(DirectoryInfo sourceDir, DirectoryInfo targetDir,
IProgress<double> progress = null, string[] exclusions = null) =>
CopyDirectoryWithProgress(sourceDir, targetDir, (msg, prog) => progress?.Report(prog), exclusions);
2024-05-01 10:31:55 -04:00
public static Result CopyDirectoryWithProgress(DirectoryInfo sourceDir, DirectoryInfo targetDir,
Action<string, int> updateCallback = null, string[] exclusions = null)
{
try
2023-05-11 23:11:39 -04:00
{
2024-05-04 16:19:04 -04:00
var allFiles = sourceDir.GetFiles("*", SearchOption.AllDirectories);
var fileCopies = new List<CopyInfo>();
int count = 0;
2024-05-01 10:31:55 -04:00
2024-05-04 16:19:04 -04:00
// filter files before starting copy
foreach (var file in allFiles)
{
count++;
updateCallback?.Invoke("getting list of files to copy", (int)Math.Floor((double)count / allFiles.Length * 100));
var currentFileRelativePath = file.FullName.Replace(sourceDir.FullName, "");
if (exclusions != null)
{
// check exclusions
foreach (var exclusion in exclusions)
{
if (currentFileRelativePath.StartsWith(exclusion) || currentFileRelativePath == exclusion)
{
Log.Debug(
$"EXCLUSION FOUND :: FILE\nExclusion: '{exclusion}'\nPath: '{currentFileRelativePath}'");
break;
}
}
}
// don't copy .bak files
if (currentFileRelativePath.EndsWith(".bak"))
{
Log.Debug($"EXCLUDING BAK FILE :: {currentFileRelativePath}");
break;
}
fileCopies.Add(new CopyInfo(file.FullName, file.FullName.Replace(sourceDir.FullName, targetDir.FullName)));
}
count = 0;
2024-05-01 10:31:55 -04:00
2024-05-04 16:19:04 -04:00
// process copy info for files that need to be copied
foreach (var copyInfo in fileCopies)
{
count++;
updateCallback?.Invoke(copyInfo.FileName, (int)Math.Floor((double)count / fileCopies.Count * 100));
var result = copyInfo.Copy();
if (!result.Succeeded)
{
return result;
}
}
2024-05-01 10:31:55 -04: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
}
}
2024-05-01 10:31:55 -04:00
public static bool StreamAssemblyResourceOut(string resourceName, string outputFilePath)
2023-07-30 16:15:52 -04:00
{
try
{
Log.Debug($"Starting StreamAssemblyResourceOut, resourcename: {resourceName}, outputFilePath: {outputFilePath}");
var assembly = Assembly.GetExecutingAssembly();
Log.Debug($"1");
2024-05-01 10:31:55 -04:00
FileInfo outputFile = new FileInfo(outputFilePath);
Log.Debug($"2");
if (outputFile.Exists)
{
Log.Debug($"3");
outputFile.Delete();
}
Log.Debug($"4");
if (!outputFile.Directory.Exists)
{
Log.Debug($"5");
Directory.CreateDirectory(outputFile.Directory.FullName);
}
Log.Debug($"6");
var resName = assembly.GetManifestResourceNames().First(x => x.EndsWith(resourceName));
Log.Debug($"7");
using (FileStream fs = File.Create(outputFilePath))
using (Stream s = assembly.GetManifestResourceStream(resName))
{
Log.Debug($"8");
s.CopyTo(fs);
}
Log.Debug($"9");
outputFile.Refresh();
Log.Debug(outputFile.Exists.ToString());
return outputFile.Exists;
}
catch (Exception ex)
2023-07-30 16:15:52 -04:00
{
Log.Fatal(ex, $"Failed to stream resource out: {resourceName}");
return false;
2023-07-30 16:15:52 -04:00
}
}
2024-05-01 10:31:55 -04:00
public static bool CheckPathForProblemLocations(string path, out PathCheck failedCheck)
2023-08-22 10:21:52 -04:00
{
failedCheck = new();
2024-05-01 10:31:55 -04:00
var problemPaths = new List<PathCheck>()
{
new("Documents", PathCheckType.EndsWith, PathCheckAction.Warn),
2024-04-06 12:53:45 -04:00
new("Desktop", PathCheckType.EndsWith, PathCheckAction.Deny),
2024-04-10 19:20:04 -04:00
new("Battlestate Games", PathCheckType.Contains, PathCheckAction.Deny),
new("Desktop", PathCheckType.Contains, PathCheckAction.Warn),
new("scoped_dir", PathCheckType.Contains, PathCheckAction.Deny),
new("Downloads", PathCheckType.Contains, PathCheckAction.Deny),
new("OneDrive", PathCheckType.Contains, PathCheckAction.Deny),
2024-04-06 12:53:45 -04:00
new("NextCloud", PathCheckType.Contains, PathCheckAction.Deny),
new("DropBox", PathCheckType.Contains, PathCheckAction.Deny),
new("Google", PathCheckType.Contains, PathCheckAction.Deny),
new("Program Files", PathCheckType.Contains, PathCheckAction.Deny),
new("Program Files (x86", PathCheckType.Contains, PathCheckAction.Deny),
new(Path.Join("spt-installer", "cache"), PathCheckType.Contains, PathCheckAction.Deny),
new("Drive Root", PathCheckType.DriveRoot, PathCheckAction.Deny)
};
2024-05-01 10:31:55 -04:00
foreach (var check in problemPaths)
{
switch (check.CheckType)
{
case PathCheckType.EndsWith:
if (path.ToLower().EndsWith(check.Target.ToLower()))
{
failedCheck = check;
return true;
}
2024-05-01 10:31:55 -04:00
break;
case PathCheckType.Contains:
if (path.ToLower().Contains(check.Target.ToLower()))
{
failedCheck = check;
return true;
}
2024-05-01 10:31:55 -04:00
break;
2023-11-15 09:57:27 -05:00
case PathCheckType.DriveRoot:
if (Regex.Match(path.ToLower(), @"^\w:(\\|\/)$").Success)
{
failedCheck = check;
2023-11-15 09:57:27 -05:00
return true;
}
2024-05-01 10:31:55 -04:00
break;
}
}
2024-05-01 10:31:55 -04:00
return false;
2023-08-22 10:21:52 -04:00
}
}