Compare commits

..

No commits in common. "45796d9bb287b66abcfea1e7abc3fd82a3f83460" and "b66e7dd4ad397101969bb3f173cd3294985df42d" have entirely different histories.

3 changed files with 79 additions and 100 deletions

BIN
0.3.zip

Binary file not shown.

View File

@ -10,8 +10,6 @@ namespace RedundancyFinder
string[] extensions;
List<string> ignorePaths = new List<string>();
public event EventHandler<DirectoryErrorEventArgs>? DirectoryError;
public event EventHandler<FileErrorEventArgs>? FileError;
public event EventHandler<FileFoundEventArgs>? FileFound;
@ -19,7 +17,6 @@ namespace RedundancyFinder
public event EventHandler<ProcessingFileEventArgs>? ProcessingFile;
public void FindRedundancies(string[] paths, string[] extensions)
{
redundancies?.Values.SelectMany(x => x.Paths).ToList().ForEach(x => ignorePaths.Add(x));
this.extensions = extensions;
foreach (var path in paths)
{
@ -38,6 +35,8 @@ namespace RedundancyFinder
{
try
{
// Process files in the current directory
foreach (var file in Directory.GetFiles(directoryPath))
{
@ -73,45 +72,47 @@ namespace RedundancyFinder
private void ProcessFile(string filePath)
{
if(ignorePaths.Contains(filePath))
{
return;
}
if (!extensions.Contains(Path.GetExtension(filePath)))
{
return;
}
TaskStarted?.Invoke(this, filePath);
ProcessingFile?.Invoke(this, new ProcessingFileEventArgs() { Path = filePath });
try
{
try
{
var fileHash = ComputeFileHash(filePath);
if (fileHash == null)
{
return;
}
long fileSize = new FileInfo(filePath).Length;
lock (redundancies)
{
if (!redundancies.ContainsKey(fileHash))
{
var redundancy = new Redundancy() { Hash = fileHash, FileSize = fileSize };
redundancies.Add(fileHash, redundancy);
}
redundancies[fileHash].Paths.Add(filePath);
}
FileFound?.Invoke(this, new FileFoundEventArgs(filePath, fileHash, fileSize));
}
catch (Exception ex)
{
FileError?.Invoke(this, new FileErrorEventArgs() { Exception = ex, Path = filePath });
}
var fileHash = ComputeFileHash(filePath);
if (fileHash == null)
{
return;
}
long fileSize = new FileInfo(filePath).Length;
lock (redundancies)
{
if (redundancies.ContainsKey(fileHash))
{
return;
}
}
if (!redundancies.ContainsKey(fileHash))
{
var redundancy = new Redundancy() { Hash = fileHash, FileSize = fileSize };
redundancies.Add(fileHash, redundancy);
}
redundancies[fileHash].Paths.Add(filePath);
}
FileFound?.Invoke(this, new FileFoundEventArgs(filePath, fileHash, fileSize));
}
catch (Exception ex)
{
FileError?.Invoke(this, new FileErrorEventArgs() { Exception = ex, Path = filePath });
}
}
private static string ComputeFileHash(string filePath)
{

View File

@ -38,42 +38,19 @@ namespace RedundancyFinderCLI
}
public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
{
var extensions = settings.Extensions.Split(",", StringSplitOptions.RemoveEmptyEntries);
Finder finder = new Finder();
int hashingTasks = 0;
int hashingTasksFinished = 0;
// Register the ProcessExit event to save redundancies on exit
AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
{
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) =>
{
@ -81,14 +58,14 @@ namespace RedundancyFinderCLI
{
if (settings.Verbose)
{
WriteLine($"[red]Access denied to file: [/]{e.Path}. Skipping. Error: [red]{e.Exception.Message}[/]");
AnsiConsole.MarkupLine($"[red]Access denied to file: [/]{e.Path}. Skipping. Error: [red]{e.Exception.Message}[/]");
}
}
else
{
if (settings.Verbose)
{
WriteLine($"[red] Error processing file:\n[/]Path:{e.Path}\n[red]{e.Exception.Message}[/]");
AnsiConsole.MarkupLine($"[red] Error processing file:\n[/]Path:{e.Path}\n[red]{e.Exception.Message}[/]");
}
}
};
@ -99,14 +76,14 @@ namespace RedundancyFinderCLI
{
if (settings.Verbose)
{
WriteLine($"[red]Access denied to directory: [/]{e.Path}. Skipping. Error: [red]{e.Exception.Message}[/]");
AnsiConsole.MarkupLine($"[red]Access denied to directory: [/]{e.Path}. Skipping. Error: [red]{e.Exception.Message}[/]");
}
}
else
{
if (settings.Verbose)
{
WriteLine($"[red] Error processing directory:\n[/]Path:{e.Path}\n[red]{e.Exception.Message}[/]");
AnsiConsole.MarkupLine($"[red] Error processing directory:\n[/]Path:{e.Path}\n[red]{e.Exception.Message}[/]");
}
}
};
@ -115,42 +92,69 @@ namespace RedundancyFinderCLI
{
if (settings.Verbose)
{
WriteLine($"[green]Processing file: [/]{e.Path}");
AnsiConsole.MarkupLine($"[green]Processing file: [/]{e.Path}");
}
};
try
{
var p = AnsiConsole.Progress();
p.Start(ctx =>
p
.Start(ctx =>
{
// Define tasks
//var hashing = ctx.AddTask("[green]Hashing Files[/]",autoStart:false,);
finder.TaskStarted += (sender, e) =>
{
hashingTasks++;
if (settings.Verbose)
{
WriteLine($"[green]Task started: [/]{e}");
AnsiConsole.MarkupLine($"[green]Task started: [/]{e}");
}
//hashing.MaxValue = hashingTasks;
};
finder.FileFound += (sender, e) =>
{
if (settings.Verbose)
{
WriteLine($"[green]File found: [/]{e.FilePath} {GetSizeFormat((ulong)e.Size)} ");
AnsiConsole.MarkupLine($"[green]File found: [/]{e.FilePath} {GetSizeFormat((ulong)e.Size)} ");
}
hashingTasksFinished++;
//hashing.Value = hashingTasksFinished;
};
//hashing.Value = hashing.MaxValue;
finder.FindRedundancies(settings.SearchPaths, extensions);
//hashing.StopTask();
});
SaveRedundancies(finder, settings.OutputPath);
var json = JsonConvert.SerializeObject(finder.Redundancies, Formatting.Indented);
var outputPath = settings.OutputPath;
//check if path is relative or absolute
if (!Path.IsPathRooted(settings.OutputPath))
{
outputPath = Path.Combine(Directory.GetCurrentDirectory(), outputPath);
}
File.WriteAllText(outputPath, json);
AnsiConsole.MarkupLine($"[yellow]Wrote [/]{finder.Redundancies.Count}[yellow] redundancies to [/]'{outputPath}'");
ulong totalSize = finder.Redundancies.Select(x => (ulong)x.Value.FileSize).Aggregate((a, b) => a + b);
string sizeFormat = GetSizeFormat(totalSize);
WriteLine($"Total Size: [green]{sizeFormat}[/]");
AnsiConsole.MarkupLine($"Total Size: [green]{sizeFormat}[/]");
if (settings.Verbose)
{
foreach (var redundancy in finder.Redundancies.Values)
@ -163,43 +167,17 @@ namespace RedundancyFinderCLI
}
AnsiConsole.WriteLine();
}
}
}
catch (Exception e)
{
WriteLine($"[red] Error:\n[/]{e.Message}");
AnsiConsole.MarkupLine($"[red] Error:\n[/]{e.Message}");
}
return 0;
}
private void SaveRedundancies(Finder finder, string outputPath)
{
try
{
var json = JsonConvert.SerializeObject(finder.Redundancies, Formatting.Indented);
// Check if path is relative or absolute
if (!Path.IsPathRooted(outputPath))
{
outputPath = Path.Combine(Directory.GetCurrentDirectory(), outputPath);
}
File.WriteAllText(outputPath, json);
WriteLine($"[yellow]Wrote [/]{finder.Redundancies.Count}[yellow] redundancies to [/]'{outputPath}'");
}
catch (Exception ex)
{
WriteLine($"[red]Failed to save redundancies: {ex.Message}[/]");
}
}
private void WriteLine(string v)
{
string now = Markup.Escape($"[{DateTime.Now.ToString("HH:mm:ss")}]");
AnsiConsole.MarkupLine($"[gray]{now}[/] {v}");
}
private static string GetSizeFormat(ulong totalSize)
{
string sizeUnit = "B";