a
This commit is contained in:
@ -2,13 +2,11 @@ using MetadataExtractor;
|
||||
using MetadataExtractor.Formats.Avi;
|
||||
using MetadataExtractor.Formats.Exif;
|
||||
using MetadataExtractor.Formats.QuickTime;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text.Json;
|
||||
using View_by_Distance.Metadata.Models.Stateless;
|
||||
using View_by_Distance.Shared.Models;
|
||||
using View_by_Distance.Shared.Models.Methods;
|
||||
using View_by_Distance.Shared.Models.Properties;
|
||||
using View_by_Distance.Shared.Models.Stateless;
|
||||
|
||||
namespace View_by_Distance.Metadata.Models;
|
||||
|
||||
@ -51,54 +49,10 @@ public class B_Metadata : IMetadata<MetadataExtractor.Directory>
|
||||
return result;
|
||||
}
|
||||
|
||||
private Dictionary<string, List<KeyValuePair<string, string>>> GetMetadataCollection(string subFile)
|
||||
public ReadOnlyDictionary<string, MetadataExtractorDirectory> GetMetadataCollection(List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, string[] changesFrom, MappingFromItem mappingFromItem)
|
||||
{
|
||||
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 = ImageMetadataReader.ReadMetadata(subFile);
|
||||
foreach (MetadataExtractor.Directory directory in directories)
|
||||
{
|
||||
if (!results.ContainsKey(directory.Name))
|
||||
results.Add(directory.Name, new());
|
||||
foreach (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, string[] changesFrom, MappingFromItem mappingFromItem)
|
||||
{
|
||||
List<KeyValuePair<string, string>> results = new();
|
||||
Dictionary<string, MetadataExtractorDirectory>? results = null;
|
||||
string json = string.Empty;
|
||||
Dictionary<string, List<KeyValuePair<string, string>>>? dictionary;
|
||||
List<DateTime> dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList();
|
||||
(_, int directoryIndex) = Shared.Models.Stateless.Methods.IPath.GetDirectoryNameAndIndex(_PropertyConfiguration.ResultAllInOneSubdirectoryLength, mappingFromItem.ImageFileHolder.Name);
|
||||
FileInfo fileInfo = new(Path.Combine(_FileGroups[_PropertyConfiguration.ResultSingleton][directoryIndex], $"{mappingFromItem.ImageFileHolder.NameWithoutExtension}{mappingFromItem.ImageFileHolder.ExtensionLowered}.json"));
|
||||
@ -113,33 +67,34 @@ public class B_Metadata : IMetadata<MetadataExtractor.Directory>
|
||||
fileInfo.Refresh();
|
||||
}
|
||||
if (_PropertiesChangedForMetadata)
|
||||
dictionary = new();
|
||||
results = null;
|
||||
else if (!fileInfo.Exists)
|
||||
dictionary = new();
|
||||
results = null;
|
||||
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();
|
||||
results = null;
|
||||
else
|
||||
{
|
||||
json = File.ReadAllText(fileInfo.FullName);
|
||||
try
|
||||
{
|
||||
dictionary = JsonSerializer.Deserialize<Dictionary<string, List<KeyValuePair<string, string>>>>(json);
|
||||
if (dictionary is null)
|
||||
results = Stateless.Methods.Metadata.Deserialize(json);
|
||||
if (results is null)
|
||||
throw new Exception();
|
||||
subFileTuples.Add(new Tuple<string, DateTime>(nameof(B_Metadata), fileInfo.LastWriteTime));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
dictionary = new();
|
||||
results = null;
|
||||
parseExceptions.Add(nameof(B_Metadata));
|
||||
}
|
||||
}
|
||||
if (dictionary is null || !dictionary.Any())
|
||||
if (results is null || results.Count == 0)
|
||||
{
|
||||
dictionary = GetMetadataCollection(mappingFromItem.ImageFileHolder.FullName);
|
||||
json = JsonSerializer.Serialize(dictionary, _WriteIndentedJsonSerializerOptions);
|
||||
IReadOnlyList<MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(mappingFromItem.ImageFileHolder.FullName);
|
||||
results = Stateless.Methods.Metadata.Covert(directories);
|
||||
json = JsonSerializer.Serialize(results, DictionaryStringMetadataExtractorDirectorySourceGenerationContext.Default.DictionaryStringMetadataExtractorDirectory);
|
||||
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))
|
||||
@ -154,12 +109,7 @@ public class B_Metadata : IMetadata<MetadataExtractor.Directory>
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
return new(results);
|
||||
}
|
||||
|
||||
(DateTime?, DateTime?[]) IMetadata<MetadataExtractor.Directory>.GetDateTimes(FileHolder fileHolder, IReadOnlyList<MetadataExtractor.Directory> directories)
|
||||
|
Reference in New Issue
Block a user