Used to re-organized folders
This commit is contained in:
113
Rename/Rename.cs
113
Rename/Rename.cs
@ -10,6 +10,7 @@ using View_by_Distance.Metadata.Models;
|
||||
using View_by_Distance.Metadata.Models.Stateless.Methods;
|
||||
using View_by_Distance.Rename.Models;
|
||||
using View_by_Distance.Shared.Models;
|
||||
using View_by_Distance.Shared.Models.Properties;
|
||||
using View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
|
||||
namespace View_by_Distance.Rename;
|
||||
@ -20,8 +21,7 @@ public class Rename : IRename
|
||||
private record ToDo(string? Directory, FileHolder FileHolder, string File, bool JsonFile);
|
||||
private record Record(DateTime DateTime, ExifDirectory ExifDirectory, string File, string JsonFile);
|
||||
|
||||
private readonly AppSettings _AppSettings;
|
||||
private readonly RenameConfiguration _RenameConfiguration;
|
||||
private ProgressBar? _ProgressBar;
|
||||
|
||||
public Rename(List<string> args, ILogger<Program>? logger, IConfigurationRoot configurationRoot, AppSettings appSettings, bool isSilent, IConsole console)
|
||||
{
|
||||
@ -31,31 +31,24 @@ public class Rename : IRename
|
||||
throw new NullReferenceException(nameof(args));
|
||||
if (console is null)
|
||||
throw new NullReferenceException(nameof(console));
|
||||
_AppSettings = appSettings;
|
||||
IRename rename = this;
|
||||
long ticks = DateTime.Now.Ticks;
|
||||
ResultConfiguration resultConfiguration = Metadata.Models.Binder.ResultConfiguration.Get(configurationRoot, appSettings.RequireRootDirectoryExists);
|
||||
MetadataConfiguration metadataConfiguration = Metadata.Models.Binder.MetadataConfiguration.Get(configurationRoot, resultConfiguration);
|
||||
RenameConfiguration renameConfiguration = Models.Binder.RenameConfiguration.Get(configurationRoot, metadataConfiguration);
|
||||
_RenameConfiguration = renameConfiguration;
|
||||
DirectoryInfo directoryInfo = new(Path.GetFullPath(resultConfiguration.RootDirectory));
|
||||
logger?.LogInformation("{RootDirectory}", directoryInfo.FullName);
|
||||
ReadOnlyCollection<Record> exifDirectories = GetExifDirectoryCollection(directoryInfo);
|
||||
ReadOnlyCollection<ToDo> toDoCollection = GetToDoCollection(logger, ticks, exifDirectories);
|
||||
ReadOnlyCollection<string> lines = RenameFilesInDirectories(toDoCollection);
|
||||
if (lines.Count != 0)
|
||||
{
|
||||
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines);
|
||||
_ = IPath.DeleteEmptyDirectories(directoryInfo.FullName);
|
||||
}
|
||||
RenameWork(logger, appSettings, rename, ticks, renameConfiguration);
|
||||
}
|
||||
|
||||
(ReadOnlyCollection<string>, FilePath?) IRename.ConvertAndGetFfmpegFiles(FilePath filePath)
|
||||
void IRename.Tick() =>
|
||||
_ProgressBar?.Tick();
|
||||
|
||||
(ReadOnlyCollection<string>, FilePath?) IRename.ConvertAndGetFfmpegFiles(IRenameConfiguration renameConfiguration, FilePath filePath)
|
||||
{
|
||||
List<string> results = [];
|
||||
FilePath? result;
|
||||
bool isIgnoreExtension;
|
||||
bool isValidImageFormatExtension = _RenameConfiguration.ValidImageFormatExtensions.Contains(filePath.ExtensionLowered);
|
||||
isIgnoreExtension = isValidImageFormatExtension && _RenameConfiguration.IgnoreExtensions.Contains(filePath.ExtensionLowered);
|
||||
bool isValidImageFormatExtension = renameConfiguration.ValidImageFormatExtensions.Contains(filePath.ExtensionLowered);
|
||||
isIgnoreExtension = isValidImageFormatExtension && renameConfiguration.IgnoreExtensions.Contains(filePath.ExtensionLowered);
|
||||
if (!isIgnoreExtension && isValidImageFormatExtension)
|
||||
result = null;
|
||||
else
|
||||
@ -69,11 +62,11 @@ public class Rename : IRename
|
||||
results.AddRange(Directory.GetFiles(filePath.DirectoryName, $"{filePath.Name}-*.jpg", SearchOption.TopDirectoryOnly));
|
||||
if (results.Count == 0)
|
||||
throw new Exception();
|
||||
result = IId.GetFilePath(_RenameConfiguration.MetadataConfiguration, results[0]);
|
||||
result = IId.GetFilePath(renameConfiguration.MetadataConfiguration, results[0]);
|
||||
if (!result.Name.EndsWith("-0001.jpg"))
|
||||
throw new Exception();
|
||||
isValidImageFormatExtension = _RenameConfiguration.ValidImageFormatExtensions.Contains(result.ExtensionLowered);
|
||||
isIgnoreExtension = isValidImageFormatExtension && _RenameConfiguration.IgnoreExtensions.Contains(result.ExtensionLowered);
|
||||
isValidImageFormatExtension = renameConfiguration.ValidImageFormatExtensions.Contains(result.ExtensionLowered);
|
||||
isIgnoreExtension = isValidImageFormatExtension && renameConfiguration.IgnoreExtensions.Contains(result.ExtensionLowered);
|
||||
if (isIgnoreExtension || !isValidImageFormatExtension)
|
||||
throw new Exception();
|
||||
if (result.DirectoryName is null)
|
||||
@ -117,7 +110,7 @@ public class Rename : IRename
|
||||
|
||||
#pragma warning restore CA1416
|
||||
|
||||
private void GetExifDirectoryCollection(IRename rename, List<(string, FileInfo, ExifDirectory)> exifDirectories, IEnumerable<string> files, A_Metadata metadata)
|
||||
private static void GetExifDirectoryCollection(IRename rename, RenameConfiguration renameConfiguration, List<(string, FileInfo, ExifDirectory)> exifDirectories, IEnumerable<string> files, A_Metadata metadata)
|
||||
{
|
||||
FileInfo fileInfo;
|
||||
FilePath filePath;
|
||||
@ -127,21 +120,18 @@ public class Rename : IRename
|
||||
DeterministicHashCode deterministicHashCode;
|
||||
foreach (string file in files)
|
||||
{
|
||||
filePath = IId.GetFilePath(_RenameConfiguration.MetadataConfiguration, file);
|
||||
if (filePath.ExtensionLowered is ".paddedId" or ".lsv")
|
||||
rename.Tick();
|
||||
filePath = IId.GetFilePath(renameConfiguration.MetadataConfiguration, file);
|
||||
if (renameConfiguration.SkipIdFiles && filePath.Id is not null && (filePath.IsIdFormat || filePath.IsPaddedIdFormat))
|
||||
continue;
|
||||
if (files.Contains($"{filePath.FullName}.paddedId"))
|
||||
continue;
|
||||
if (filePath.Id is not null && (filePath.IsIdFormat || filePath.IsPaddedIdFormat))
|
||||
continue;
|
||||
(ffmpegFiles, ffmpegFilePath) = rename.ConvertAndGetFfmpegFiles(filePath);
|
||||
(ffmpegFiles, ffmpegFilePath) = rename.ConvertAndGetFfmpegFiles(renameConfiguration, filePath);
|
||||
if (ffmpegFilePath is not null)
|
||||
filePath = ffmpegFilePath;
|
||||
if (filePath.Id is not null)
|
||||
deterministicHashCode = new(null, filePath.Id, null);
|
||||
else
|
||||
deterministicHashCode = rename.GetDeterministicHashCode(filePath);
|
||||
(fileInfo, exifDirectory) = metadata.GetMetadataCollection(_RenameConfiguration.MetadataConfiguration, filePath, deterministicHashCode);
|
||||
(fileInfo, exifDirectory) = metadata.GetMetadataCollection(renameConfiguration.MetadataConfiguration, filePath, deterministicHashCode);
|
||||
exifDirectories.Add(new(file, fileInfo, exifDirectory));
|
||||
foreach (string ffmpegFile in ffmpegFiles)
|
||||
File.Delete(ffmpegFile);
|
||||
@ -161,24 +151,24 @@ public class Rename : IRename
|
||||
return new(results);
|
||||
}
|
||||
|
||||
private ReadOnlyCollection<Record> GetExifDirectoryCollection(DirectoryInfo directoryInfo)
|
||||
private ReadOnlyCollection<Record> GetExifDirectoryCollection(IRename rename, AppSettings appSettings, RenameConfiguration renameConfiguration, DirectoryInfo directoryInfo)
|
||||
{
|
||||
ReadOnlyCollection<Record> results;
|
||||
IRename rename = this;
|
||||
List<(string, FileInfo, ExifDirectory)> exifDirectories = [];
|
||||
int appSettingsMaxDegreeOfParallelism = _AppSettings.MaxDegreeOfParallelism;
|
||||
int appSettingsMaxDegreeOfParallelism = appSettings.MaxDegreeOfParallelism;
|
||||
IEnumerable<string> files = Directory.EnumerateFiles(directoryInfo.FullName, "*", SearchOption.AllDirectories);
|
||||
A_Metadata metadata = new(_RenameConfiguration.MetadataConfiguration);
|
||||
A_Metadata metadata = new(renameConfiguration.MetadataConfiguration);
|
||||
_ProgressBar = new(123000, "EnumerateFiles load", new ProgressBarOptions() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true });
|
||||
if (appSettingsMaxDegreeOfParallelism == 1)
|
||||
GetExifDirectoryCollection(rename, exifDirectories, files, metadata);
|
||||
GetExifDirectoryCollection(rename, renameConfiguration, exifDirectories, files, metadata);
|
||||
else
|
||||
{
|
||||
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = appSettingsMaxDegreeOfParallelism };
|
||||
ProgressBar progressBar = new(123000, "EnumerateFiles load", new ProgressBarOptions() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true });
|
||||
files.AsParallel().ForAll(A_Metadata.SetExifDirectoryCollection(rename, _RenameConfiguration.MetadataConfiguration, metadata, exifDirectories, () => progressBar.Tick()));
|
||||
if (progressBar.CurrentTick != exifDirectories.Count)
|
||||
files.AsParallel().ForAll(A_Metadata.SetExifDirectoryCollection(rename, renameConfiguration, metadata, exifDirectories));
|
||||
if (_ProgressBar.CurrentTick != exifDirectories.Count)
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
_ProgressBar.Dispose();
|
||||
results = GetExifDirectoryCollection(exifDirectories);
|
||||
return results;
|
||||
}
|
||||
@ -194,16 +184,26 @@ public class Rename : IRename
|
||||
}
|
||||
}
|
||||
|
||||
private ReadOnlyCollection<ToDo> GetToDoCollection(ILogger<Program>? logger, long ticks, ReadOnlyCollection<Record> exifDirectories)
|
||||
private static string GetCheckDirectory(RenameConfiguration renameConfiguration, Record record, FileHolder fileHolder)
|
||||
{
|
||||
string? checkDirectory;
|
||||
if (fileHolder.DirectoryName is null)
|
||||
throw new NullReferenceException(nameof(fileHolder.DirectoryName));
|
||||
(int season, string seasonName) = IDate.GetSeason(record.DateTime.DayOfYear);
|
||||
string maker = IMetadata.GetMaker(record.ExifDirectory.ExifDirectoryBase);
|
||||
string splat = fileHolder.DirectoryName[^3..][1] == '!' ? fileHolder.DirectoryName[^3..] : string.Empty;
|
||||
string rootDirectory = renameConfiguration.MoveFilesToRoot ? renameConfiguration.MetadataConfiguration.ResultConfiguration.RootDirectory : fileHolder.DirectoryName;
|
||||
checkDirectory = Path.Combine(rootDirectory, "_ Destination _", $"{record.DateTime.Year}.{season} {seasonName} {maker.Split(' ')[0]}{splat}");
|
||||
return checkDirectory;
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<ToDo> GetToDoCollection(ILogger<Program>? logger, RenameConfiguration renameConfiguration, long ticks, ReadOnlyCollection<Record> exifDirectories)
|
||||
{
|
||||
List<ToDo> results = [];
|
||||
int season;
|
||||
string maker;
|
||||
Record record;
|
||||
string jsonFile;
|
||||
string paddedId;
|
||||
string checkFile;
|
||||
string seasonName;
|
||||
string directoryName;
|
||||
FileHolder fileHolder;
|
||||
string? checkDirectory;
|
||||
@ -214,6 +214,7 @@ public class Rename : IRename
|
||||
string jsonFileSubDirectory;
|
||||
int intMinValueLength = int.MinValue.ToString().Length;
|
||||
VerifyIntMinValueLength(exifDirectories, intMinValueLength);
|
||||
ResultConfiguration resultConfiguration = renameConfiguration.MetadataConfiguration.ResultConfiguration;
|
||||
ReadOnlyCollection<Record> records = new((from l in exifDirectories orderby l.DateTime select l).ToArray());
|
||||
for (int i = 0; i < records.Count; i++)
|
||||
{
|
||||
@ -223,12 +224,10 @@ public class Rename : IRename
|
||||
fileHolder = new(record.File);
|
||||
if (fileHolder.DirectoryName is null)
|
||||
continue;
|
||||
maker = IMetadata.GetMaker(record.ExifDirectory.ExifDirectoryBase);
|
||||
(season, seasonName) = IDate.GetSeason(record.DateTime.DayOfYear);
|
||||
checkDirectory = GetCheckDirectory(renameConfiguration, record, fileHolder);
|
||||
checkFileExtension = fileHolder.ExtensionLowered == jpeg ? jpg : fileHolder.ExtensionLowered;
|
||||
checkDirectory = Path.Combine(fileHolder.DirectoryName, $"{record.DateTime.Year}.{season} {seasonName}{maker}");
|
||||
jsonFileSubDirectory = Path.GetDirectoryName(Path.GetDirectoryName(record.JsonFile)) ?? throw new Exception();
|
||||
paddedId = IId.GetPaddedId(intMinValueLength, _RenameConfiguration.MetadataConfiguration.Offset + i, record.ExifDirectory.Id.Value);
|
||||
paddedId = IId.GetPaddedId(intMinValueLength, renameConfiguration.MetadataConfiguration.Offset + i, record.ExifDirectory.Id.Value);
|
||||
checkFile = Path.Combine(checkDirectory, $"{paddedId}{checkFileExtension}");
|
||||
if (checkFile == fileHolder.FullName)
|
||||
continue;
|
||||
@ -238,7 +237,7 @@ public class Rename : IRename
|
||||
if (File.Exists(checkFile))
|
||||
continue;
|
||||
}
|
||||
(directoryName, _) = IPath.GetDirectoryNameAndIndex(_RenameConfiguration.MetadataConfiguration.ResultConfiguration, record.ExifDirectory.Id.Value);
|
||||
(directoryName, _) = IPath.GetDirectoryNameAndIndex(resultConfiguration, record.ExifDirectory.Id.Value);
|
||||
jsonFile = Path.Combine(jsonFileSubDirectory, directoryName, $"{record.ExifDirectory.Id.Value}{checkFileExtension}.json");
|
||||
if (record.JsonFile != jsonFile)
|
||||
results.Add(new(null, new(record.JsonFile), jsonFile, JsonFile: true));
|
||||
@ -267,8 +266,10 @@ public class Rename : IRename
|
||||
{
|
||||
List<string> results = [];
|
||||
VerifyDirectories(toDoCollection);
|
||||
_ProgressBar = new(toDoCollection.Count, "Move Files", new ProgressBarOptions() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true });
|
||||
foreach (ToDo toDo in toDoCollection)
|
||||
{
|
||||
_ProgressBar.Tick();
|
||||
if (toDo.JsonFile)
|
||||
{
|
||||
if (File.Exists(toDo.File))
|
||||
@ -281,11 +282,29 @@ public class Rename : IRename
|
||||
{
|
||||
if (File.Exists(toDo.File))
|
||||
File.Delete(toDo.File);
|
||||
File.Move(toDo.FileHolder.FullName, toDo.File);
|
||||
try
|
||||
{ File.Move(toDo.FileHolder.FullName, toDo.File); }
|
||||
catch (Exception)
|
||||
{ continue; }
|
||||
results.Add($"{toDo.FileHolder.FullName}\t{toDo.File}");
|
||||
}
|
||||
}
|
||||
_ProgressBar.Dispose();
|
||||
return new(results);
|
||||
}
|
||||
|
||||
private void RenameWork(ILogger<Program>? logger, AppSettings appSettings, IRename rename, long ticks, RenameConfiguration renameConfiguration)
|
||||
{
|
||||
DirectoryInfo directoryInfo = new(Path.GetFullPath(renameConfiguration.MetadataConfiguration.ResultConfiguration.RootDirectory));
|
||||
logger?.LogInformation("{RootDirectory}", directoryInfo.FullName);
|
||||
ReadOnlyCollection<Record> exifDirectories = GetExifDirectoryCollection(rename, appSettings, renameConfiguration, directoryInfo);
|
||||
ReadOnlyCollection<ToDo> toDoCollection = GetToDoCollection(logger, renameConfiguration, ticks, exifDirectories);
|
||||
ReadOnlyCollection<string> lines = RenameFilesInDirectories(toDoCollection);
|
||||
if (lines.Count != 0)
|
||||
{
|
||||
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines);
|
||||
_ = IPath.DeleteEmptyDirectories(directoryInfo.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user