Rename
editorconfig
This commit is contained in:
@ -3,6 +3,7 @@ using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Phares.Shared;
|
||||
using ShellProgressBar;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text;
|
||||
using View_by_Distance.Metadata.Models;
|
||||
using View_by_Distance.Rename.Models;
|
||||
@ -15,6 +16,9 @@ namespace View_by_Distance.Rename;
|
||||
public class Rename
|
||||
{
|
||||
|
||||
private record Result(string File,
|
||||
ReadOnlyDictionary<string, MetadataExtractorDirectory> KeyValuePairs);
|
||||
|
||||
private record Record(int Index,
|
||||
bool IsIgnoreExtension,
|
||||
bool IsValidImageFormatExtension,
|
||||
@ -45,11 +49,17 @@ public class Rename
|
||||
Configuration configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration);
|
||||
_PropertyConfiguration = propertyConfiguration;
|
||||
_Configuration = configuration;
|
||||
logger?.LogInformation(propertyConfiguration.RootDirectory);
|
||||
logger?.LogInformation("{RootDirectory}", propertyConfiguration.RootDirectory);
|
||||
Property.Models.Configuration.Verify(propertyConfiguration, requireExist: false);
|
||||
Verify();
|
||||
List<string> linesB = IteratedRenameFilesInDirectories(logger);
|
||||
if (linesB.Count != 0)
|
||||
{
|
||||
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", linesB);
|
||||
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(propertyConfiguration.RootDirectory);
|
||||
}
|
||||
List<string> lines = RenameFilesInDirectories(logger);
|
||||
if (lines.Any())
|
||||
if (lines.Count != 0)
|
||||
{
|
||||
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines);
|
||||
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(propertyConfiguration.RootDirectory);
|
||||
@ -72,27 +82,46 @@ public class Rename
|
||||
throw new NullReferenceException(nameof(_PropertyConfiguration));
|
||||
}
|
||||
|
||||
private static Action<string> GetResultCollection(A_Metadata metadata, List<Result> results, Action tick)
|
||||
{
|
||||
tick.Invoke();
|
||||
return file => results.Add(new(file, metadata.GetMetadataCollection(file)));
|
||||
}
|
||||
|
||||
private List<string> IteratedRenameFilesInDirectories(ILogger? logger)
|
||||
{
|
||||
List<string> old = [];
|
||||
List<Result> results = [];
|
||||
string rootDirectoryFullPath = Path.GetFullPath(_PropertyConfiguration.RootDirectory);
|
||||
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = _AppSettings.MaxDegreeOfParallelism };
|
||||
IEnumerable<string> files = Directory.EnumerateFiles(rootDirectoryFullPath, "*", SearchOption.AllDirectories);
|
||||
A_Metadata metadata = new(_PropertyConfiguration, _Configuration.ForceMetadataLastWriteTimeToCreationTime, _Configuration.PropertiesChangedForMetadata);
|
||||
ProgressBar progressBar = new(short.MaxValue, "EnumerateFiles load", new ProgressBarOptions() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true });
|
||||
files.AsParallel().ForAll(GetResultCollection(metadata, results, () => progressBar.Tick()));
|
||||
return old;
|
||||
}
|
||||
|
||||
private List<string> RenameFilesInDirectories(ILogger? logger)
|
||||
{
|
||||
List<string> results = new();
|
||||
List<string> results = [];
|
||||
string message;
|
||||
bool nefPresentCheck;
|
||||
bool nefPresent = false;
|
||||
ProgressBar progressBar;
|
||||
List<Record> records = new();
|
||||
List<Record> records = [];
|
||||
const string fileSearchFilter = "*";
|
||||
const bool useCeilingAverage = false;
|
||||
const string directorySearchFilter = "*";
|
||||
List<string> distinctDirectories = new();
|
||||
List<string> distinctDirectories = [];
|
||||
B_Metadata metadata = new(_PropertyConfiguration);
|
||||
List<(FileHolder, string, string)> toDoCollection = new();
|
||||
List<(FileHolder, string)> verifiedToDoCollection = new();
|
||||
List<(FileHolder, string, string)> toDoCollection = [];
|
||||
List<(FileHolder, string)> verifiedToDoCollection = [];
|
||||
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
||||
List<string[]> filesCollection = IDirectory.GetFilesCollection(_PropertyConfiguration.RootDirectory, directorySearchFilter, fileSearchFilter, useCeilingAverage);
|
||||
int count = filesCollection.Select(l => l.Length).Sum();
|
||||
foreach (string[] files in filesCollection)
|
||||
{
|
||||
if (!files.Any())
|
||||
if (files.Length == 0)
|
||||
continue;
|
||||
// foreach (string files[i] in files)
|
||||
// {
|
||||
@ -124,7 +153,7 @@ public class Rename
|
||||
}
|
||||
progressBar.Dispose();
|
||||
}
|
||||
if (records.Any())
|
||||
if (records.Count != 0)
|
||||
{
|
||||
int intMinValueLength = int.MinValue.ToString().Length;
|
||||
foreach (Record record in records)
|
||||
@ -158,7 +187,7 @@ public class Rename
|
||||
File.WriteAllText($"{to}.paddedId", $"{to}{Environment.NewLine}{fileHolder.FullName}");
|
||||
}
|
||||
ConsoleKey? consoleKey = null;
|
||||
logger?.LogInformation($"Ready to Move {verifiedToDoCollection.Count} files[i](s)?");
|
||||
logger?.LogInformation("Ready to Move {verifiedToDoCollection.Count} files[i](s)?", verifiedToDoCollection.Count);
|
||||
for (int y = 0; y < int.MaxValue; y++)
|
||||
{
|
||||
logger?.LogInformation("Press \"Y\" key to move files[i](s), \"N\" key to log files[i](s) or close console to not move files");
|
||||
@ -182,12 +211,12 @@ public class Rename
|
||||
|
||||
private static List<(FileHolder, string, string)> GetRenameUndoToDoCollection(ProgressBar progressBar, string[] files)
|
||||
{
|
||||
List<(FileHolder, string, string)> results = new();
|
||||
List<(FileHolder, string, string)> results = [];
|
||||
string[] lines;
|
||||
string fileName;
|
||||
string? directory;
|
||||
FileHolder fileHolder;
|
||||
List<string> distinct = new();
|
||||
List<string> distinct = [];
|
||||
foreach (string file in files)
|
||||
{
|
||||
progressBar.Tick();
|
||||
@ -211,7 +240,7 @@ public class Rename
|
||||
|
||||
private List<Record> GetRecords(B_Metadata metadata, int offset, ProgressBar progressBar, string[] files)
|
||||
{
|
||||
List<Record> results = new();
|
||||
List<Record> results = [];
|
||||
int? id;
|
||||
string? message;
|
||||
string? directory;
|
||||
@ -259,7 +288,7 @@ public class Rename
|
||||
.ExecuteAsync();
|
||||
result.Task.Wait();
|
||||
ffmpegFiles = Directory.GetFiles(fileHolder.DirectoryName, $"{fileHolder.Name}-*.jpg", SearchOption.TopDirectoryOnly);
|
||||
if (!ffmpegFiles.Any())
|
||||
if (ffmpegFiles.Length == 0)
|
||||
continue;
|
||||
fileHolder = new(ffmpegFiles.First());
|
||||
if (!fileHolder.Name.EndsWith("-0001.jpg"))
|
||||
@ -280,14 +309,14 @@ public class Rename
|
||||
}
|
||||
if (message is not null)
|
||||
throw new Exception(message);
|
||||
results.Add(new(i + offset, isIgnoreExtension, isValidImageFormatExtension, new() { fileHolder }, ffmpegFiles is null, dateTimeOriginal, dateTimes, id));
|
||||
results.Add(new(i + offset, isIgnoreExtension, isValidImageFormatExtension, [fileHolder], ffmpegFiles is null, dateTimeOriginal, dateTimes, id));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<(FileHolder, string, string)> GetToDoCollection(ProgressBar progressBar, bool nefPresent, List<Record> records, int intMinValueLength)
|
||||
{
|
||||
List<(FileHolder, string, string)> results = new();
|
||||
List<(FileHolder, string, string)> results = [];
|
||||
int season;
|
||||
string paddedId;
|
||||
string checkFile;
|
||||
@ -303,7 +332,7 @@ public class Rename
|
||||
string checkFileExtension;
|
||||
DateTime? dateTimeFromName;
|
||||
const string jpeg = ".jpeg";
|
||||
List<string> distinct = new();
|
||||
List<string> distinct = [];
|
||||
string[] directoryNameSegments;
|
||||
DateTime? dateTimeOriginalByLogic;
|
||||
foreach (Record record in records)
|
||||
@ -316,7 +345,7 @@ public class Rename
|
||||
continue;
|
||||
if (string.IsNullOrEmpty(fileHolder.DirectoryName))
|
||||
continue;
|
||||
dateTimeFromName = !record.DateTimes.Any() ? null : record.DateTimes.First();
|
||||
dateTimeFromName = record.DateTimes.Length == 0 ? null : record.DateTimes.First();
|
||||
if (fileHolder.ExtensionLowered == jpeg)
|
||||
{
|
||||
if (!record.IsIgnoreExtension && record.IsValidImageFormatExtension)
|
||||
@ -431,7 +460,7 @@ public class Rename
|
||||
|
||||
private static List<string> Move(ProgressBar progressBar, List<(FileHolder, string)> verifiedToDoCollection)
|
||||
{
|
||||
List<string> results = new();
|
||||
List<string> results = [];
|
||||
foreach ((FileHolder fileHolder, string to) in verifiedToDoCollection)
|
||||
{
|
||||
progressBar.Tick();
|
||||
|
Reference in New Issue
Block a user