Switch to ExifDirectory from Property

This commit is contained in:
2025-04-06 18:23:57 -07:00
parent 3f7affceef
commit c7ded16e50
50 changed files with 2647 additions and 1846 deletions

View File

@ -1,4 +1,4 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Phares.Shared;
using ShellProgressBar;
@ -19,7 +19,7 @@ public class CopyDistinct
private readonly IsEnvironment _IsEnvironment;
private readonly IConfigurationRoot _ConfigurationRoot;
private readonly Property.Models.Configuration _PropertyConfiguration;
private readonly ReadOnlyDictionary<string, ReadOnlyDictionary<byte, ReadOnlyCollection<string>>> _FileGroups;
private readonly ReadOnlyDictionary<byte, ReadOnlyCollection<string>> _FileGroups;
public CopyDistinct(List<string> args, ILogger<Program> logger, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
{
@ -37,7 +37,9 @@ public class CopyDistinct
_Configuration = configuration;
logger?.LogInformation(propertyConfiguration.RootDirectory);
(bool move, ReadOnlyCollection<string[]> filesCollection, bool anyLenFiles, bool moveBack) = Verify();
_FileGroups = Shared.Models.Stateless.Methods.IPath.GetKeyValuePairs(propertyConfiguration, appSettings.CopyTo, [appSettings.ResultDirectoryKey]);
ReadOnlyDictionary<string, ReadOnlyDictionary<byte, ReadOnlyCollection<string>>> keyValuePairs =
Shared.Models.Stateless.Methods.IPath.GetKeyValuePairs(propertyConfiguration, appSettings.CopyTo, [appSettings.ResultDirectoryKey]);
_FileGroups = keyValuePairs[appSettings.ResultDirectoryKey];
List<string> lines = CopyDistinctFilesInDirectories(logger, move, filesCollection, anyLenFiles, moveBack);
if (lines.Count != 0)
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines);
@ -93,6 +95,76 @@ public class CopyDistinct
return (move, new(filesCollection), anyLenFiles, moveBack);
}
private List<string> CopyDistinctFilesInDirectories(ILogger<Program>? logger, bool move, ReadOnlyCollection<string[]> filesCollection, bool anyLenFiles, bool moveBack)
{
List<string> results = [];
ProgressBar progressBar;
string[] distinctDirectories;
ConsoleKey? consoleKey = null;
string message = nameof(CopyDistinct);
List<(FilePath, string)> toDoCollection;
int count = filesCollection.Select(l => l.Length).Sum();
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
if (moveBack)
{
if (!anyLenFiles)
throw new NotSupportedException();
(distinctDirectories, toDoCollection) = GetMoveBackToDoCollection(_PropertyConfiguration, filesCollection);
}
else
{
progressBar = new(count, message, options);
string key = string.IsNullOrEmpty(_AppSettings.ResultDirectoryKey) ? _PropertyConfiguration.ResultAllInOne : _AppSettings.ResultDirectoryKey;
if (key != _PropertyConfiguration.ResultContent)
throw new NotImplementedException("Changed but didn't update!");
ReadOnlyCollection<ReadOnlyCollection<FilePath>> filePathsCollection = IDirectory.GetFilePathCollections(_Configuration.PropertyConfiguration, filesCollection, useIgnoreExtensions: true);
(distinctDirectories, toDoCollection) = IDirectory.GetToDoCollection(_PropertyConfiguration, _AppSettings.CopyDuplicates, _AppSettings.IfCanUseId, filePathsCollection, _FileGroups, () => progressBar.Tick());
progressBar.Dispose();
}
foreach (string distinctDirectory in distinctDirectories)
{
if (!Directory.Exists(distinctDirectory))
_ = Directory.CreateDirectory(distinctDirectory);
}
if (move)
logger?.LogInformation($"Ready to Move {toDoCollection.Count} file(s)?");
else if (!moveBack)
logger?.LogInformation($"Ready to Copy {toDoCollection.Count} file(s)?");
else
logger?.LogInformation($"Ready to Move back {toDoCollection.Count} file(s)?");
for (int y = 0; y < int.MaxValue; y++)
{
if (move)
logger?.LogInformation("Press \"Y\" key to move file(s), \"N\" key to log file(s) or close console to not move files");
else if (!moveBack)
logger?.LogInformation("Press \"Y\" key to copy file(s), \"N\" key to log file(s) or close console to not copy files");
else
logger?.LogInformation("Press \"Y\" key to move back file(s), \"N\" key to log file(s) or close console to not move back files");
consoleKey = System.Console.ReadKey().Key;
if (consoleKey is ConsoleKey.Y or ConsoleKey.N)
break;
}
logger?.LogInformation(". . .");
if (consoleKey is null || consoleKey.Value != ConsoleKey.Y)
{
if (move || moveBack)
logger?.LogInformation("Nothing moved!");
else
logger?.LogInformation("Nothing copied!");
}
else
{
progressBar = new(count, message, options);
results.AddRange(IDirectory.CopyOrMove(toDoCollection, move, moveBack, () => progressBar.Tick()));
progressBar.Dispose();
if (move || moveBack)
logger?.LogInformation("Done moving");
else
logger?.LogInformation("Done copying");
}
return results;
}
private static (string[], List<(FilePath, string)>) GetMoveBackToDoCollection(Property.Models.Configuration propertyConfiguration, ReadOnlyCollection<string[]> filesCollection)
{
List<(FilePath, string)> results = [];
@ -159,74 +231,4 @@ public class CopyDistinct
return (distinctDirectories.ToArray(), results);
}
private List<string> CopyDistinctFilesInDirectories(ILogger<Program>? logger, bool move, ReadOnlyCollection<string[]> filesCollection, bool anyLenFiles, bool moveBack)
{
List<string> results = [];
ProgressBar progressBar;
string[] distinctDirectories;
ConsoleKey? consoleKey = null;
string message = nameof(CopyDistinct);
List<(FilePath, string)> toDoCollection;
int count = filesCollection.Select(l => l.Length).Sum();
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
if (moveBack)
{
if (!anyLenFiles)
throw new NotSupportedException();
(distinctDirectories, toDoCollection) = GetMoveBackToDoCollection(_PropertyConfiguration, filesCollection);
}
else
{
progressBar = new(count, message, options);
string key = string.IsNullOrEmpty(_AppSettings.ResultDirectoryKey) ? _PropertyConfiguration.ResultAllInOne : _AppSettings.ResultDirectoryKey;
if (key != _PropertyConfiguration.ResultContent)
throw new NotImplementedException("Changed but didn't update!");
ReadOnlyCollection<ReadOnlyCollection<FilePath>> filePathsCollection = IDirectory.GetFilePathCollections(_Configuration.PropertyConfiguration, filesCollection);
(distinctDirectories, toDoCollection) = IDirectory.GetToDoCollection(_PropertyConfiguration, _AppSettings.CopyDuplicates, _AppSettings.IfCanUseId, filePathsCollection, _FileGroups, () => progressBar.Tick());
progressBar.Dispose();
}
foreach (string distinctDirectory in distinctDirectories)
{
if (!Directory.Exists(distinctDirectory))
_ = Directory.CreateDirectory(distinctDirectory);
}
if (move)
logger?.LogInformation($"Ready to Move {toDoCollection.Count} file(s)?");
else if (!moveBack)
logger?.LogInformation($"Ready to Copy {toDoCollection.Count} file(s)?");
else
logger?.LogInformation($"Ready to Move back {toDoCollection.Count} file(s)?");
for (int y = 0; y < int.MaxValue; y++)
{
if (move)
logger?.LogInformation("Press \"Y\" key to move file(s), \"N\" key to log file(s) or close console to not move files");
else if (!moveBack)
logger?.LogInformation("Press \"Y\" key to copy file(s), \"N\" key to log file(s) or close console to not copy files");
else
logger?.LogInformation("Press \"Y\" key to move back file(s), \"N\" key to log file(s) or close console to not move back files");
consoleKey = System.Console.ReadKey().Key;
if (consoleKey is ConsoleKey.Y or ConsoleKey.N)
break;
}
logger?.LogInformation(". . .");
if (consoleKey is null || consoleKey.Value != ConsoleKey.Y)
{
if (move || moveBack)
logger?.LogInformation("Nothing moved!");
else
logger?.LogInformation("Nothing copied!");
}
else
{
progressBar = new(count, message, options);
results.AddRange(IDirectory.CopyOrMove(toDoCollection, move, moveBack, () => progressBar.Tick()));
progressBar.Dispose();
if (move || moveBack)
logger?.LogInformation("Done moving");
else
logger?.LogInformation("Done copying");
}
return results;
}
}