131 lines
6.6 KiB
C#
131 lines
6.6 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Phares.Shared;
|
|
using Serilog;
|
|
using ShellProgressBar;
|
|
using View_by_Distance.Delete.By.Distinct.Models;
|
|
using View_by_Distance.Property.Models;
|
|
using View_by_Distance.Shared.Models.Methods;
|
|
|
|
namespace View_by_Distance.Delete.By.Distinct;
|
|
|
|
public class DeleteByDistinct
|
|
{
|
|
|
|
public DeleteByDistinct(List<string> args, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
|
|
{
|
|
if (isSilent)
|
|
{ }
|
|
if (console is null)
|
|
{ }
|
|
long ticks = DateTime.Now.Ticks;
|
|
ILogger? log = Log.ForContext<DeleteByDistinct>();
|
|
Dictionary<long, Dictionary<long, List<string>>> fileSizeToCollection = new();
|
|
Configuration configuration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
|
|
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
|
configuration.Update();
|
|
log.Information(configuration.RootDirectory);
|
|
bool compareIsPopulatedAndNotTheSame = !string.IsNullOrEmpty(appSettings.CompareRootDirectory) && appSettings.CompareRootDirectory != configuration.RootDirectory;
|
|
Work(appSettings, ticks, log, configuration.RootDirectory, nameof(configuration.RootDirectory), options, fileSizeToCollection, logOnly: compareIsPopulatedAndNotTheSame);
|
|
if (compareIsPopulatedAndNotTheSame)
|
|
Work(appSettings, ticks, log, appSettings.CompareRootDirectory, nameof(appSettings.CompareRootDirectory), options, fileSizeToCollection, logOnly: false);
|
|
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(appSettings.CompareRootDirectory);
|
|
}
|
|
|
|
private static void Work(AppSettings appSettings, long ticks, ILogger log, string directory, string variable, ProgressBarOptions options, Dictionary<long, Dictionary<long, List<string>>> fileSizeToCollection, bool logOnly)
|
|
{
|
|
string message;
|
|
string checkName;
|
|
string deleteLog;
|
|
int totalSeconds;
|
|
FileInfo fileInfo;
|
|
DateTime checkDate;
|
|
ProgressBar progressBar;
|
|
List<string>? fileNames;
|
|
ConsoleKey? consoleKey = null;
|
|
List<string> deletedFiles = new();
|
|
List<string> deletedDirectories = new();
|
|
Dictionary<long, List<string>>? fileTicksToNames;
|
|
log.Information($"Gathering {appSettings.SearchPattern} files from <{directory}>");
|
|
(string directory, string[] files)[] leftCollection = Shared.Models.Stateless.Methods.IFileHolder.GetFiles(directory, appSettings.SearchPattern).ToArray();
|
|
totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
|
message = $") Looking for duplicates from <{directory}> - {totalSeconds} total second(s)";
|
|
progressBar = new(leftCollection.Length, message, options);
|
|
foreach ((_, string[] files) in leftCollection)
|
|
{
|
|
progressBar.Tick();
|
|
foreach (string file in files)
|
|
{
|
|
fileInfo = new(file);
|
|
if (fileInfo.Length < 100)
|
|
continue;
|
|
if (!fileSizeToCollection.TryGetValue(fileInfo.Length, out fileTicksToNames))
|
|
{
|
|
fileSizeToCollection.Add(fileInfo.Length, new());
|
|
if (!fileSizeToCollection.TryGetValue(fileInfo.Length, out fileTicksToNames))
|
|
throw new Exception();
|
|
}
|
|
// if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames))
|
|
// {
|
|
// fileTicksToNames.Add(fileInfo.LastWriteTime.Ticks, new());
|
|
// if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames))
|
|
// throw new Exception();
|
|
// }
|
|
checkDate = new DateTime(ticks);
|
|
// checkDate = new DateTime(fileInfo.LastWriteTime.Year, fileInfo.LastWriteTime.Month, fileInfo.LastWriteTime.Day);
|
|
if (!fileTicksToNames.TryGetValue(checkDate.Ticks, out fileNames))
|
|
{
|
|
fileTicksToNames.Add(checkDate.Ticks, new());
|
|
if (!fileTicksToNames.TryGetValue(checkDate.Ticks, out fileNames))
|
|
throw new Exception();
|
|
}
|
|
checkName = fileInfo.Name.ToLower(); //.Replace(".jpeg", ".jpg");
|
|
if (fileNames.Contains(checkName))
|
|
deletedFiles.Add(file);
|
|
else
|
|
fileNames.Add(checkName);
|
|
}
|
|
}
|
|
progressBar.Dispose();
|
|
log.Information(". . .");
|
|
deleteLog = $"{ticks}-{variable}-Files.lsv";
|
|
if (!logOnly)
|
|
File.WriteAllLines(Path.Combine(directory, deleteLog), deletedFiles);
|
|
if (deletedFiles.Any() && !logOnly)
|
|
{
|
|
log.Information($"Ready to delete {deletedFiles.Count} from {variable} {appSettings.SearchPattern} file(s)? See <{deleteLog}>");
|
|
for (int y = 0; y < int.MaxValue; y++)
|
|
{
|
|
log.Information("Press \"Y\" key to delete file(s), \"N\" key to log file(s) or close console to not delete files");
|
|
consoleKey = Console.ReadKey().Key;
|
|
if (consoleKey is ConsoleKey.Y or ConsoleKey.N)
|
|
break;
|
|
}
|
|
log.Information(". . .");
|
|
if (consoleKey is not null && consoleKey.Value == ConsoleKey.Y)
|
|
{
|
|
foreach (string file in deletedFiles)
|
|
{
|
|
try
|
|
{ File.Delete(file); }
|
|
catch (Exception) { }
|
|
}
|
|
totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
|
message = $") Looking for empty directories from <{directory}> - {totalSeconds} total second(s)";
|
|
progressBar = new(4, message, options);
|
|
for (int i = 1; i < 5; i++)
|
|
{
|
|
progressBar.Tick();
|
|
List<string> check = new();
|
|
Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(directory, check);
|
|
if (!check.Any())
|
|
break;
|
|
deletedDirectories.AddRange(check);
|
|
}
|
|
progressBar.Dispose();
|
|
log.Information(". . .");
|
|
deleteLog = $"{ticks + 1}-{variable}-Directories.lsv";
|
|
File.WriteAllLines(Path.Combine(directory, deleteLog), deletedDirectories.Distinct());
|
|
}
|
|
}
|
|
}
|
|
} |