reload implemented

This commit is contained in:
nihil 2025-05-10 19:17:29 +02:00
parent 63c1c88393
commit 1bb7d461e0
2 changed files with 58 additions and 29 deletions

View File

@ -10,6 +10,8 @@ namespace RedundancyFinder
string[] extensions; string[] extensions;
List<string> ignorePaths = new List<string>();
public event EventHandler<DirectoryErrorEventArgs>? DirectoryError; public event EventHandler<DirectoryErrorEventArgs>? DirectoryError;
public event EventHandler<FileErrorEventArgs>? FileError; public event EventHandler<FileErrorEventArgs>? FileError;
public event EventHandler<FileFoundEventArgs>? FileFound; public event EventHandler<FileFoundEventArgs>? FileFound;
@ -17,6 +19,7 @@ namespace RedundancyFinder
public event EventHandler<ProcessingFileEventArgs>? ProcessingFile; public event EventHandler<ProcessingFileEventArgs>? ProcessingFile;
public void FindRedundancies(string[] paths, string[] extensions) public void FindRedundancies(string[] paths, string[] extensions)
{ {
redundancies?.Values.SelectMany(x => x.Paths).ToList().ForEach(x => ignorePaths.Add(x));
this.extensions = extensions; this.extensions = extensions;
foreach (var path in paths) foreach (var path in paths)
{ {
@ -70,6 +73,11 @@ namespace RedundancyFinder
private void ProcessFile(string filePath) private void ProcessFile(string filePath)
{ {
if(ignorePaths.Contains(filePath))
{
return;
}
if (!extensions.Contains(Path.GetExtension(filePath))) if (!extensions.Contains(Path.GetExtension(filePath)))
{ {
return; return;
@ -89,11 +97,6 @@ namespace RedundancyFinder
long fileSize = new FileInfo(filePath).Length; long fileSize = new FileInfo(filePath).Length;
lock (redundancies) lock (redundancies)
{ {
if (redundancies.ContainsKey(fileHash))
{
return;
}
if (!redundancies.ContainsKey(fileHash)) if (!redundancies.ContainsKey(fileHash))
{ {
var redundancy = new Redundancy() { Hash = fileHash, FileSize = fileSize }; var redundancy = new Redundancy() { Hash = fileHash, FileSize = fileSize };

View File

@ -50,6 +50,31 @@ namespace RedundancyFinderCLI
SaveRedundancies(finder, settings.OutputPath); SaveRedundancies(finder, settings.OutputPath);
}; };
// Load existing redundancies if the output file exists
if (File.Exists(settings.OutputPath))
{
try
{
var existingData = File.ReadAllText(settings.OutputPath);
var existingRedundancies = JsonConvert.DeserializeObject<Dictionary<string, Redundancy>>(existingData);
if (existingRedundancies != null)
{
foreach (var entry in existingRedundancies)
{
finder.Redundancies[entry.Key] = entry.Value;
}
WriteLine($"[yellow]Resumed from existing output file: [/]'{settings.OutputPath}'");
WriteLine($"[yellow]Loaded {finder.Redundancies.Count} redundancies from the file.[/]");
}
}
catch (Exception ex)
{
WriteLine($"[red]Failed to load existing output file: {ex.Message}[/]");
}
}
finder.FileError += (sender, e) => finder.FileError += (sender, e) =>
{ {
if (e.Exception is UnauthorizedAccessException) if (e.Exception is UnauthorizedAccessException)
@ -168,6 +193,7 @@ namespace RedundancyFinderCLI
} }
} }
private void WriteLine(string v) private void WriteLine(string v)
{ {
string now = Markup.Escape($"[{DateTime.Now.ToString("HH:mm:ss")}]"); string now = Markup.Escape($"[{DateTime.Now.ToString("HH:mm:ss")}]");