150 lines
7.3 KiB
C#

using System.Diagnostics;
using System.Text.Json;
using View_by_Distance.Metadata.Models.Stateless;
using View_by_Distance.Property.Models;
using View_by_Distance.Property.Models.Stateless;
using View_by_Distance.Shared.Models.Stateless;
namespace View_by_Distance.Metadata.Models;
/// <summary>
// Dictionary<string, List<KeyValuePair<string, string>>>
/// </summary>
public class B_Metadata
{
private readonly Serilog.ILogger? _Log;
private readonly bool _PropertiesChangedForMetadata;
private readonly bool _ForceMetadataLastWriteTimeToCreationTime;
private readonly IReadOnlyDictionary<string, string[]> _JsonGroups;
private readonly JsonSerializerOptions _WriteIndentedJsonSerializerOptions;
public B_Metadata(Configuration configuration, bool forceMetadataLastWriteTimeToCreationTime, bool propertiesChangedForMetadata, string bResultsFullGroupDirectory)
{
_Log = Serilog.Log.ForContext<B_Metadata>();
_PropertiesChangedForMetadata = propertiesChangedForMetadata;
_ForceMetadataLastWriteTimeToCreationTime = forceMetadataLastWriteTimeToCreationTime;
_WriteIndentedJsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true };
_JsonGroups = Shared.Models.Stateless.Methods.IPath.GetKeyValuePairs(configuration.ResultAllInOne, bResultsFullGroupDirectory);
}
public override string ToString()
{
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
return result;
}
private Dictionary<string, List<KeyValuePair<string, string>>> GetMetadataCollection(string subFile)
{
Dictionary<string, List<KeyValuePair<string, string>>> results = new();
if (_Log is null)
throw new NullReferenceException(nameof(_Log));
try
{
object? @object;
string? tagDescription;
List<string> tagNames = new();
int type = (int)IExif.Tags.Orientation;
string key = nameof(IExif.Tags.Orientation);
IReadOnlyList<MetadataExtractor.Directory> directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(subFile);
foreach (MetadataExtractor.Directory directory in directories)
{
if (!results.ContainsKey(directory.Name))
results.Add(directory.Name, new());
foreach (MetadataExtractor.Tag tag in directory.Tags)
{
tagNames.Add(tag.Name);
if (string.IsNullOrEmpty(tag.Description))
continue;
results[directory.Name].Add(new KeyValuePair<string, string>(string.Concat(tag.Type, '\t', tag.Name), tag.Description));
}
if (tagNames.Contains(key) && !results.ContainsKey(key))
{
@object = directory.GetObject(type);
if (@object is null)
continue;
tagDescription = @object.ToString();
if (string.IsNullOrEmpty(tagDescription))
continue;
results.Add(key, new() { new(string.Concat(type, '\t', key), tagDescription) });
}
}
}
catch (Exception)
{
_Log.Info(string.Concat(new StackFrame().GetMethod()?.Name, " <", subFile, ">"));
}
return results;
}
public (int, List<KeyValuePair<string, string>>) GetMetadataCollection(List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, Shared.Models.MappingFromItem mappingFromItem)
{
List<KeyValuePair<string, string>> results = new();
string json = string.Empty;
string[] changesFrom = new string[] { nameof(A_Property) };
Dictionary<string, List<KeyValuePair<string, string>>>? dictionary;
char directory = Shared.Models.Stateless.Methods.IDirectory.GetDirectory(mappingFromItem.ImageFileHolder.Name);
int directoryIndex = Shared.Models.Stateless.Methods.IDirectory.GetDirectory(directory);
List<DateTime> dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList();
FileInfo fileInfo = new(Path.Combine(_JsonGroups["{}"][directoryIndex], $"{mappingFromItem.ImageFileHolder.NameWithoutExtension}{mappingFromItem.ImageFileHolder.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)
dictionary = new();
else if (!fileInfo.Exists)
dictionary = new();
else if (!fileInfo.FullName.EndsWith(".json") && !fileInfo.FullName.EndsWith(".old"))
throw new ArgumentException("must be a *.json file");
else if (dateTimes.Any() && dateTimes.Max() > fileInfo.LastWriteTime)
dictionary = new();
else
{
json = File.ReadAllText(fileInfo.FullName);
try
{
dictionary = JsonSerializer.Deserialize<Dictionary<string, List<KeyValuePair<string, string>>>>(json);
if (dictionary is null)
throw new Exception();
subFileTuples.Add(new Tuple<string, DateTime>(nameof(B_Metadata), fileInfo.LastWriteTime));
}
catch (Exception)
{
dictionary = new();
parseExceptions.Add(nameof(B_Metadata));
}
}
if (dictionary is null || !dictionary.Any())
{
dictionary = GetMetadataCollection(mappingFromItem.ImageFileHolder.FullName);
json = JsonSerializer.Serialize(dictionary, _WriteIndentedJsonSerializerOptions);
bool updateDateWhenMatches = dateTimes.Any() && fileInfo.Exists && dateTimes.Max() > fileInfo.LastWriteTime;
DateTime? dateTime = !updateDateWhenMatches ? null : dateTimes.Max();
if (Shared.Models.Stateless.Methods.IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches, compareBeforeWrite: true, updateToWhenMatches: dateTime))
{
if (!_ForceMetadataLastWriteTimeToCreationTime)
subFileTuples.Add(new Tuple<string, DateTime>(nameof(B_Metadata), DateTime.Now));
else
{
File.SetLastWriteTime(fileInfo.FullName, fileInfo.CreationTime);
fileInfo.Refresh();
subFileTuples.Add(new Tuple<string, DateTime>(nameof(B_Metadata), fileInfo.CreationTime));
}
}
}
foreach (KeyValuePair<string, List<KeyValuePair<string, string>>> keyValuePair in dictionary)
{
foreach (KeyValuePair<string, string> keyValue in keyValuePair.Value)
results.Add(new(string.Concat(keyValuePair.Key, '\t', keyValue.Key), keyValue.Value));
}
return new(dictionary.Count, results);
}
}