using MetadataExtractor; using System.Text.Json; using View_by_Distance.Metadata.Models.Stateless; using View_by_Distance.Metadata.Models.Stateless.Methods; 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.Metadata.Models; public class A_Metadata { private readonly IMetadataConfiguration _AAConfiguration; private readonly bool _PropertiesChangedForMetadata; private readonly bool _ForceMetadataLastWriteTimeToCreationTime; private readonly IReadOnlyDictionary _FileGroups; public A_Metadata(IMetadataConfiguration aAConfiguration, bool forceMetadataLastWriteTimeToCreationTime, bool propertiesChangedForMetadata) { _AAConfiguration = aAConfiguration; _PropertiesChangedForMetadata = propertiesChangedForMetadata; _ForceMetadataLastWriteTimeToCreationTime = forceMetadataLastWriteTimeToCreationTime; string bResultsFullGroupDirectory = IResult.GetResultsFullGroupDirectory(_AAConfiguration, nameof(A_Metadata), string.Empty, includeResizeGroup: false, includeModel: false, includePredictorModel: false); _FileGroups = IPath.GetKeyValuePairs(aAConfiguration, bResultsFullGroupDirectory, [aAConfiguration.ResultSingleton]); } public ExifDirectory GetMetadataCollection(IMetadataConfiguration metadataConfiguration, FilePath filePath, 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); fileInfo.Refresh(); } if (_ForceMetadataLastWriteTimeToCreationTime && fileInfo.Exists && fileInfo.LastWriteTime != fileInfo.CreationTime) { File.SetLastWriteTime(fileInfo.FullName, fileInfo.CreationTime); fileInfo.Refresh(); } if (_PropertiesChangedForMetadata) results = null; else if (!fileInfo.Exists) results = null; else if (!fileInfo.FullName.EndsWith(".json") && !fileInfo.FullName.EndsWith(".old")) throw new ArgumentException("must be a *.json file"); else { string json = File.ReadAllText(fileInfo.FullName); try { results = JsonSerializer.Deserialize(json, ExifDirectorySourceGenerationContext.Default.ExifDirectory); if (results is null) throw new Exception(); } catch (Exception) { results = null; } } if (results is null) { System.Drawing.Size? size; try { size = Dimensions.GetDimensions(filePath.FullName); } catch (Exception) { size = null; } IReadOnlyList directories = ImageMetadataReader.ReadMetadata(filePath.FullName); results = Exif.Covert(filePath, deterministicHashCode, fileInfo, size, directories); string json = JsonSerializer.Serialize(results, ExifDirectorySourceGenerationContext.Default.ExifDirectory); if (IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null) && _ForceMetadataLastWriteTimeToCreationTime) { File.SetLastWriteTime(fileInfo.FullName, fileInfo.CreationTime); fileInfo.Refresh(); } } return results; } public static Action GetResultCollection(IRename rename, IMetadataConfiguration metadataConfiguration, A_Metadata metadata, List exifDirectories, Action tick) { return file => { tick.Invoke(); 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); } lock (exifDirectories) exifDirectories.Add(metadata.GetMetadataCollection(metadataConfiguration, filePath, deterministicHashCode)); } } }; } }