Tested first run

This commit is contained in:
2022-08-29 19:30:36 -07:00
parent 659e0e39bf
commit 753eeaba2a
26 changed files with 711 additions and 458 deletions

View File

@ -2,7 +2,6 @@ using System.Diagnostics;
using System.Text.Json;
using View_by_Distance.Property.Models;
using View_by_Distance.Shared.Models;
using View_by_Distance.Shared.Models.Properties;
using WindowsShortcutFactory;
namespace View_by_Distance.Map.Models;
@ -11,6 +10,7 @@ public class MapLogic
{
protected readonly List<double> _SkipCollection;
protected readonly List<string> _NotMappedPersonKeys;
protected readonly List<(int, string[])> _AllCollection;
protected readonly Dictionary<int, int[]> _KeyValuePairs;
protected readonly Dictionary<int, int[]> _IndicesFromNew;
@ -25,23 +25,30 @@ public class MapLogic
public Dictionary<int, int[]> IndicesFromNew => _IndicesFromNew;
private readonly Serilog.ILogger? _Log;
private readonly string _OutputExtension;
private readonly Configuration _Configuration;
private readonly string _FacesFilenameExtension;
private readonly string _ResizeFilenameExtension;
private readonly string _FacePartsFilenameExtension;
private readonly string _FacesHiddenFilenameExtension;
public MapLogic(int maxDegreeOfParallelism, Configuration configuration, string outputExtension, Dictionary<string, Person> personKeyValuePairs)
public MapLogic(int maxDegreeOfParallelism, Configuration configuration, string resizeFilenameExtension, string facesFilenameExtension, string facesHiddenFilenameExtension, string facePartsFilenameExtension, Person[] people)
{
_AllCollection = new();
_Configuration = configuration;
_Log = Serilog.Log.ForContext<MapLogic>();
_FacesFilenameExtension = facesFilenameExtension;
_ResizeFilenameExtension = resizeFilenameExtension;
_FacePartsFilenameExtension = facePartsFilenameExtension;
_FacesHiddenFilenameExtension = facesHiddenFilenameExtension;
Dictionary<int, string[]>? deterministicHashCodeUnknownFaceKeyValuePairs;
if (configuration.VerifyToSeason is null || !configuration.VerifyToSeason.Any())
throw new Exception();
string json;
string[] files;
string fullPath;
_OutputExtension = outputExtension;
List<double> skipCollection = new();
Dictionary<int, int[]>? keyValuePairs;
List<string> notMappedPersonKeys = new();
List<KeyValuePair<int, int[]>>? collection;
string deterministicHashCodeContentDirectory;
Dictionary<int, int[]> indicesFromNew = new();
@ -52,6 +59,7 @@ public class MapLogic
Dictionary<string, (string, int?, string, PersonBirthday[])> peopleKeyValuePairs = new();
if (string.IsNullOrEmpty(rootDirectoryParent))
throw new NullReferenceException(nameof(rootDirectoryParent));
string deterministicHashCodeRootDirectory = Path.Combine(rootDirectoryParent, "DeterministicHashCode");
files = Directory.GetFiles(rootDirectoryParent, "DeterministicHashCode*.json", SearchOption.TopDirectoryOnly);
if (files.Length != 1)
deterministicHashCodeUnknownFaceKeyValuePairs = new();
@ -62,11 +70,9 @@ public class MapLogic
if (deterministicHashCodeUnknownFaceKeyValuePairs is null)
throw new NullReferenceException(nameof(deterministicHashCodeUnknownFaceKeyValuePairs));
}
string[] directories = Directory.GetDirectories(rootDirectoryParent, "DeterministicHashCode", SearchOption.TopDirectoryOnly);
if (!directories.Any())
deterministicHashCodeContentDirectory = string.Empty;
else
deterministicHashCodeContentDirectory = Stateless.ByDeterministicHashCode.SetByRef(_OutputExtension, personKeyValuePairs, skipCollection, peopleKeyValuePairs, deterministicHashCodeUnknownFaceKeyValuePairs, deterministicHashCodeKeyValuePairs, incorrectDeterministicHashCodeKeyValuePairs, directories[0]);
if (!Directory.Exists(deterministicHashCodeRootDirectory))
_ = Directory.CreateDirectory(deterministicHashCodeRootDirectory);
deterministicHashCodeContentDirectory = Stateless.ByDeterministicHashCode.SetByRef(_ResizeFilenameExtension, people, skipCollection, peopleKeyValuePairs, notMappedPersonKeys, deterministicHashCodeUnknownFaceKeyValuePairs, deterministicHashCodeKeyValuePairs, incorrectDeterministicHashCodeKeyValuePairs, deterministicHashCodeRootDirectory);
if (!deterministicHashCodeUnknownFaceKeyValuePairs.Any())
sixCharacterNamedFaceInfo = new();
else
@ -113,6 +119,7 @@ public class MapLogic
_KeyValuePairs = keyValuePairs;
_IndicesFromNew = indicesFromNew;
_SkipCollection = skipCollection;
_NotMappedPersonKeys = notMappedPersonKeys;
_PeopleKeyValuePairs = peopleKeyValuePairs;
_SixCharacterNamedFaceInfo = sixCharacterNamedFaceInfo;
_DeterministicHashCodeKeyValuePairs = deterministicHashCodeKeyValuePairs;
@ -121,6 +128,10 @@ public class MapLogic
_DeterministicHashCodeUnknownFaceKeyValuePairs = deterministicHashCodeUnknownFaceKeyValuePairs;
}
public MapLogic(int maxDegreeOfParallelism, Configuration configuration, string outputExtension, Person[] people) :
this(maxDegreeOfParallelism, configuration, outputExtension, outputExtension, outputExtension, outputExtension, people)
{ }
public bool Skip(double deterministicHashCodeKey) => _SkipCollection.Contains(deterministicHashCodeKey);
public void SaveShortcuts(string[] juliePhares, string dResultsFullGroupDirectory, long ticks, List<Item> items)
@ -297,21 +308,13 @@ public class MapLogic
personKeys.AddRange(_DeterministicHashCodeKeyValuePairs[deterministicHashCodeKey]);
for (int i = 0; i < personKeys.Count; i++)
{
if (_PeopleKeyValuePairs.ContainsKey(personKeys[i]))
{
person = _PeopleKeyValuePairs[personKeys[i]];
personBirthday = person.PersonBirthdays[0];
approximateYears = person.ApproximateYears;
displayDirectoryName = person.DisplayDirectoryName;
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
}
else
{
approximateYears = null;
personKey = personKeys[i];
displayDirectoryName = "_ _ _";
personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey);
}
if (!_PeopleKeyValuePairs.ContainsKey(personKeys[i]))
continue;
person = _PeopleKeyValuePairs[personKeys[i]];
personBirthday = person.PersonBirthdays[0];
approximateYears = person.ApproximateYears;
displayDirectoryName = person.DisplayDirectoryName;
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
if (personBirthday is null)
continue;
mapping = new(approximateYears, displayDirectoryName, face.Location.NormalizedPixelPercentage, personBirthday, personKey);
@ -325,21 +328,13 @@ public class MapLogic
personKeys.AddRange(_DeterministicHashCodeUnknownFaceKeyValuePairs[item.Property.Id.Value]);
for (int i = 0; i < personKeys.Count; i++)
{
if (_PeopleKeyValuePairs.ContainsKey(personKeys[i]))
{
person = _PeopleKeyValuePairs[personKeys[i]];
personBirthday = person.PersonBirthdays[0];
approximateYears = person.ApproximateYears;
displayDirectoryName = person.DisplayDirectoryName;
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
}
else
{
approximateYears = null;
personKey = personKeys[i];
displayDirectoryName = "_ _ _";
personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey);
}
if (!_PeopleKeyValuePairs.ContainsKey(personKeys[i]))
continue;
person = _PeopleKeyValuePairs[personKeys[i]];
personBirthday = person.PersonBirthdays[0];
approximateYears = person.ApproximateYears;
displayDirectoryName = person.DisplayDirectoryName;
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
if (personBirthday is null)
continue;
mapping = new(approximateYears, displayDirectoryName, personBirthday, personKey);
@ -349,6 +344,72 @@ public class MapLogic
}
}
private void SaveContainers(List<SaveContainer> saveContainers)
{
WindowsShortcut windowsShortcut;
string[] directories = (from l in saveContainers select l.Directory).Distinct().ToArray();
foreach (string directory in directories)
{
if (string.IsNullOrEmpty(directory))
continue;
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
}
foreach (SaveContainer saveContainer in saveContainers)
{
if (string.IsNullOrEmpty(saveContainer.Directory) || string.IsNullOrEmpty(saveContainer.CheckFile) || saveContainer.ResizedFileHolder is null || saveContainer.FaceFileHolder is null || !string.IsNullOrEmpty(saveContainer.Json))
continue;
if (File.Exists(saveContainer.CheckFile))
continue;
if (saveContainer.FaceFileHolder.Exists)
File.Copy(saveContainer.FaceFileHolder.FullName, saveContainer.CheckFile);
else
File.Copy(saveContainer.ResizedFileHolder.FullName, saveContainer.CheckFile);
if (saveContainer.HiddenFaceFileHolder is not null && saveContainer.HiddenFaceFileHolder.Exists)
File.Copy(saveContainer.HiddenFaceFileHolder.FullName, Path.ChangeExtension(saveContainer.CheckFile, _FacesHiddenFilenameExtension));
else if (saveContainer.FacePartsFileHolder is not null && saveContainer.FacePartsFileHolder.Exists)
File.Copy(saveContainer.FacePartsFileHolder.FullName, Path.ChangeExtension(saveContainer.CheckFile, _FacePartsFilenameExtension));
}
foreach (SaveContainer saveContainer in saveContainers)
{
if (string.IsNullOrEmpty(saveContainer.Directory) || string.IsNullOrEmpty(saveContainer.CheckFile) || saveContainer.ResizedFileHolder is not null || saveContainer.FaceFileHolder is not null || string.IsNullOrEmpty(saveContainer.Json))
continue;
if (File.Exists(saveContainer.CheckFile))
continue;
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(saveContainer.CheckFile, saveContainer.Json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
}
foreach (SaveContainer saveContainer in saveContainers)
{
if (string.IsNullOrEmpty(saveContainer.Directory) || string.IsNullOrEmpty(saveContainer.CheckFile) || saveContainer.ResizedFileHolder is null)
continue;
if (string.IsNullOrEmpty(saveContainer.ShortcutFile) || !saveContainer.ResizedFileHolder.Exists)
continue;
try
{
windowsShortcut = new() { Path = saveContainer.ResizedFileHolder.FullName };
windowsShortcut.Save(saveContainer.ShortcutFile);
windowsShortcut.Dispose();
}
catch (Exception)
{ }
}
}
public void SaveNotMappedPersonKeys(string zPropertyHolderContentDirectory)
{
string directory;
SaveContainer saveContainer;
List<SaveContainer> saveContainers = new();
const string facePopulatedKey = nameof(Closest);
foreach (string personKey in _NotMappedPersonKeys)
{
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}NotMapped", personKey, Property.Models.Stateless.IResult.AllInOne);
saveContainer = new(directory);
saveContainers.Add(saveContainer);
}
SaveContainers(saveContainers);
}
public List<(Item, (string, Face?, (string, string, string, string))[])> GetCollection(List<Item> items, string dFacesContentDirectory)
{
List<(Item, (string, Face?, (string, string, string, string))[])> results = new();
@ -508,110 +569,9 @@ public class MapLogic
}
}
private List<(IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json)> GetClosest(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
private List<SaveContainer> GetMappingSaveContainers(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
{
List<(IFileHolder?, string, FileInfo?, string, string, string)> results = new();
Closest? match;
string dateKey;
string checkFile;
string directory;
string shortcutFile;
FileInfo faceFileInfo;
string? directoryName;
string facesDirectory;
string personDirectory;
List<int> used = new();
FileInfo landmarkFileInfo;
string landmarksDirectory;
double deterministicHashCodeKey;
DateTime dateTime = DateTime.Now;
const string facePopulatedKey = nameof(Closest);
foreach (Container container in containers)
{
if (!container.Items.Any())
continue;
if (!container.SourceDirectory.StartsWith(argZero))
continue;
foreach (Item item in container.Items)
{
used.Clear();
if (item.ImageFileHolder is null || item.Property?.Id is null || item.ResizedFileHolder is null)
continue;
if (!item.Closest.Any())
continue;
directoryName = Path.GetDirectoryName(item.RelativePath);
if (directoryName is null)
throw new Exception();
foreach (Face face in item.Faces)
{
match = null;
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
continue;
foreach (Closest closest in item.Closest)
{
if (closest.NormalizedPixelPercentage != face.Location.NormalizedPixelPercentage.Value)
continue;
match = closest;
break;
}
if (match is null)
continue;
foreach (Mapping mapping in item.Mapping)
{
if (mapping.NormalizedPixelPercentage is null || mapping.NormalizedPixelPercentage.Value != face.Location.NormalizedPixelPercentage.Value)
continue;
throw new Exception();
}
dateKey = Stateless.MapLogic.GetDateKey(dateTime, match.Mapping, match.MinimumDateTime, match.IsWrongYear);
directory = Path.Combine(zPropertyHolderContentDirectory, facePopulatedKey, match.Mapping.PersonKey, dateKey);
personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk");
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
landmarksDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face);
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}");
faceFileInfo = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.png"));
landmarkFileInfo = new(Path.Combine(landmarksDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.gif"));
if (string.IsNullOrEmpty(personDirectory))
shortcutFile = string.Empty;
else
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
results.Add(new(item.ResizedFileHolder, directory, faceFileInfo, checkFile, shortcutFile, string.Empty));
personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk", match.Mapping.DisplayDirectoryName);
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
used.Add(face.Location.NormalizedPixelPercentage.Value);
}
foreach (Closest closest in item.Closest)
{
if (used.Contains(closest.NormalizedPixelPercentage))
continue;
dateKey = Stateless.MapLogic.GetDateKey(dateTime, closest.Mapping, closest.MinimumDateTime, closest.IsWrongYear);
directory = Path.Combine(zPropertyHolderContentDirectory, facePopulatedKey, closest.Mapping.PersonKey, dateKey);
personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName, "lnk");
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
landmarksDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, closest);
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}");
faceFileInfo = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.png"));
landmarkFileInfo = new(Path.Combine(landmarksDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.gif"));
if (string.IsNullOrEmpty(personDirectory))
shortcutFile = string.Empty;
else
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
results.Add(new(item.ResizedFileHolder, directory, faceFileInfo, checkFile, shortcutFile, string.Empty));
personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName[..1], "lnk", closest.Mapping.DisplayDirectoryName);
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
used.Add(closest.NormalizedPixelPercentage);
}
}
}
return results;
}
private List<(IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json)> GetMapping(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
{
List<(IFileHolder?, string, FileInfo?, string, string, string)> results = new();
List<SaveContainer> results = new();
string key;
string json;
string dateKey;
@ -620,14 +580,16 @@ public class MapLogic
string directory;
bool? isWrongYear;
string shortcutFile;
FileInfo faceFileInfo;
string? directoryName;
string facesDirectory;
string personDirectory;
List<int> used = new();
DateTime minimumDateTime;
FileInfo landmarkFileInfo;
string landmarksDirectory;
FileHolder faceFileHolder;
string facePartsDirectory;
SaveContainer saveContainer;
FileHolder facePartsFileHolder;
FileHolder hiddenFaceFileHolder;
double deterministicHashCodeKey;
DateTime dateTime = DateTime.Now;
const string facePopulatedKey = nameof(Mapping);
@ -677,26 +639,31 @@ public class MapLogic
else
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}OutOfControl", match.PersonKey, dateKey);
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk");
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
saveContainer = new(personDirectory);
results.Add(saveContainer);
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
landmarksDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face);
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}");
faceFileInfo = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.png"));
landmarkFileInfo = new(Path.Combine(landmarksDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.gif"));
faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}"));
hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}"));
facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}"));
if (string.IsNullOrEmpty(personDirectory))
shortcutFile = string.Empty;
else
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
results.Add(new(item.ResizedFileHolder, directory, faceFileInfo, checkFile, shortcutFile, string.Empty));
saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile);
results.Add(saveContainer);
if (!string.IsNullOrEmpty(checkFile) && Shared.Models.Stateless.IMapping.SaveFaceEncoding)
{
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.json");
json = JsonSerializer.Serialize(face.FaceEncoding);
results.Add(new(null, directory, null, checkFile, string.Empty, json));
saveContainer = new(checkFile, directory, json);
results.Add(saveContainer);
}
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk", match.DisplayDirectoryName);
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
saveContainer = new(personDirectory);
results.Add(saveContainer);
used.Add(face.Location.NormalizedPixelPercentage.Value);
}
if (deterministicHashCodeKeyValuePairsAny && Shared.Models.Stateless.IMapping.UseDeterministicHashCodeUnknownFaceKeyValuePairsForSaveMapping)
@ -735,26 +702,31 @@ public class MapLogic
else
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}WithButOutOfControl", match.PersonKey, dateKey);
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk");
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
saveContainer = new(personDirectory);
results.Add(saveContainer);
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
landmarksDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face);
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}");
faceFileInfo = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.png"));
landmarkFileInfo = new(Path.Combine(landmarksDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.gif"));
faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}"));
facePartsFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}"));
facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}"));
if (string.IsNullOrEmpty(personDirectory))
shortcutFile = string.Empty;
else
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
results.Add(new(item.ResizedFileHolder, directory, faceFileInfo, checkFile, shortcutFile, string.Empty));
saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile);
results.Add(saveContainer);
if (!string.IsNullOrEmpty(checkFile) && Shared.Models.Stateless.IMapping.SaveFaceEncoding)
{
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.json");
json = JsonSerializer.Serialize(face.FaceEncoding);
results.Add(new(null, directory, null, checkFile, string.Empty, json));
saveContainer = new(checkFile, directory, json);
results.Add(saveContainer);
}
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk", match.DisplayDirectoryName);
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
saveContainer = new(personDirectory);
results.Add(saveContainer);
used.Add(face.Location.NormalizedPixelPercentage.Value);
}
}
@ -763,63 +735,127 @@ public class MapLogic
return results;
}
private static void Save(List<(IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json)> collection)
private List<SaveContainer> GetClosestSaveContainers(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
{
WindowsShortcut windowsShortcut;
string[] directories = (from l in collection select l.directory).Distinct().ToArray();
foreach (string directory in directories)
List<SaveContainer> results = new();
Closest? match;
string dateKey;
string checkFile;
string directory;
string shortcutFile;
string? directoryName;
string facesDirectory;
string personDirectory;
List<int> used = new();
FileHolder faceFileHolder;
string facePartsDirectory;
SaveContainer saveContainer;
FileHolder facePartsFileHolder;
FileHolder hiddenFaceFileHolder;
double deterministicHashCodeKey;
DateTime dateTime = DateTime.Now;
const string facePopulatedKey = nameof(Closest);
foreach (Container container in containers)
{
if (string.IsNullOrEmpty(directory))
if (!container.Items.Any())
continue;
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
}
foreach ((IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json) in collection)
{
if (string.IsNullOrEmpty(directory) || string.IsNullOrEmpty(checkFile) || resizedFileHolder is null || faceFileInfo is null || !string.IsNullOrEmpty(json))
if (!container.SourceDirectory.StartsWith(argZero))
continue;
if (File.Exists(checkFile))
continue;
if (faceFileInfo.Directory is not null && faceFileInfo.Directory.Exists && faceFileInfo.Exists)
File.Copy(faceFileInfo.FullName, checkFile);
else
File.Copy(resizedFileHolder.FullName, checkFile);
}
foreach ((IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json) in collection)
{
if (string.IsNullOrEmpty(directory) || string.IsNullOrEmpty(checkFile) || resizedFileHolder is not null || faceFileInfo is not null || string.IsNullOrEmpty(json))
continue;
if (File.Exists(checkFile))
continue;
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(checkFile, json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
}
foreach ((IFileHolder? resizedFileHolder, string directory, FileInfo? _, string checkFile, string shortcutFile, string json) in collection)
{
if (string.IsNullOrEmpty(directory) || string.IsNullOrEmpty(checkFile) || resizedFileHolder is null)
continue;
if (string.IsNullOrEmpty(shortcutFile) || !resizedFileHolder.Exists)
continue;
try
foreach (Item item in container.Items)
{
windowsShortcut = new() { Path = resizedFileHolder.FullName };
windowsShortcut.Save(shortcutFile);
windowsShortcut.Dispose();
used.Clear();
if (item.ImageFileHolder is null || item.Property?.Id is null || item.ResizedFileHolder is null)
continue;
if (!item.Closest.Any())
continue;
directoryName = Path.GetDirectoryName(item.RelativePath);
if (directoryName is null)
throw new Exception();
foreach (Face face in item.Faces)
{
match = null;
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
continue;
foreach (Closest closest in item.Closest)
{
if (closest.NormalizedPixelPercentage != face.Location.NormalizedPixelPercentage.Value)
continue;
match = closest;
break;
}
if (match is null)
continue;
foreach (Mapping mapping in item.Mapping)
{
if (mapping.NormalizedPixelPercentage is null || mapping.NormalizedPixelPercentage.Value != face.Location.NormalizedPixelPercentage.Value)
continue;
throw new Exception();
}
dateKey = Stateless.MapLogic.GetDateKey(dateTime, match.Mapping, match.MinimumDateTime, match.IsWrongYear);
directory = Path.Combine(zPropertyHolderContentDirectory, facePopulatedKey, match.Mapping.PersonKey, dateKey);
personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk");
saveContainer = new(personDirectory);
results.Add(saveContainer);
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face);
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}");
faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}"));
hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}"));
facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}"));
if (string.IsNullOrEmpty(personDirectory))
shortcutFile = string.Empty;
else
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile);
results.Add(saveContainer);
personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk", match.Mapping.DisplayDirectoryName);
saveContainer = new(personDirectory);
results.Add(saveContainer);
used.Add(face.Location.NormalizedPixelPercentage.Value);
}
foreach (Closest closest in item.Closest)
{
if (used.Contains(closest.NormalizedPixelPercentage))
continue;
dateKey = Stateless.MapLogic.GetDateKey(dateTime, closest.Mapping, closest.MinimumDateTime, closest.IsWrongYear);
directory = Path.Combine(zPropertyHolderContentDirectory, facePopulatedKey, closest.Mapping.PersonKey, dateKey);
personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName, "lnk");
saveContainer = new(personDirectory);
results.Add(saveContainer);
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, closest);
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}");
faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}"));
hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}"));
facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}"));
if (string.IsNullOrEmpty(personDirectory))
shortcutFile = string.Empty;
else
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile);
results.Add(saveContainer);
personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName[..1], "lnk", closest.Mapping.DisplayDirectoryName);
saveContainer = new(personDirectory);
results.Add(saveContainer);
used.Add(closest.NormalizedPixelPercentage);
}
}
catch (Exception)
{ }
}
}
public void SaveClosest(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
{
List<(IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json)> collection = GetClosest(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory, zPropertyHolderContentDirectory);
Save(collection);
return results;
}
public void SaveMapping(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
{
List<(IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json)> collection = GetMapping(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory, zPropertyHolderContentDirectory);
Save(collection);
List<SaveContainer> saveContainers = GetMappingSaveContainers(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory, zPropertyHolderContentDirectory);
SaveContainers(saveContainers);
}
public void SaveClosest(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
{
List<SaveContainer> saveContainers = GetClosestSaveContainers(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory, zPropertyHolderContentDirectory);
SaveContainers(saveContainers);
}
}