AA.Compare Project to Match not runToDoCollectionFirst
Removed Layered AppSettings with Nested Objects at First Level
This commit is contained in:
210
Rename/Rename.cs
210
Rename/Rename.cs
@ -49,10 +49,10 @@ public partial class Rename : IRename, IDisposable
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
ReadOnlyCollection<string> IRename.ConvertAndGetFastForwardMovingPictureExpertsGroupFiles(IRenameConfiguration renameConfiguration, FilePath filePath)
|
||||
ReadOnlyCollection<string> IRename.ConvertAndGetFastForwardMovingPictureExpertsGroupFiles(IRenameSettings renameSettings, FilePath filePath)
|
||||
{
|
||||
List<string> results = [];
|
||||
bool isValidVideoFormatExtensions = renameConfiguration.ValidVideoFormatExtensions.Contains(filePath.ExtensionLowered);
|
||||
bool isValidVideoFormatExtensions = renameSettings.ValidVideoFormatExtensions.Contains(filePath.ExtensionLowered);
|
||||
if (isValidVideoFormatExtensions)
|
||||
{
|
||||
bool check;
|
||||
@ -79,7 +79,7 @@ public partial class Rename : IRename, IDisposable
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
return new(results);
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
#pragma warning disable CA1416
|
||||
@ -117,22 +117,22 @@ public partial class Rename : IRename, IDisposable
|
||||
|
||||
#pragma warning restore CA1416
|
||||
|
||||
private void NonParallelismAndInPlace(RenameConfiguration renameConfiguration, ReadOnlyCollection<int> ids, ExifDirectory exifDirectory, FileInfo fileInfo, FilePath filePath, bool fastForwardMovingPictureExpertsGroupUsed, ReadOnlyCollection<FileHolder> sidecarFiles)
|
||||
private void NonParallelismAndInPlace(AppSettings appSettings, ReadOnlyCollection<int> ids, ExifDirectory exifDirectory, FileInfo fileInfo, FilePath filePath, bool fastForwardMovingPictureExpertsGroupUsed, ReadOnlyCollection<FileHolder> sidecarFiles)
|
||||
{
|
||||
if (exifDirectory.Id is null)
|
||||
if (exifDirectory.FilePath.Id is null)
|
||||
throw new NotImplementedException();
|
||||
int i = 0;
|
||||
ToDo toDo;
|
||||
const string jpg = ".jpg";
|
||||
const string jpeg = ".jpeg";
|
||||
List<ToDo> toDoCollection = [];
|
||||
DateTime? dateTime = IDate.GetDateTimeOriginal(exifDirectory);
|
||||
ReadOnlyCollection<string> keywords = IMetadata.GetKeywords(exifDirectory);
|
||||
MetadataConfiguration metadataConfiguration = renameConfiguration.MetadataConfiguration;
|
||||
bool hasIgnoreKeyword = metadataConfiguration.IgnoreRulesKeyWords.Any(keywords.Contains);
|
||||
bool hasIgnoreKeyword = appSettings.MetadataSettings.IgnoreRulesKeyWords.Any(keywords.Contains);
|
||||
string checkFileExtension = filePath.ExtensionLowered == jpeg ? jpg : filePath.ExtensionLowered;
|
||||
bool hasDateTimeOriginal = dateTime is not null;
|
||||
string paddedId = IId.GetPaddedId(metadataConfiguration, exifDirectory.Id.Value, hasIgnoreKeyword, hasDateTimeOriginal, i);
|
||||
string checkDirectory = renameConfiguration.InPlaceWithOriginalName ? Path.Combine(filePath.DirectoryFullPath, filePath.FileNameFirstSegment) : filePath.DirectoryFullPath;
|
||||
string paddedId = IId.GetPaddedId(appSettings.ResultSettings, appSettings.MetadataSettings, exifDirectory.FilePath.Id.Value, hasIgnoreKeyword, hasDateTimeOriginal, i);
|
||||
string checkDirectory = appSettings.RenameSettings.InPlaceWithOriginalName ? Path.Combine(filePath.DirectoryFullPath, filePath.FileNameFirstSegment) : filePath.DirectoryFullPath;
|
||||
string checkFile = Path.Combine(checkDirectory, $"{paddedId}{checkFileExtension}");
|
||||
if (checkFile != filePath.FullName)
|
||||
{
|
||||
@ -142,21 +142,22 @@ public partial class Rename : IRename, IDisposable
|
||||
if (File.Exists(checkFile))
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
toDoCollection.Add(new(checkDirectory, filePath, checkFile, JsonFile: false));
|
||||
toDo = new(checkDirectory, filePath, checkFile, JsonFile: false);
|
||||
toDoCollection.Add(toDo);
|
||||
if (sidecarFiles.Count != 0)
|
||||
{
|
||||
if (renameConfiguration.InPlace)
|
||||
throw new NotSupportedException($"Must use {nameof(renameConfiguration.InPlaceWithOriginalName)} when sidecar file(s) are present!");
|
||||
if (appSettings.RenameSettings.InPlace)
|
||||
throw new NotSupportedException($"Must use {nameof(appSettings.RenameSettings.InPlaceWithOriginalName)} when sidecar file(s) are present!");
|
||||
dateTime ??= IDate.GetMinimum(exifDirectory);
|
||||
RecordB recordB = new(dateTime.Value, exifDirectory, fastForwardMovingPictureExpertsGroupUsed, filePath, sidecarFiles, hasDateTimeOriginal, hasIgnoreKeyword, fileInfo.FullName);
|
||||
toDoCollection.AddRange(GetSidecarFiles(metadataConfiguration, recordB, [], checkDirectory, paddedId));
|
||||
toDoCollection.AddRange(GetSidecarFiles(appSettings, recordB, [], checkDirectory, paddedId));
|
||||
}
|
||||
_ = RenameFilesInDirectories(renameConfiguration, new(toDoCollection));
|
||||
_ = RenameFilesInDirectories(appSettings.RenameSettings, new(toDoCollection));
|
||||
string jsonFile = Path.Combine(checkDirectory, $"{paddedId}{checkFileExtension}.json");
|
||||
File.Move(fileInfo.FullName, jsonFile, overwrite: true);
|
||||
if (renameConfiguration.InPlaceWithOriginalName && ids.Count > 0)
|
||||
if (appSettings.RenameSettings.InPlaceWithOriginalName && ids.Count > 0)
|
||||
{
|
||||
string contains = ids.Contains(exifDirectory.Id.Value) ? "_ Exists _" : "_ New _";
|
||||
string contains = ids.Contains(exifDirectory.FilePath.Id.Value) ? "_ Exists _" : "_ New _";
|
||||
string idCheck = Path.Combine(checkDirectory, contains, fastForwardMovingPictureExpertsGroupUsed ? "Video" : "Image");
|
||||
if (!Directory.Exists(idCheck))
|
||||
_ = Directory.CreateDirectory(idCheck);
|
||||
@ -164,16 +165,16 @@ public partial class Rename : IRename, IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private List<RecordA> GetRecordACollection(ILogger<Program>? logger, RenameConfiguration renameConfiguration, IRename rename, ReadOnlyCollection<int> ids, IEnumerable<string> files, A_Metadata metadata)
|
||||
private List<RecordA> GetRecordACollection(ILogger<Program>? logger, AppSettings appSettings, IRename rename, ReadOnlyCollection<int> ids, IEnumerable<string> files, A_Metadata metadata)
|
||||
{
|
||||
List<RecordA> results = [];
|
||||
int index = -1;
|
||||
RecordA recordA;
|
||||
FileInfo fileInfo;
|
||||
FilePath filePath;
|
||||
string directoryName;
|
||||
ExifDirectory exifDirectory;
|
||||
List<FileHolder> sidecarFiles;
|
||||
DeterministicHashCode deterministicHashCode;
|
||||
bool fastForwardMovingPictureExpertsGroupUsed;
|
||||
FilePath? fastForwardMovingPictureExpertsGroupFilePath;
|
||||
ReadOnlyCollection<string>? fastForwardMovingPictureExpertsGroupFiles;
|
||||
@ -182,36 +183,34 @@ public partial class Rename : IRename, IDisposable
|
||||
{
|
||||
index += 1;
|
||||
rename.Tick();
|
||||
if (keyValuePair.Value.Count > 1 && !renameConfiguration.ForceNewId)
|
||||
if (keyValuePair.Value.Count > 1 && !appSettings.RenameSettings.ForceNewId)
|
||||
{
|
||||
if (renameConfiguration.InPlaceMoveDirectory)
|
||||
if (appSettings.RenameSettings.InPlaceMoveDirectory)
|
||||
continue;
|
||||
throw new NotSupportedException($"When sidecar files are present {nameof(renameConfiguration.ForceNewId)} must be true!");
|
||||
throw new NotSupportedException($"When sidecar files are present {nameof(appSettings.RenameSettings.ForceNewId)} must be true!");
|
||||
}
|
||||
if (keyValuePair.Value.Count > 2)
|
||||
throw new NotSupportedException("Too many sidecar files!");
|
||||
foreach (FileHolder fileHolder in keyValuePair.Value)
|
||||
{
|
||||
if (renameConfiguration.SidecarExtensions.Contains(fileHolder.ExtensionLowered))
|
||||
if (appSettings.RenameSettings.SidecarExtensions.Contains(fileHolder.ExtensionLowered))
|
||||
continue;
|
||||
if (renameConfiguration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered))
|
||||
if (appSettings.RenameSettings.IgnoreExtensions.Contains(fileHolder.ExtensionLowered))
|
||||
continue;
|
||||
filePath = FilePath.Get(renameConfiguration.MetadataConfiguration, fileHolder, index);
|
||||
if (renameConfiguration.SkipIdFiles && filePath.Id is not null && (filePath.IsIntelligentIdFormat || filePath.SortOrder is not null))
|
||||
filePath = FilePath.Get(appSettings.ResultSettings, appSettings.MetadataSettings, fileHolder, index);
|
||||
if (appSettings.RenameSettings.SkipIdFiles && filePath.Id is not null && (filePath.IsIntelligentIdFormat || filePath.SortOrder is not null))
|
||||
continue;
|
||||
if (!renameConfiguration.ForceNewId && filePath.Id is not null)
|
||||
if (!appSettings.RenameSettings.ForceNewId && filePath.Id is not null)
|
||||
{
|
||||
fastForwardMovingPictureExpertsGroupFiles = null;
|
||||
deterministicHashCode = new(null, filePath.Id, null);
|
||||
directoryName = Path.GetFileName(filePath.DirectoryFullPath);
|
||||
if (renameConfiguration.InPlaceWithOriginalName || (renameConfiguration.InPlace && directoryName.EndsWith(filePath.Id.Value.ToString())))
|
||||
if (appSettings.RenameSettings.InPlaceWithOriginalName || (appSettings.RenameSettings.InPlace && directoryName.EndsWith(filePath.Id.Value.ToString())))
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
fastForwardMovingPictureExpertsGroupFiles = rename.ConvertAndGetFastForwardMovingPictureExpertsGroupFiles(renameConfiguration, filePath);
|
||||
fastForwardMovingPictureExpertsGroupFilePath = fastForwardMovingPictureExpertsGroupFiles.Count == 0 ? null : FilePath.Get(renameConfiguration.MetadataConfiguration, FileHolder.Get(fastForwardMovingPictureExpertsGroupFiles[0]), index);
|
||||
deterministicHashCode = fastForwardMovingPictureExpertsGroupFilePath is null ? rename.GetDeterministicHashCode(filePath) : rename.GetDeterministicHashCode(fastForwardMovingPictureExpertsGroupFilePath);
|
||||
fastForwardMovingPictureExpertsGroupFiles = rename.ConvertAndGetFastForwardMovingPictureExpertsGroupFiles(appSettings.RenameSettings, filePath);
|
||||
fastForwardMovingPictureExpertsGroupFilePath = fastForwardMovingPictureExpertsGroupFiles.Count == 0 ? null : FilePath.Get(appSettings.ResultSettings, appSettings.MetadataSettings, FileHolder.Get(fastForwardMovingPictureExpertsGroupFiles[0]), index);
|
||||
}
|
||||
sidecarFiles = [];
|
||||
for (int i = 0; i < keyValuePair.Value.Count; i++)
|
||||
@ -221,7 +220,7 @@ public partial class Rename : IRename, IDisposable
|
||||
sidecarFiles.Add(keyValuePair.Value[i]);
|
||||
}
|
||||
try
|
||||
{ (fileInfo, exifDirectory) = metadata.GetMetadataCollection(renameConfiguration.MetadataConfiguration, filePath, deterministicHashCode); }
|
||||
{ (fileInfo, exifDirectory) = metadata.GetMetadataCollection(appSettings.ResultSettings, appSettings.MetadataSettings, filePath); }
|
||||
catch (Exception)
|
||||
{
|
||||
logger?.LogWarning("<{filePath}>", filePath.FullName);
|
||||
@ -233,19 +232,21 @@ public partial class Rename : IRename, IDisposable
|
||||
foreach (string fastForwardMovingPictureExpertsGroupFile in fastForwardMovingPictureExpertsGroupFiles)
|
||||
File.Delete(fastForwardMovingPictureExpertsGroupFile);
|
||||
}
|
||||
if (renameConfiguration.InPlace || renameConfiguration.InPlaceWithOriginalName)
|
||||
NonParallelismAndInPlace(renameConfiguration, ids, exifDirectory, fileInfo, filePath, fastForwardMovingPictureExpertsGroupUsed, new(sidecarFiles));
|
||||
if (!fastForwardMovingPictureExpertsGroupUsed && renameConfiguration.InPlaceMoveDirectory && renameConfiguration.ValidVideoFormatExtensions.Contains(filePath.ExtensionLowered))
|
||||
if (appSettings.RenameSettings.InPlace || appSettings.RenameSettings.InPlaceWithOriginalName)
|
||||
NonParallelismAndInPlace(appSettings, ids, exifDirectory, fileInfo, filePath, fastForwardMovingPictureExpertsGroupUsed, new(sidecarFiles));
|
||||
if (!fastForwardMovingPictureExpertsGroupUsed && appSettings.RenameSettings.InPlaceMoveDirectory && appSettings.RenameSettings.ValidVideoFormatExtensions.Contains(filePath.ExtensionLowered))
|
||||
fastForwardMovingPictureExpertsGroupUsed = true;
|
||||
results.Add(new(exifDirectory, fastForwardMovingPictureExpertsGroupUsed, fileInfo, filePath, new(sidecarFiles)));
|
||||
recordA = new(exifDirectory, fastForwardMovingPictureExpertsGroupUsed, fileInfo, filePath, new(sidecarFiles));
|
||||
results.Add(recordA);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<RecordB> GetRecordBCollection(MetadataConfiguration metadataConfiguration, List<RecordA> recordACollection)
|
||||
private static ReadOnlyCollection<RecordB> GetRecordBCollection(AppSettings appSettings, List<RecordA> recordACollection)
|
||||
{
|
||||
List<RecordB> results = [];
|
||||
RecordB recordB;
|
||||
DateTime? dateTime;
|
||||
bool hasIgnoreKeyword;
|
||||
bool hasDateTimeOriginal;
|
||||
@ -256,52 +257,54 @@ public partial class Rename : IRename, IDisposable
|
||||
hasDateTimeOriginal = dateTime is not null;
|
||||
dateTime ??= IDate.GetMinimum(recordA.ExifDirectory);
|
||||
keywords = IMetadata.GetKeywords(recordA.ExifDirectory);
|
||||
hasIgnoreKeyword = metadataConfiguration.IgnoreRulesKeyWords.Any(l => keywords.Contains(l));
|
||||
results.Add(new(dateTime.Value, recordA.ExifDirectory, recordA.FastForwardMovingPictureExpertsGroupUsed, recordA.FilePath, recordA.SidecarFiles, hasDateTimeOriginal, hasIgnoreKeyword, recordA.FileInfo.FullName));
|
||||
hasIgnoreKeyword = appSettings.MetadataSettings.IgnoreRulesKeyWords.Any(l => keywords.Contains(l));
|
||||
recordB = new(dateTime.Value, recordA.ExifDirectory, recordA.FastForwardMovingPictureExpertsGroupUsed, recordA.FilePath, recordA.SidecarFiles, hasDateTimeOriginal, hasIgnoreKeyword, recordA.FileInfo.FullName);
|
||||
results.Add(recordB);
|
||||
}
|
||||
return new(results);
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
private ReadOnlyCollection<RecordB> GetRecordBCollection(ILogger<Program>? logger, AppSettings appSettings, IRename rename, ReadOnlyCollection<int> ids, DirectoryInfo directoryInfo)
|
||||
{
|
||||
ReadOnlyCollection<RecordB> results;
|
||||
RecordA recordA;
|
||||
List<RecordA> recordACollection = [];
|
||||
RenameConfiguration renameConfiguration = appSettings.RenameConfiguration;
|
||||
A_Metadata metadata = new(renameConfiguration.MetadataConfiguration);
|
||||
int appSettingsMaxDegreeOfParallelism = appSettings.MaxDegreeOfParallelism;
|
||||
A_Metadata metadata = new(appSettings.ResultSettings, appSettings.MetadataSettings);
|
||||
int appSettingsMaxDegreeOfParallelism = appSettings.RenameSettings.MaxDegreeOfParallelism;
|
||||
IEnumerable<string> files = appSettingsMaxDegreeOfParallelism == 1 ? Directory.GetFiles(directoryInfo.FullName, "*", SearchOption.AllDirectories) : Directory.EnumerateFiles(directoryInfo.FullName, "*", SearchOption.AllDirectories);
|
||||
int filesCount = appSettingsMaxDegreeOfParallelism == 1 ? files.Count() : 123000;
|
||||
_ProgressBar = new(filesCount, "EnumerateFiles load", new ProgressBarOptions() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true });
|
||||
if (appSettingsMaxDegreeOfParallelism == 1)
|
||||
recordACollection.AddRange(GetRecordACollection(logger, renameConfiguration, rename, ids, files, metadata));
|
||||
recordACollection.AddRange(GetRecordACollection(logger, appSettings, rename, ids, files, metadata));
|
||||
else
|
||||
{
|
||||
List<string> distinct = [];
|
||||
List<(bool, FilePath, FileInfo, ExifDirectory, ReadOnlyCollection<FileHolder>)> collection = [];
|
||||
List<MetadataGroup> metadataGroups = [];
|
||||
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = appSettingsMaxDegreeOfParallelism };
|
||||
files.AsParallel().ForAll(IMetadata.SetExifDirectoryCollection(rename, renameConfiguration, metadata, distinct, collection));
|
||||
files.AsParallel().ForAll(IMetadata.SetExifDirectoryCollection(rename, appSettings.ResultSettings, appSettings.MetadataSettings, appSettings.RenameSettings, metadata, distinct, metadataGroups));
|
||||
if (_ProgressBar.CurrentTick != recordACollection.Count)
|
||||
throw new NotSupportedException();
|
||||
foreach ((bool fastForwardMovingPictureExpertsGroupUsed, FilePath filePath, FileInfo fileInfo, ExifDirectory exifDirectory, ReadOnlyCollection<FileHolder> sidecarFiles) in collection)
|
||||
foreach (MetadataGroup metadataGroup in metadataGroups)
|
||||
{
|
||||
if (fastForwardMovingPictureExpertsGroupUsed || !renameConfiguration.InPlaceMoveDirectory || !renameConfiguration.ValidVideoFormatExtensions.Contains(filePath.ExtensionLowered))
|
||||
recordACollection.Add(new(exifDirectory, fastForwardMovingPictureExpertsGroupUsed, fileInfo, filePath, sidecarFiles));
|
||||
if (metadataGroup.FastForwardMovingPictureExpertsGroupUsed || !appSettings.RenameSettings.InPlaceMoveDirectory || !appSettings.RenameSettings.ValidVideoFormatExtensions.Contains(metadataGroup.FilePath.ExtensionLowered))
|
||||
recordA = new(metadataGroup.ExifDirectory, metadataGroup.FastForwardMovingPictureExpertsGroupUsed, metadataGroup.FileInfo, metadataGroup.FilePath, metadataGroup.SidecarFiles);
|
||||
else
|
||||
recordACollection.Add(new(exifDirectory, FastForwardMovingPictureExpertsGroupUsed: true, fileInfo, filePath, sidecarFiles));
|
||||
recordA = new(metadataGroup.ExifDirectory, FastForwardMovingPictureExpertsGroupUsed: true, metadataGroup.FileInfo, metadataGroup.FilePath, metadataGroup.SidecarFiles);
|
||||
recordACollection.Add(recordA);
|
||||
}
|
||||
}
|
||||
_ProgressBar.Dispose();
|
||||
results = GetRecordBCollection(renameConfiguration.MetadataConfiguration, recordACollection);
|
||||
results = GetRecordBCollection(appSettings, recordACollection);
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void VerifyIntMinValueLength(MetadataConfiguration metadataConfiguration, ReadOnlyCollection<RecordB> recordBCollection)
|
||||
private static void VerifyIntMinValueLength(MetadataSettings metadataSettings, ReadOnlyCollection<RecordB> recordBCollection)
|
||||
{
|
||||
foreach (RecordB recordB in recordBCollection)
|
||||
{
|
||||
if (recordB.ExifDirectory.Id is null)
|
||||
if (recordB.ExifDirectory.FilePath.Id is null)
|
||||
continue;
|
||||
if (metadataConfiguration.IntMinValueLength < recordB.ExifDirectory.Id.Value.ToString().Length)
|
||||
if (metadataSettings.IntMinValueLength < recordB.ExifDirectory.FilePath.Id.Value.ToString().Length)
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
@ -312,7 +315,7 @@ public partial class Rename : IRename, IDisposable
|
||||
private static string GetDirectoryName(string year, string tfw, string prefix, string? splat, int seasonValue, string seasonName, string makerSplit) =>
|
||||
splat is null ? $"{prefix}{year} {tfw}{year}.{seasonValue} {seasonName}{makerSplit}" : $"{prefix}{year} {tfw}{year}{splat}";
|
||||
|
||||
private static string? GetCheckDirectory(RenameConfiguration renameConfiguration, RecordB record, ReadOnlyCollection<int> ids, bool multipleDirectoriesWithFiles, string paddedId)
|
||||
private static string? GetCheckDirectory(AppSettings appSettings, RecordB record, ReadOnlyCollection<int> ids, bool multipleDirectoriesWithFiles, string paddedId)
|
||||
{
|
||||
string? result;
|
||||
string year = record.DateTime.Year.ToString();
|
||||
@ -322,7 +325,7 @@ public partial class Rename : IRename, IDisposable
|
||||
else
|
||||
{
|
||||
(bool? isWrongYear, string[] years) = IDate.IsWrongYear(record.FilePath, record.ExifDirectory);
|
||||
if (renameConfiguration.InPlaceMoveDirectory && !record.FilePath.FileNameFirstSegment.Contains(paddedId))
|
||||
if (appSettings.RenameSettings.InPlaceMoveDirectory && !record.FilePath.FileNameFirstSegment.Contains(paddedId))
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
@ -331,9 +334,9 @@ public partial class Rename : IRename, IDisposable
|
||||
string[] segments = checkDirectoryName.Split(years, StringSplitOptions.None);
|
||||
string? splat = checkDirectoryName[^3..][1] == '!' ? checkDirectoryName[^3..] : null;
|
||||
(int seasonValue, string seasonName) = IDate.GetSeason(record.DateTime.DayOfYear);
|
||||
string rootDirectory = renameConfiguration.MetadataConfiguration.ResultConfiguration.RootDirectory;
|
||||
string contains = record.ExifDirectory.Id is null || ids.Contains(record.ExifDirectory.Id.Value) ? "_ Exists _" : "_ New-Destination _";
|
||||
string makerSplit = string.IsNullOrEmpty(maker) ? string.IsNullOrEmpty(renameConfiguration.DefaultMaker) ? string.Empty : renameConfiguration.DefaultMaker : $" {maker.Split(' ')[0]}";
|
||||
string rootDirectory = appSettings.ResultSettings.RootDirectory;
|
||||
string contains = record.ExifDirectory.FilePath.Id is null || ids.Contains(record.ExifDirectory.FilePath.Id.Value) ? "_ Exists _" : "_ New-Destination _";
|
||||
string makerSplit = string.IsNullOrEmpty(maker) ? string.IsNullOrEmpty(appSettings.RenameSettings.DefaultMaker) ? string.Empty : appSettings.RenameSettings.DefaultMaker : $" {maker.Split(' ')[0]}";
|
||||
string directoryName = GetDirectoryName(year, tfw, segments[0], splat, seasonValue, seasonName, makerSplit);
|
||||
result = Path.GetFullPath(Path.Combine(rootDirectory, contains, directoryName));
|
||||
}
|
||||
@ -341,16 +344,17 @@ public partial class Rename : IRename, IDisposable
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<ToDo> GetSidecarFiles(MetadataConfiguration metadataConfiguration, RecordB record, List<string> distinct, string checkDirectory, string paddedId)
|
||||
private static List<ToDo> GetSidecarFiles(AppSettings appSettings, RecordB record, List<string> distinct, string checkDirectory, string paddedId)
|
||||
{
|
||||
List<ToDo> results = [];
|
||||
ToDo toDo;
|
||||
string checkFile;
|
||||
FilePath filePath;
|
||||
string checkFileExtension;
|
||||
foreach (FileHolder fileHolder in record.SidecarFiles)
|
||||
{
|
||||
checkFileExtension = fileHolder.ExtensionLowered;
|
||||
filePath = FilePath.Get(metadataConfiguration, fileHolder, index: null);
|
||||
filePath = FilePath.Get(appSettings.ResultSettings, appSettings.MetadataSettings, fileHolder, index: null);
|
||||
checkFile = Path.Combine(checkDirectory, $"{paddedId}{checkFileExtension}");
|
||||
if (checkFile == filePath.FullName)
|
||||
continue;
|
||||
@ -363,16 +367,17 @@ public partial class Rename : IRename, IDisposable
|
||||
if (distinct.Contains(checkFile))
|
||||
continue;
|
||||
distinct.Add(checkFile);
|
||||
results.Add(new(checkDirectory, filePath, checkFile, JsonFile: false));
|
||||
toDo = new(checkDirectory, filePath, checkFile, JsonFile: false);
|
||||
results.Add(toDo);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static bool? GetDirectoryCheck(RenameConfiguration renameConfiguration)
|
||||
private static bool? GetDirectoryCheck(ResultSettings resultSettings)
|
||||
{
|
||||
bool? result = null;
|
||||
IEnumerable<string> files;
|
||||
string[] directories = Directory.GetDirectories(renameConfiguration.MetadataConfiguration.ResultConfiguration.RootDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
string[] directories = Directory.GetDirectories(resultSettings.RootDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
files = Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories);
|
||||
@ -390,9 +395,10 @@ public partial class Rename : IRename, IDisposable
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<ToDo> GetToDoCollection(RenameConfiguration renameConfiguration, ReadOnlyCollection<int> ids, ReadOnlyCollection<RecordB> recordBCollection)
|
||||
private static ReadOnlyCollection<ToDo> GetToDoCollection(AppSettings appSettings, ReadOnlyCollection<int> ids, ReadOnlyCollection<RecordB> recordBCollection)
|
||||
{
|
||||
List<ToDo> results = [];
|
||||
ToDo toDo;
|
||||
RecordB record;
|
||||
string jsonFile;
|
||||
string paddedId;
|
||||
@ -406,18 +412,17 @@ public partial class Rename : IRename, IDisposable
|
||||
List<string> distinct = [];
|
||||
const string jpeg = ".jpeg";
|
||||
string jsonFileSubDirectory;
|
||||
bool? directoryCheck = GetDirectoryCheck(renameConfiguration);
|
||||
MetadataConfiguration metadataConfiguration = renameConfiguration.MetadataConfiguration;
|
||||
VerifyIntMinValueLength(metadataConfiguration, recordBCollection);
|
||||
bool? directoryCheck = GetDirectoryCheck(appSettings.ResultSettings);
|
||||
VerifyIntMinValueLength(appSettings.MetadataSettings, recordBCollection);
|
||||
bool multipleDirectoriesWithFiles = directoryCheck is not null && directoryCheck.Value;
|
||||
ReadOnlyCollection<RecordB> sorted = new((from l in recordBCollection orderby l.DateTime select l).ToArray());
|
||||
ReadOnlyCollection<RecordB> sorted = (from l in recordBCollection orderby l.DateTime select l).ToArray().AsReadOnly();
|
||||
for (int i = 0; i < sorted.Count; i++)
|
||||
{
|
||||
record = sorted[i];
|
||||
if (record.ExifDirectory.Id is null)
|
||||
if (record.ExifDirectory.FilePath.Id is null)
|
||||
continue;
|
||||
paddedId = IId.GetPaddedId(metadataConfiguration, record.ExifDirectory.Id.Value, record.HasIgnoreKeyword, record.HasDateTimeOriginal, i);
|
||||
checkDirectory = GetCheckDirectory(renameConfiguration, record, ids, multipleDirectoriesWithFiles, paddedId);
|
||||
paddedId = IId.GetPaddedId(appSettings.ResultSettings, appSettings.MetadataSettings, record.ExifDirectory.FilePath.Id.Value, record.HasIgnoreKeyword, record.HasDateTimeOriginal, i);
|
||||
checkDirectory = GetCheckDirectory(appSettings, record, ids, multipleDirectoriesWithFiles, paddedId);
|
||||
if (string.IsNullOrEmpty(checkDirectory))
|
||||
continue;
|
||||
checkFileExtension = record.FilePath.ExtensionLowered == jpeg ? jpg : record.FilePath.ExtensionLowered;
|
||||
@ -431,23 +436,25 @@ public partial class Rename : IRename, IDisposable
|
||||
if (File.Exists(checkFile))
|
||||
continue;
|
||||
}
|
||||
(directoryName, _) = IPath.GetDirectoryNameAndIndex(metadataConfiguration.ResultConfiguration, record.ExifDirectory.Id.Value);
|
||||
jsonFile = Path.Combine(jsonFileSubDirectory, directoryName, $"{record.ExifDirectory.Id.Value}{checkFileExtension}.json");
|
||||
(directoryName, _) = IPath.GetDirectoryNameAndIndex(appSettings.ResultSettings, record.ExifDirectory.FilePath.Id.Value);
|
||||
jsonFile = Path.Combine(jsonFileSubDirectory, directoryName, $"{record.ExifDirectory.FilePath.Id.Value}{checkFileExtension}.json");
|
||||
if (record.JsonFile != jsonFile)
|
||||
{
|
||||
fileHolder = FileHolder.Get(record.JsonFile);
|
||||
filePath = FilePath.Get(metadataConfiguration, fileHolder, index: null);
|
||||
results.Add(new(null, filePath, jsonFile, JsonFile: true));
|
||||
filePath = FilePath.Get(appSettings.ResultSettings, appSettings.MetadataSettings, fileHolder, index: null);
|
||||
toDo = new(null, filePath, jsonFile, JsonFile: true);
|
||||
results.Add(toDo);
|
||||
}
|
||||
if (distinct.Contains(checkFile))
|
||||
continue;
|
||||
distinct.Add(checkFile);
|
||||
results.Add(new(checkDirectory, record.FilePath, checkFile, JsonFile: false));
|
||||
toDo = new(checkDirectory, record.FilePath, checkFile, JsonFile: false);
|
||||
results.Add(toDo);
|
||||
if (record.SidecarFiles.Count == 0)
|
||||
continue;
|
||||
results.AddRange(GetSidecarFiles(metadataConfiguration, record, distinct, checkDirectory, paddedId));
|
||||
results.AddRange(GetSidecarFiles(appSettings, record, distinct, checkDirectory, paddedId));
|
||||
}
|
||||
return new(results);
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
private static void VerifyDirectories(ReadOnlyCollection<ToDo> toDoCollection)
|
||||
@ -463,11 +470,11 @@ public partial class Rename : IRename, IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private ReadOnlyCollection<string> RenameFilesInDirectories(RenameConfiguration renameConfiguration, ReadOnlyCollection<ToDo> toDoCollection)
|
||||
private ReadOnlyCollection<string> RenameFilesInDirectories(RenameSettings renameSettings, ReadOnlyCollection<ToDo> toDoCollection)
|
||||
{
|
||||
List<string> results = [];
|
||||
VerifyDirectories(toDoCollection);
|
||||
bool useProgressBar = !renameConfiguration.InPlace && !renameConfiguration.InPlaceWithOriginalName;
|
||||
bool useProgressBar = !renameSettings.InPlace && !renameSettings.InPlaceWithOriginalName;
|
||||
if (useProgressBar)
|
||||
_ProgressBar = new(toDoCollection.Count, "Move Files", new ProgressBarOptions() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true });
|
||||
foreach (ToDo toDo in toDoCollection)
|
||||
@ -498,61 +505,58 @@ public partial class Rename : IRename, IDisposable
|
||||
}
|
||||
if (useProgressBar)
|
||||
_ProgressBar?.Dispose();
|
||||
return new(results);
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
private static void SaveIdentifiersToDisk(long ticks, RenameConfiguration renameConfiguration, ReadOnlyCollection<RecordB> recordBCollection)
|
||||
private static void SaveIdentifiersToDisk(long ticks, AppSettings appSettings, ReadOnlyCollection<RecordB> recordBCollection)
|
||||
{
|
||||
string paddedId;
|
||||
Identifier identifier;
|
||||
List<Identifier> identifiers = [];
|
||||
MetadataConfiguration metadataConfiguration = renameConfiguration.MetadataConfiguration;
|
||||
string aMetadataCollectionDirectory = IResult.GetResultsDateGroupDirectory(metadataConfiguration.ResultConfiguration, nameof(A_Metadata), metadataConfiguration.ResultConfiguration.ResultCollection);
|
||||
string aMetadataCollectionDirectory = IResult.GetResultsDateGroupDirectory(appSettings.ResultSettings, nameof(A_Metadata), appSettings.ResultSettings.ResultCollection);
|
||||
foreach (RecordB record in recordBCollection)
|
||||
{
|
||||
if (record.ExifDirectory.Id is null)
|
||||
if (record.ExifDirectory.FilePath.Id is null)
|
||||
continue;
|
||||
paddedId = IId.GetPaddedId(renameConfiguration.MetadataConfiguration, record.ExifDirectory.Id.Value, record.HasIgnoreKeyword, record.HasDateTimeOriginal, index: null);
|
||||
identifier = new([], record.HasDateTimeOriginal, record.ExifDirectory.Id.Value, record.FilePath.Length, paddedId, record.DateTime.Ticks);
|
||||
paddedId = IId.GetPaddedId(appSettings.ResultSettings, appSettings.MetadataSettings, record.ExifDirectory.FilePath.Id.Value, record.HasIgnoreKeyword, record.HasDateTimeOriginal, index: null);
|
||||
identifier = new([], record.HasDateTimeOriginal, record.ExifDirectory.FilePath.Id.Value, record.FilePath.Length, paddedId, record.DateTime.Ticks);
|
||||
identifiers.Add(identifier);
|
||||
}
|
||||
string json = JsonSerializer.Serialize(identifiers.OrderBy(l => l.PaddedId).ToArray(), IdentifierCollectionSourceGenerationContext.Default.IdentifierArray);
|
||||
_ = IPath.WriteAllText(Path.Combine(aMetadataCollectionDirectory, $"{ticks}.json"), json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<int> GetIds(RenameConfiguration renameConfiguration)
|
||||
private static ReadOnlyCollection<int> GetIds(RenameSettings renameSettings)
|
||||
{
|
||||
ReadOnlyCollection<int> results;
|
||||
string? propertyCollectionFile = string.IsNullOrEmpty(renameConfiguration.RelativePropertyCollectionFile) ? null : renameConfiguration.RelativePropertyCollectionFile;
|
||||
string? propertyCollectionFile = string.IsNullOrEmpty(renameSettings.RelativePropertyCollectionFile) ? null : renameSettings.RelativePropertyCollectionFile;
|
||||
string? json = !File.Exists(propertyCollectionFile) ? null : File.ReadAllText(propertyCollectionFile);
|
||||
Identifier[]? identifiers = json is null ? null : JsonSerializer.Deserialize(json, IdentifierCollectionSourceGenerationContext.Default.IdentifierArray);
|
||||
if (identifiers is null && !string.IsNullOrEmpty(renameConfiguration.RelativePropertyCollectionFile))
|
||||
throw new Exception($"Invalid {nameof(renameConfiguration.RelativePropertyCollectionFile)}");
|
||||
if (identifiers is null && !string.IsNullOrEmpty(renameSettings.RelativePropertyCollectionFile))
|
||||
throw new Exception($"Invalid {nameof(renameSettings.RelativePropertyCollectionFile)}");
|
||||
results = identifiers is null ? new([]) : new((from l in identifiers select l.Id).ToArray());
|
||||
return results;
|
||||
}
|
||||
|
||||
private void RenameWork(ILogger<Program>? logger, AppSettings appSettings, IRename rename, long ticks)
|
||||
{
|
||||
ReadOnlyCollection<int> ids = GetIds(appSettings.RenameConfiguration);
|
||||
RenameConfiguration renameConfiguration = appSettings.RenameConfiguration;
|
||||
MetadataConfiguration metadataConfiguration = renameConfiguration.MetadataConfiguration;
|
||||
_ = IPath.DeleteEmptyDirectories(metadataConfiguration.ResultConfiguration.RootDirectory);
|
||||
DirectoryInfo directoryInfo = new(Path.GetFullPath(metadataConfiguration.ResultConfiguration.RootDirectory));
|
||||
ReadOnlyCollection<int> ids = GetIds(appSettings.RenameSettings);
|
||||
_ = IPath.DeleteEmptyDirectories(appSettings.ResultSettings.RootDirectory);
|
||||
DirectoryInfo directoryInfo = new(Path.GetFullPath(appSettings.ResultSettings.RootDirectory));
|
||||
logger?.LogInformation("{Ticks} {RootDirectory}", ticks, directoryInfo.FullName);
|
||||
ReadOnlyCollection<RecordB> recordBCollection = GetRecordBCollection(logger, appSettings, rename, ids, directoryInfo);
|
||||
SaveIdentifiersToDisk(ticks, renameConfiguration, recordBCollection);
|
||||
if (renameConfiguration.InPlace || renameConfiguration.InPlaceWithOriginalName)
|
||||
SaveIdentifiersToDisk(ticks, appSettings, recordBCollection);
|
||||
if (appSettings.RenameSettings.InPlace || appSettings.RenameSettings.InPlaceWithOriginalName)
|
||||
{
|
||||
if (recordBCollection.Count > 0)
|
||||
recordBCollection = new([]);
|
||||
string aMetadataSingletonDirectory = IResult.GetResultsGroupDirectory(metadataConfiguration.ResultConfiguration, nameof(A_Metadata));
|
||||
string aMetadataSingletonDirectory = IResult.GetResultsGroupDirectory(appSettings.ResultSettings, nameof(A_Metadata));
|
||||
_ = IPath.DeleteEmptyDirectories(aMetadataSingletonDirectory);
|
||||
}
|
||||
if (!renameConfiguration.OnlySaveIdentifiersToDisk)
|
||||
if (!appSettings.RenameSettings.OnlySaveIdentifiersToDisk)
|
||||
{
|
||||
ReadOnlyCollection<ToDo> toDoCollection = GetToDoCollection(renameConfiguration, ids, recordBCollection);
|
||||
ReadOnlyCollection<string> lines = RenameFilesInDirectories(renameConfiguration, toDoCollection);
|
||||
ReadOnlyCollection<ToDo> toDoCollection = GetToDoCollection(appSettings, ids, recordBCollection);
|
||||
ReadOnlyCollection<string> lines = RenameFilesInDirectories(appSettings.RenameSettings, toDoCollection);
|
||||
if (lines.Count != 0)
|
||||
{
|
||||
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines);
|
||||
|
Reference in New Issue
Block a user