OriginalFileName

This commit is contained in:
2023-11-11 19:33:20 -07:00
parent 5a511a38e1
commit bf8b1c010e
13 changed files with 300 additions and 261 deletions

View File

@ -1,4 +1,5 @@
using MetadataExtractor;
using System.Collections.ObjectModel;
using System.Text.Json;
using View_by_Distance.Metadata.Models.Stateless;
using View_by_Distance.Metadata.Models.Stateless.Methods;
@ -30,11 +31,17 @@ public class A_Metadata
_FileGroups = IPath.GetKeyValuePairs(aAConfiguration, bResultsFullGroupDirectory, [aAConfiguration.ResultSingleton]);
}
public ExifDirectory GetMetadataCollection(IMetadataConfiguration metadataConfiguration, FilePath filePath, DeterministicHashCode deterministicHashCode)
public FileInfo GetFileInfo(FilePath filePath)
{
FileInfo result;
(_, int directoryIndex) = IPath.GetDirectoryNameAndIndex(_AAConfiguration.ResultAllInOneSubdirectoryLength, filePath.Name);
result = new(Path.Combine(_FileGroups[_AAConfiguration.ResultSingleton][directoryIndex], $"{filePath.NameWithoutExtension}{filePath.ExtensionLowered}.json"));
return result;
}
public ExifDirectory GetMetadataCollection(IMetadataConfiguration metadataConfiguration, FilePath filePath, FileInfo fileInfo, DeterministicHashCode deterministicHashCode)
{
ExifDirectory? results;
(_, int directoryIndex) = IPath.GetDirectoryNameAndIndex(_AAConfiguration.ResultAllInOneSubdirectoryLength, filePath.Name);
FileInfo fileInfo = new(Path.Combine(_FileGroups[_AAConfiguration.ResultSingleton][directoryIndex], $"{filePath.NameWithoutExtension}{filePath.ExtensionLowered}.json"));
if (_ForceMetadataLastWriteTimeToCreationTime && !fileInfo.Exists && File.Exists(Path.ChangeExtension(fileInfo.FullName, ".delete")))
{
File.Move(Path.ChangeExtension(fileInfo.FullName, ".delete"), fileInfo.FullName);
@ -71,9 +78,8 @@ public class A_Metadata
try
{ size = Dimensions.GetDimensions(filePath.FullName); }
catch (Exception) { size = null; }
IReadOnlyList<MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(filePath.FullName);
results = Exif.Covert(filePath, deterministicHashCode, fileInfo, size, directories);
results = Exif.Covert(filePath, deterministicHashCode, size, directories);
string json = JsonSerializer.Serialize(results, ExifDirectorySourceGenerationContext.Default.ExifDirectory);
if (IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null) && _ForceMetadataLastWriteTimeToCreationTime)
{
@ -84,26 +90,31 @@ public class A_Metadata
return results;
}
public static Action<string> GetResultCollection(IRename rename, IMetadataConfiguration metadataConfiguration, A_Metadata metadata, List<ExifDirectory> exifDirectories, Action tick)
public static Action<string> SetExifDirectoryCollection(IRename rename, IMetadataConfiguration metadataConfiguration, A_Metadata metadata, List<(string, FileInfo, ExifDirectory)> exifDirectories, Action tick)
{
return file =>
{
tick.Invoke();
FileInfo fileInfo;
FilePath? ffmpegFilePath;
ExifDirectory exifDirectory;
ReadOnlyCollection<string> ffmpegFiles;
DeterministicHashCode deterministicHashCode;
FilePath filePath = IId.GetFilePath(metadataConfiguration, file);
if (filePath.ExtensionLowered is not ".paddedId" and not ".lsv")
{
if (filePath.Id is null || (!filePath.IsIdFormat && !filePath.IsPaddedIdFormat))
{
string[]? ffmpegFiles = rename.ConvertAndGetFfmpegFiles(filePath);
filePath = ffmpegFiles is null || ffmpegFiles.Length < 0 ? filePath : IId.GetFilePath(filePath, ffmpegFiles[0]);
DeterministicHashCode deterministicHashCode = filePath.Id is not null ? deterministicHashCode = new(null, filePath.Id, null) : deterministicHashCode = rename.GetDeterministicHashCode(filePath);
if (ffmpegFiles is not null)
{
foreach (string ffmpegFile in ffmpegFiles)
File.Delete(ffmpegFile);
}
(ffmpegFiles, ffmpegFilePath) = rename.ConvertAndGetFfmpegFiles(filePath);
if (ffmpegFilePath is not null)
filePath = ffmpegFilePath;
fileInfo = metadata.GetFileInfo(filePath);
deterministicHashCode = filePath.Id is not null ? deterministicHashCode = new(null, filePath.Id, null) : deterministicHashCode = rename.GetDeterministicHashCode(filePath);
exifDirectory = metadata.GetMetadataCollection(metadataConfiguration, filePath, fileInfo, deterministicHashCode);
lock (exifDirectories)
exifDirectories.Add(metadata.GetMetadataCollection(metadataConfiguration, filePath, deterministicHashCode));
exifDirectories.Add(new(file, fileInfo, exifDirectory));
foreach (string ffmpegFile in ffmpegFiles)
File.Delete(ffmpegFile);
}
}
};

View File

@ -81,15 +81,7 @@ public class Configuration
public static MetadataConfiguration Get(IConfigurationRoot configurationRoot)
{
MetadataConfiguration result;
#if Linux
string environmentName = "Linux";
#elif OSX
string environmentName = "OSX";
#elif Windows
string environmentName = "Windows";
#endif
string section = string.Concat(environmentName, ":", nameof(Configuration));
IConfigurationSection configurationSection = configurationRoot.GetSection(section);
IConfigurationSection configurationSection = configurationRoot.GetSection(nameof(Configuration));
#pragma warning disable IL3050, IL2026
Configuration? configuration = configurationSection.Get<Configuration>();
#pragma warning restore IL3050, IL2026

View File

@ -1,127 +0,0 @@
// using System.Text.Json;
// using System.Text.Json.Serialization;
// namespace View_by_Distance.Metadata.Models;
// public class ZMetadataConfiguration : Shared.Models.Properties.IAAConfiguration
// {
// protected string _RootDirectory;
// public string RootDirectory => _RootDirectory;
// public string DateGroup { init; get; }
// public string FileNameDirectorySeparator { init; get; }
// public bool ForcePropertyLastWriteTimeToCreationTime { init; get; }
// public string[] IgnoreExtensions { init; get; }
// public string[] IgnoreRulesKeyWords { init; get; }
// public int MaxImagesInDirectoryForTopLevelFirstPass { init; get; }
// public string? ModelName { init; get; }
// public int? NumberOfJitters { init; get; }
// public int? NumberOfTimesToUpsample { init; get; }
// public int Offset { init; get; }
// public string Pattern { init; get; }
// public string PersonBirthdayFormat { init; get; }
// public bool PopulatePropertyId { init; get; }
// public string? PredictorModelName { init; get; }
// public bool PropertiesChangedForProperty { init; get; }
// public string[] PropertyContentCollectionFiles { init; get; }
// public string ResultAllInOne { init; get; }
// public int ResultAllInOneSubdirectoryLength { init; get; }
// public string ResultCollection { init; get; }
// public string ResultContent { init; get; }
// public string ResultSingleton { init; get; }
// public string[] ValidImageFormatExtensions { init; get; }
// [JsonConstructor]
// public MetadataConfiguration(string dateGroup,
// string fileNameDirectorySeparator,
// bool forcePropertyLastWriteTimeToCreationTime,
// string[] ignoreExtensions,
// string[] ignoreRulesKeyWords,
// int maxImagesInDirectoryForTopLevelFirstPass,
// string? modelName,
// int? numberOfJitters,
// int? numberOfTimesToUpsample,
// int offset,
// string pattern,
// string personBirthdayFormat,
// bool populatePropertyId,
// string? predictorModelName,
// bool propertiesChangedForProperty,
// string[] propertyContentCollectionFiles,
// string resultAllInOne,
// int resultAllInOneSubdirectoryLength,
// string resultCollection,
// string resultContent,
// string resultSingleton,
// string rootDirectory,
// string[] validImageFormatExtensions)
// {
// DateGroup = dateGroup;
// FileNameDirectorySeparator = fileNameDirectorySeparator;
// ForcePropertyLastWriteTimeToCreationTime = forcePropertyLastWriteTimeToCreationTime;
// IgnoreExtensions = ignoreExtensions;
// IgnoreRulesKeyWords = ignoreRulesKeyWords;
// MaxImagesInDirectoryForTopLevelFirstPass = maxImagesInDirectoryForTopLevelFirstPass;
// ModelName = modelName;
// NumberOfJitters = numberOfJitters;
// NumberOfTimesToUpsample = numberOfTimesToUpsample;
// Offset = offset;
// Pattern = pattern;
// PersonBirthdayFormat = personBirthdayFormat;
// PredictorModelName = predictorModelName;
// PopulatePropertyId = populatePropertyId;
// PropertiesChangedForProperty = propertiesChangedForProperty;
// PropertyContentCollectionFiles = propertyContentCollectionFiles;
// ResultAllInOne = resultAllInOne;
// ResultAllInOneSubdirectoryLength = resultAllInOneSubdirectoryLength;
// ResultCollection = resultCollection;
// ResultContent = resultContent;
// ResultSingleton = resultSingleton;
// _RootDirectory = rootDirectory;
// ValidImageFormatExtensions = validImageFormatExtensions;
// }
// public override string ToString()
// {
// string result = JsonSerializer.Serialize(this, MetadataConfigurationSourceGenerationContext.Default.MetadataConfiguration);
// return result;
// }
// public void ChangeRootDirectory(string rootDirectory) =>
// _RootDirectory = Path.GetFullPath(rootDirectory);
// public static void Verify(MetadataConfiguration propertyConfiguration, bool requireExist)
// {
// if (propertyConfiguration is null)
// throw new NullReferenceException(nameof(propertyConfiguration));
// if (propertyConfiguration.IgnoreExtensions is null || propertyConfiguration.IgnoreExtensions.Length == 0)
// throw new NullReferenceException(nameof(propertyConfiguration.IgnoreExtensions));
// if (propertyConfiguration.IgnoreRulesKeyWords is null || propertyConfiguration.IgnoreRulesKeyWords.Length == 0)
// throw new NullReferenceException(nameof(propertyConfiguration.IgnoreRulesKeyWords));
// if (propertyConfiguration.PropertyContentCollectionFiles is null)
// throw new NullReferenceException(nameof(propertyConfiguration.PropertyContentCollectionFiles));
// if (propertyConfiguration.ValidImageFormatExtensions is null || propertyConfiguration.ValidImageFormatExtensions.Length == 0)
// throw new NullReferenceException(nameof(propertyConfiguration.ValidImageFormatExtensions));
// if (propertyConfiguration is null)
// throw new NullReferenceException(nameof(propertyConfiguration));
// if (string.IsNullOrEmpty(propertyConfiguration.DateGroup))
// throw new NullReferenceException(nameof(propertyConfiguration.DateGroup));
// if (string.IsNullOrEmpty(propertyConfiguration.FileNameDirectorySeparator))
// throw new NullReferenceException(nameof(propertyConfiguration.FileNameDirectorySeparator));
// if (string.IsNullOrEmpty(propertyConfiguration.Pattern))
// throw new NullReferenceException(nameof(propertyConfiguration.Pattern));
// if (string.IsNullOrEmpty(propertyConfiguration.RootDirectory) || (requireExist && !Directory.Exists(propertyConfiguration.RootDirectory)))
// throw new NullReferenceException(nameof(propertyConfiguration.RootDirectory));
// if (propertyConfiguration.RootDirectory != Path.GetFullPath(propertyConfiguration.RootDirectory))
// throw new Exception();
// }
// }
// [JsonSourceGenerationOptions(WriteIndented = true)]
// [JsonSerializable(typeof(MetadataConfiguration))]
// internal partial class MetadataConfigurationSourceGenerationContext : JsonSerializerContext
// {
// }

View File

@ -122,7 +122,7 @@ internal static class Dimensions
/// <param name="path">The path of the image to get the dimensions of.</param>
/// <returns>The dimensions of the specified image.</returns>
/// <exception cref="ArgumentException">The image was of an unrecognized format.</exception>
public static Size GetDimensions(BinaryReader binaryReader)
internal static Size GetDimensions(BinaryReader binaryReader)
{
int maxMagicBytesLength = _ImageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;
@ -146,11 +146,11 @@ internal static class Dimensions
/// <summary>
/// Gets the dimensions of an image.
/// </summary>
///internal </summary>
/// <param name="path">The path of the image to get the dimensions of.</param>
/// <returns>The dimensions of the specified image.</returns>
/// <exception cref="ArgumentException">The image was of an unrecognized format.</exception>
public static Size GetDimensions(string path)
internal static Size GetDimensions(string path)
{
using BinaryReader binaryReader = new(File.OpenRead(path));
try

View File

@ -322,7 +322,7 @@ internal abstract class Exif
return result;
}
internal static Shared.Models.ExifDirectory Covert(Shared.Models.FilePath filePath, Shared.Models.DeterministicHashCode deterministicHashCode, FileInfo fileInfo, System.Drawing.Size? size, IReadOnlyList<MetadataExtractor.Directory> directories)
internal static Shared.Models.ExifDirectory Covert(Shared.Models.FilePath filePath, Shared.Models.DeterministicHashCode deterministicHashCode, System.Drawing.Size? size, IReadOnlyList<MetadataExtractor.Directory> directories)
{
Shared.Models.ExifDirectory results;
Shared.Models.AviDirectory aviDirectory = GetAviDirectory(directories);
@ -338,14 +338,13 @@ internal abstract class Exif
Shared.Models.QuickTimeTrackHeaderDirectory quickTimeTrackHeaderDirectory = GetQuickTimeTrackHeaderDirectoryDirectory(directories);
results = new(aviDirectory,
exifDirectoryBase,
filePath.FullName,
fileMetadataDirectory,
gifHeaderDirectory,
gpsDirectory,
size?.Height,
deterministicHashCode.Id ?? filePath.Id,
fileInfo.FullName,
jpegDirectory,
filePath.Name,
photoshopDirectory,
pngDirectory,
quickTimeMovieHeaderDirectory,