1048 lines
61 KiB
C#
1048 lines
61 KiB
C#
using Humanizer;
|
|
using ShellProgressBar;
|
|
using System.Text.Json;
|
|
using View_by_Distance.Map.Models.Stateless;
|
|
using View_by_Distance.Shared.Models;
|
|
using View_by_Distance.Shared.Models.Stateless.Methods;
|
|
using WindowsShortcutFactory;
|
|
|
|
namespace View_by_Distance.Map.Models;
|
|
|
|
public class MapLogic
|
|
{
|
|
|
|
protected readonly Dictionary<int, List<int>> _SkipCollection;
|
|
protected readonly List<PersonContainer> _NotMappedPersonContainers;
|
|
protected readonly Dictionary<long, PersonContainer> _PersonKeyToPersonContainer;
|
|
protected readonly Dictionary<long, (long LCL, long Minimum, long Maximum, long UCL)> _PersonKeyToRanges;
|
|
protected readonly Dictionary<int, Dictionary<int, PersonContainer[]>> _IdThenNormalizedPixelPercentageToPersonContainers;
|
|
|
|
public Dictionary<int, int[]> KeyValuePairs => throw new NotImplementedException();
|
|
public Dictionary<int, int[]> IndicesFromNew => throw new NotImplementedException();
|
|
|
|
private readonly long _Ticks;
|
|
private readonly Serilog.ILogger? _Log;
|
|
private readonly int _MaxDegreeOfParallelism;
|
|
private readonly Configuration? _Configuration;
|
|
private readonly string _EDistanceContentTicksDirectory;
|
|
private readonly Shared.Models.Methods.IMapLogicSupport? _MapLogicSupport;
|
|
private readonly Shared.Models.Properties.IPropertyConfiguration _PropertyConfiguration;
|
|
|
|
public MapLogic(int maxDegreeOfParallelism, Shared.Models.Properties.IPropertyConfiguration propertyConfiguration, Configuration? configuration, long ticks, PersonContainer[] personContainers, string a2PeopleSingletonDirectory, string eDistanceContentDirectory, Mapping[] mappingCollection, Shared.Models.Methods.IMapLogicSupport? mapLogicSupport)
|
|
{
|
|
_Ticks = ticks;
|
|
_Configuration = configuration;
|
|
_MapLogicSupport = mapLogicSupport;
|
|
_Log = Serilog.Log.ForContext<MapLogic>();
|
|
_PropertyConfiguration = propertyConfiguration;
|
|
_MaxDegreeOfParallelism = maxDegreeOfParallelism;
|
|
if (_Log is null)
|
|
{ }
|
|
if (propertyConfiguration.VerifyToSeason is null || !propertyConfiguration.VerifyToSeason.Any())
|
|
throw new Exception();
|
|
if (_MaxDegreeOfParallelism == 0)
|
|
{ }
|
|
string json;
|
|
string fullPath;
|
|
List<KeyValuePair<int, int[]>>? collection;
|
|
Dictionary<int, List<int>> skipCollection = new();
|
|
List<PersonContainer> notMappedPersonContainers = new();
|
|
Dictionary<long, PersonContainer> personKeyToPersonContainer = new();
|
|
string? rootDirectoryParent = Path.GetDirectoryName(propertyConfiguration.RootDirectory);
|
|
Dictionary<long, (long LCL, long Minimum, long Maximum, long UCL)> personKeyToRanges = new();
|
|
string eDistanceContentTicksDirectory = Path.Combine(eDistanceContentDirectory, $"({ticks})");
|
|
Dictionary<int, Dictionary<int, PersonContainer[]>> idThenNormalizedPixelPercentageToPersonContainers = new();
|
|
for (int i = 1; i < 5; i++)
|
|
_ = IPath.DeleteEmptyDirectories(eDistanceContentDirectory);
|
|
_ = IPath.DeleteEmptyDirectories(eDistanceContentDirectory);
|
|
if (string.IsNullOrEmpty(rootDirectoryParent))
|
|
throw new NullReferenceException(nameof(rootDirectoryParent));
|
|
if (!Directory.Exists(eDistanceContentDirectory))
|
|
_ = Directory.CreateDirectory(eDistanceContentDirectory);
|
|
if (!Directory.Exists(eDistanceContentTicksDirectory))
|
|
_ = Directory.CreateDirectory(eDistanceContentTicksDirectory);
|
|
if (configuration is not null && mapLogicSupport is not null)
|
|
{
|
|
List<PersonContainer> personContainerCollection = new(personContainers);
|
|
Stateless.MapLogic.Set(propertyConfiguration,
|
|
configuration,
|
|
ticks,
|
|
personContainerCollection,
|
|
a2PeopleSingletonDirectory,
|
|
eDistanceContentDirectory,
|
|
mappingCollection,
|
|
mapLogicSupport,
|
|
personKeyToPersonContainer,
|
|
personKeyToRanges,
|
|
notMappedPersonContainers,
|
|
skipCollection,
|
|
idThenNormalizedPixelPercentageToPersonContainers);
|
|
if (personContainerCollection.Count == personContainers.Length)
|
|
throw new NotSupportedException();
|
|
}
|
|
foreach (string propertyContentCollectionFile in propertyConfiguration.PropertyContentCollectionFiles)
|
|
{
|
|
fullPath = Path.GetFullPath(string.Concat(rootDirectoryParent, propertyContentCollectionFile));
|
|
if (fullPath.Contains(propertyConfiguration.RootDirectory))
|
|
continue;
|
|
if (!File.Exists(fullPath))
|
|
continue;
|
|
json = File.ReadAllText(fullPath);
|
|
collection = JsonSerializer.Deserialize<List<KeyValuePair<int, int[]>>>(json);
|
|
if (collection is null)
|
|
throw new NullReferenceException(nameof(collection));
|
|
}
|
|
_SkipCollection = skipCollection;
|
|
_PersonKeyToRanges = personKeyToRanges;
|
|
_NotMappedPersonContainers = notMappedPersonContainers;
|
|
_PersonKeyToPersonContainer = personKeyToPersonContainer;
|
|
_EDistanceContentTicksDirectory = eDistanceContentTicksDirectory;
|
|
_IdThenNormalizedPixelPercentageToPersonContainers = idThenNormalizedPixelPercentageToPersonContainers;
|
|
}
|
|
|
|
public MapLogic(int maxDegreeOfParallelism, Shared.Models.Properties.IPropertyConfiguration propertyConfiguration, Configuration? configuration, long ticks, PersonContainer[] personContainers, string a2PeopleSingletonDirectory, string eDistanceContentDirectory) :
|
|
this(maxDegreeOfParallelism, propertyConfiguration, configuration, ticks, personContainers, a2PeopleSingletonDirectory, eDistanceContentDirectory, Array.Empty<Mapping>(), null)
|
|
{ }
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
|
|
return result;
|
|
}
|
|
|
|
public (Dictionary<long, int>, int) AddToMapping(Mapping[] mappingCollection)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
int result = 0;
|
|
long personKey;
|
|
const int zero = 0;
|
|
string mappingSegmentB;
|
|
int by = IMapLogic.Mapping;
|
|
PersonBirthday personBirthday;
|
|
PersonContainer[]? collection;
|
|
List<PersonContainer> personContainers = new();
|
|
Dictionary<long, int> personKeyToCount = new();
|
|
Dictionary<int, PersonContainer[]>? normalizedPixelPercentageToPersonContainers;
|
|
foreach (Mapping mapping in mappingCollection)
|
|
{
|
|
personContainers.Clear();
|
|
if (!_IdThenNormalizedPixelPercentageToPersonContainers.TryGetValue(mapping.MappingFromItem.Id, out normalizedPixelPercentageToPersonContainers))
|
|
result += 1;
|
|
else
|
|
{
|
|
if (!normalizedPixelPercentageToPersonContainers.TryGetValue(mapping.MappingFromLocation.NormalizedPixelPercentage, out collection))
|
|
result += 1;
|
|
else
|
|
personContainers.AddRange(collection);
|
|
}
|
|
foreach (PersonContainer personContainer in personContainers)
|
|
{
|
|
if (personContainer.Key is null || personContainer.Birthdays is null || !personContainer.Birthdays.Any())
|
|
continue;
|
|
personBirthday = personContainer.Birthdays[zero];
|
|
personKey = personBirthday.Value.Ticks;
|
|
if (!personKeyToCount.ContainsKey(personKey))
|
|
personKeyToCount.Add(personKey, 0);
|
|
personKeyToCount[personKey] += 1;
|
|
mappingSegmentB = Stateless.MapLogic.GetMappingSegmentB(_Ticks, personBirthday, personContainer.ApproximateYears, mapping.MappingFromItem);
|
|
mapping.UpdateMappingFromPerson(personContainer.ApproximateYears, by, personContainer.DisplayDirectoryName, personBirthday, mappingSegmentB);
|
|
}
|
|
}
|
|
return new(personKeyToCount, result);
|
|
}
|
|
|
|
private void SaveContainers(int totalNotMapped, int? updated, List<SaveContainer> saveContainers)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
string checkFile;
|
|
string sourceFile;
|
|
WindowsShortcut windowsShortcut;
|
|
string[] directories = (from l in saveContainers select l.Directory).Distinct().ToArray();
|
|
int totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - _Ticks).TotalSeconds);
|
|
string message;
|
|
if (updated is null)
|
|
message = $") {saveContainers.Count:000} save(s) - {totalNotMapped} Total not Mapped - {totalSeconds} total second(s)";
|
|
else
|
|
message = $") {saveContainers.Count:000} save(s) - {totalNotMapped} Total not Mapped - {updated} Updated - {totalSeconds} total second(s)";
|
|
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
|
foreach (string directory in directories)
|
|
{
|
|
if (string.IsNullOrEmpty(directory))
|
|
continue;
|
|
if (!Directory.Exists(directory))
|
|
_ = Directory.CreateDirectory(directory);
|
|
}
|
|
using ProgressBar progressBar = new(saveContainers.Count, message, options);
|
|
foreach (SaveContainer saveContainer in saveContainers)
|
|
{
|
|
if (string.IsNullOrEmpty(saveContainer.Directory) || string.IsNullOrEmpty(saveContainer.CheckFile) || saveContainer.FaceFileHolder is null)
|
|
continue;
|
|
progressBar.Tick();
|
|
if (!saveContainer.FaceFileHolder.Exists && saveContainer.ResizedFileHolder is not null && saveContainer.ResizedFileHolder.Exists)
|
|
{
|
|
checkFile = saveContainer.CheckFile;
|
|
sourceFile = saveContainer.ResizedFileHolder.FullName;
|
|
}
|
|
else if (saveContainer.FaceFileHolder.Exists)
|
|
{
|
|
sourceFile = saveContainer.FaceFileHolder.FullName;
|
|
checkFile = $"{saveContainer.CheckFile}{_Configuration.FacesFileNameExtension}";
|
|
}
|
|
else
|
|
continue;
|
|
if (File.Exists(checkFile))
|
|
continue;
|
|
File.Copy(sourceFile, checkFile);
|
|
if (saveContainer.MakeAllHidden)
|
|
File.SetAttributes(checkFile, FileAttributes.Hidden);
|
|
if (saveContainer.HiddenFaceFileHolder is not null && saveContainer.HiddenFaceFileHolder.Exists)
|
|
{
|
|
sourceFile = saveContainer.HiddenFaceFileHolder.FullName;
|
|
checkFile = $"{saveContainer.CheckFile}{_Configuration.FacesHiddenFileNameExtension}";
|
|
}
|
|
else if (saveContainer.FacePartsFileHolder is not null && saveContainer.FacePartsFileHolder.Exists)
|
|
{
|
|
sourceFile = saveContainer.FacePartsFileHolder.FullName;
|
|
checkFile = $"{saveContainer.CheckFile}{_Configuration.FacePartsFileNameExtension}";
|
|
}
|
|
if (File.Exists(checkFile))
|
|
continue;
|
|
File.Copy(sourceFile, checkFile);
|
|
if (saveContainer.MakeAllHidden)
|
|
File.SetAttributes(checkFile, FileAttributes.Hidden);
|
|
}
|
|
if (updated is null)
|
|
{
|
|
foreach (SaveContainer saveContainer in saveContainers)
|
|
{
|
|
if (string.IsNullOrEmpty(saveContainer.Directory) || string.IsNullOrEmpty(saveContainer.CheckFile) || saveContainer.ResizedFileHolder is null || !saveContainer.ResizedFileHolder.Exists)
|
|
continue;
|
|
checkFile = saveContainer.CheckFile;
|
|
sourceFile = saveContainer.ResizedFileHolder.FullName;
|
|
if (File.Exists(checkFile))
|
|
continue;
|
|
File.Copy(sourceFile, checkFile);
|
|
if (saveContainer.MakeAllHidden)
|
|
File.SetAttributes(checkFile, FileAttributes.Hidden);
|
|
}
|
|
}
|
|
foreach (SaveContainer saveContainer in saveContainers)
|
|
{
|
|
if (string.IsNullOrEmpty(saveContainer.ShortcutFile) || saveContainer.ResizedFileHolder is null || !saveContainer.ResizedFileHolder.Exists)
|
|
continue;
|
|
try
|
|
{
|
|
windowsShortcut = new() { Path = saveContainer.ResizedFileHolder.FullName };
|
|
windowsShortcut.Save(saveContainer.ShortcutFile);
|
|
windowsShortcut.Dispose();
|
|
if (saveContainer.MakeAllHidden)
|
|
File.SetAttributes(saveContainer.ShortcutFile, FileAttributes.Hidden);
|
|
}
|
|
catch (Exception)
|
|
{ }
|
|
}
|
|
}
|
|
|
|
private SaveContainer? GetMatchSaveContainer(string dFacesContentDirectory, string d2FacePartsContentDirectory, string directory, Mapping mapping)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
SaveContainer? result;
|
|
string shortcutFile = string.Empty;
|
|
string? directoryName = Path.GetDirectoryName(mapping.MappingFromItem.RelativePath);
|
|
string facesDirectory = Path.Combine($"{dFacesContentDirectory}{directoryName}", mapping.MappingFromItem.ImageFileHolder.NameWithoutExtension);
|
|
FileHolder faceFileHolder = new(Path.Combine(facesDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacesFileNameExtension}"));
|
|
if (directoryName is null || !faceFileHolder.Exists)
|
|
result = null;
|
|
else
|
|
{
|
|
string facePartsDirectory = Path.Combine($"{d2FacePartsContentDirectory}{directoryName}", mapping.MappingFromItem.ImageFileHolder.NameWithoutExtension);
|
|
string checkFile = Path.Combine(directory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}");
|
|
FileHolder hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacesHiddenFileNameExtension}"));
|
|
FileHolder facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacePartsFileNameExtension}"));
|
|
result = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, facePartsFileHolder, mapping.MappingFromItem.ResizedFileHolder, shortcutFile);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static (string, bool, bool) Get(int? useFiltersCounter, string forceSingleImageHumanized, Mapping mapping)
|
|
{
|
|
string by;
|
|
bool isByMapping;
|
|
bool isBySorting;
|
|
if (mapping.By is null)
|
|
{
|
|
isByMapping = false;
|
|
isBySorting = false;
|
|
by = $"{nameof(IMapLogic.Mapping)}Null";
|
|
}
|
|
else
|
|
{
|
|
isByMapping = mapping.By == IMapLogic.Mapping;
|
|
isBySorting = mapping.By == IMapLogic.Sorting;
|
|
if (isBySorting && mapping.MappingFromPerson is null)
|
|
by = $"{nameof(IMapLogic.Sorting)} Without Person";
|
|
else if (isBySorting && useFiltersCounter.HasValue)
|
|
by = $"{nameof(IMapLogic.Sorting)} Modified Filters - {useFiltersCounter.Value}";
|
|
else
|
|
{
|
|
by = mapping.By.Value switch
|
|
{
|
|
IMapLogic.Mapping => nameof(IMapLogic.Mapping),
|
|
IMapLogic.Sorting => nameof(IMapLogic.Sorting),
|
|
IMapLogic.ForceSingleImage => forceSingleImageHumanized,
|
|
_ => throw new NotImplementedException()
|
|
};
|
|
}
|
|
}
|
|
return new(by, isByMapping, isBySorting);
|
|
}
|
|
|
|
private string GetDirectory(string by, MappingFromItem mappingFromItem, SortingContainer sortingContainer)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
string result;
|
|
const int zero = 0;
|
|
string mappingSegmentB;
|
|
string personKeyFormatted;
|
|
PersonBirthday personBirthday;
|
|
PersonContainer personContainer;
|
|
result = Path.Combine(_EDistanceContentTicksDirectory, by, sortingContainer.Sorting.Id.ToString(), sortingContainer.Sorting.NormalizedPixelPercentage.ToString());
|
|
for (int i = 0; i < _NotMappedPersonContainers.Count; i++)
|
|
{
|
|
personContainer = _NotMappedPersonContainers[i];
|
|
if (personContainer.Key is null || personContainer.Birthdays is null || !personContainer.Birthdays.Any())
|
|
continue;
|
|
personBirthday = personContainer.Birthdays[zero];
|
|
mappingSegmentB = Stateless.MapLogic.GetMappingSegmentB(_Ticks, personBirthday, personContainer.ApproximateYears, mappingFromItem);
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, personBirthday);
|
|
result = Path.Combine(_EDistanceContentTicksDirectory, by, personKeyFormatted, mappingSegmentB);
|
|
_NotMappedPersonContainers.RemoveAt(i);
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private List<SaveContainer> GetSaveContainers(string dFacesContentDirectory, string d2FacePartsContentDirectory, Mapping[] mappingCollection, Dictionary<int, Dictionary<int, Mapping>> idToNormalizedPixelPercentageToMapping, Dictionary<long, int> personKeyToCount, int? useFiltersCounter, bool saveMapped)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
List<SaveContainer> results = new();
|
|
string by;
|
|
long personKey;
|
|
bool isByMapping;
|
|
bool isBySorting;
|
|
string checkFile;
|
|
string directory;
|
|
string shortcutFile;
|
|
string facesDirectory;
|
|
string? directoryName;
|
|
string personDirectory;
|
|
FileHolder faceFileHolder;
|
|
string facePartsDirectory;
|
|
string personKeyFormatted;
|
|
SaveContainer? saveContainer;
|
|
FileHolder facePartsFileHolder;
|
|
FileHolder hiddenFaceFileHolder;
|
|
Dictionary<int, Mapping>? normalizedPixelPercentageToMapping;
|
|
string forceSingleImageHumanized = nameof(IMapLogic.ForceSingleImage).Humanize(LetterCasing.Title);
|
|
foreach (Mapping mapping in mappingCollection)
|
|
{
|
|
directoryName = Path.GetDirectoryName(mapping.MappingFromItem.RelativePath);
|
|
if (directoryName is null)
|
|
throw new NotSupportedException();
|
|
(by, isByMapping, isBySorting) = Get(useFiltersCounter, forceSingleImageHumanized, mapping);
|
|
if (isByMapping && !saveMapped)
|
|
continue;
|
|
if (mapping.MappingFromPerson is null)
|
|
{
|
|
if (mapping.SortingContainer is null)
|
|
continue;
|
|
directory = GetDirectory(by, mapping.MappingFromItem, mapping.SortingContainer);
|
|
personDirectory = Path.Combine(directory, _Configuration.MappingDefaultName);
|
|
}
|
|
else
|
|
{
|
|
if (string.IsNullOrEmpty(mapping.MappingFromPerson.SegmentB))
|
|
throw new NotSupportedException();
|
|
if (string.IsNullOrEmpty(mapping.MappingFromPerson.DisplayDirectoryName))
|
|
throw new NotSupportedException();
|
|
personKey = mapping.MappingFromPerson.PersonBirthday.Value.Ticks;
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, mapping.MappingFromPerson.PersonBirthday);
|
|
directory = Path.Combine(_EDistanceContentTicksDirectory, by, personKeyFormatted, mapping.MappingFromPerson.SegmentB);
|
|
if (isByMapping)
|
|
personDirectory = Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName);
|
|
else if (mapping.By is not null)
|
|
personDirectory = Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName, "lnk");
|
|
else
|
|
personDirectory = Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName[..1], "lnk");
|
|
if (isByMapping && personKeyToCount.ContainsKey(personKey))
|
|
{
|
|
saveContainer = new(Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName, $"{personKeyToCount[personKey]} Face(s)"));
|
|
results.Add(saveContainer);
|
|
}
|
|
}
|
|
saveContainer = new(personDirectory);
|
|
results.Add(saveContainer);
|
|
facesDirectory = Path.Combine($"{dFacesContentDirectory}{directoryName}", mapping.MappingFromItem.ImageFileHolder.NameWithoutExtension);
|
|
faceFileHolder = new(Path.Combine(facesDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacesFileNameExtension}"));
|
|
if (!faceFileHolder.Exists)
|
|
continue;
|
|
if (isByMapping)
|
|
{
|
|
checkFile = Path.Combine(personDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}");
|
|
saveContainer = new(checkFile, directory, faceFileHolder);
|
|
}
|
|
else
|
|
{
|
|
facePartsDirectory = Path.Combine($"{d2FacePartsContentDirectory}{directoryName}", mapping.MappingFromItem.ImageFileHolder.NameWithoutExtension);
|
|
checkFile = Path.Combine(directory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}");
|
|
shortcutFile = Path.Combine(personDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}.lnk");
|
|
hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacesHiddenFileNameExtension}"));
|
|
facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacePartsFileNameExtension}"));
|
|
saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, facePartsFileHolder, mapping.MappingFromItem.ResizedFileHolder, shortcutFile);
|
|
}
|
|
results.Add(saveContainer);
|
|
if (!isBySorting || mapping.SortingContainer is null)
|
|
continue;
|
|
if (!idToNormalizedPixelPercentageToMapping.TryGetValue(mapping.SortingContainer.Sorting.Id, out normalizedPixelPercentageToMapping))
|
|
continue;
|
|
if (!normalizedPixelPercentageToMapping.ContainsKey(mapping.SortingContainer.Sorting.NormalizedPixelPercentage))
|
|
continue;
|
|
if (isBySorting && mapping.MappingFromPerson is null)
|
|
{
|
|
saveContainer = GetMatchSaveContainer(dFacesContentDirectory, d2FacePartsContentDirectory, directory, normalizedPixelPercentageToMapping[mapping.SortingContainer.Sorting.NormalizedPixelPercentage]);
|
|
if (saveContainer is not null)
|
|
results.Add(saveContainer);
|
|
}
|
|
saveContainer = Stateless.MapLogic.GetDebugSaveContainer(directory, mapping.SortingContainer, normalizedPixelPercentageToMapping[mapping.SortingContainer.Sorting.NormalizedPixelPercentage]);
|
|
results.Add(saveContainer);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public void UpdateFromSortingContainersThenSaveContainers(string dFacesContentDirectory, string d2FacePartsContentDirectory, Mapping[] mappingCollection, Dictionary<int, Dictionary<int, Mapping>> idToNormalizedPixelPercentageToMapping, int? useFiltersCounter, SortingContainer[] sortingContainers, int totalNotMapped)
|
|
{
|
|
if (_MapLogicSupport is not null)
|
|
{
|
|
string counts = _MapLogicSupport.GetCounts();
|
|
_ = Directory.CreateDirectory(Path.Combine(_EDistanceContentTicksDirectory, counts));
|
|
}
|
|
Dictionary<long, int> personKeyToCount = new();
|
|
int updated = UpdateFromSortingContainers(sortingContainers);
|
|
List<SaveContainer> saveContainers = GetSaveContainers(dFacesContentDirectory, d2FacePartsContentDirectory, mappingCollection, idToNormalizedPixelPercentageToMapping, personKeyToCount, useFiltersCounter, saveMapped: false);
|
|
SaveContainers(totalNotMapped, updated, saveContainers);
|
|
}
|
|
|
|
public void SaveMapped(string dFacesContentDirectory, string d2FacePartsContentDirectory, Mapping[] mappingCollection, Dictionary<int, Dictionary<int, Mapping>> idToNormalizedPixelPercentageToMapping, Dictionary<long, int> personKeyToCount, int totalNotMapped)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
int? updated = null;
|
|
int? useFiltersCounter = null;
|
|
string mappingDirectory = Path.Combine(_EDistanceContentTicksDirectory, nameof(IMapLogic.Mapping));
|
|
List<SaveContainer> saveContainers = GetSaveContainers(dFacesContentDirectory, d2FacePartsContentDirectory, mappingCollection, idToNormalizedPixelPercentageToMapping, personKeyToCount, useFiltersCounter, saveMapped: true);
|
|
SaveContainers(totalNotMapped, updated, saveContainers);
|
|
if (!string.IsNullOrEmpty(_EDistanceContentTicksDirectory) && Directory.Exists(mappingDirectory))
|
|
Stateless.MapLogic.SaveMappingShortcuts(mappingDirectory);
|
|
}
|
|
|
|
private List<(long, long, long, long)> GetPersonKeysRangesCollection(PersonContainer[] personContainers)
|
|
{
|
|
List<(long, long, long, long)> results = new();
|
|
(long, long, long, long) singleton;
|
|
foreach (PersonContainer personContainer in personContainers)
|
|
{
|
|
if (personContainer.Key is null)
|
|
continue;
|
|
if (!_PersonKeyToRanges.TryGetValue(personContainer.Key.Value, out singleton))
|
|
continue;
|
|
results.Add(singleton);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public bool Used(FaceDistance faceDistanceEncoding)
|
|
{
|
|
bool result = false;
|
|
if (faceDistanceEncoding.NormalizedPixelPercentage is null)
|
|
throw new NotSupportedException();
|
|
List<int>? normalizedPixelPercentages;
|
|
Dictionary<int, PersonContainer[]>? normalizedPixelPercentageToPersonContainers;
|
|
if (_SkipCollection.TryGetValue(faceDistanceEncoding.Id, out normalizedPixelPercentages))
|
|
{
|
|
if (normalizedPixelPercentages.Contains(faceDistanceEncoding.NormalizedPixelPercentage.Value))
|
|
result = true;
|
|
}
|
|
if (!result && _IdThenNormalizedPixelPercentageToPersonContainers.TryGetValue(faceDistanceEncoding.Id, out normalizedPixelPercentageToPersonContainers))
|
|
{
|
|
if (normalizedPixelPercentageToPersonContainers.ContainsKey(faceDistanceEncoding.NormalizedPixelPercentage.Value))
|
|
result = true;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public List<Sorting> GetSortingCollection(int i, FaceDistance faceDistanceEncoding, List<FaceDistance> faceDistanceLengths)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
List<Sorting> results = new();
|
|
Sorting sorting;
|
|
FaceDistance faceDistanceLength;
|
|
List<int>? normalizedPixelPercentages;
|
|
Dictionary<int, PersonContainer[]>? normalizedPixelPercentageToPersonContainers;
|
|
List<(long lcl, long minimum, long maximum, long ucl)> personKeysRangesCollection;
|
|
for (int j = 0; j < faceDistanceLengths.Count; j++)
|
|
{
|
|
if (faceDistanceEncoding.NormalizedPixelPercentage is null)
|
|
throw new NotSupportedException();
|
|
if (j == i)
|
|
continue;
|
|
if (_SkipCollection.TryGetValue(faceDistanceEncoding.Id, out normalizedPixelPercentages))
|
|
{
|
|
if (normalizedPixelPercentages.Contains(faceDistanceEncoding.NormalizedPixelPercentage.Value))
|
|
continue;
|
|
}
|
|
if (_IdThenNormalizedPixelPercentageToPersonContainers.TryGetValue(faceDistanceEncoding.Id, out normalizedPixelPercentageToPersonContainers))
|
|
{
|
|
if (normalizedPixelPercentageToPersonContainers.ContainsKey(faceDistanceEncoding.NormalizedPixelPercentage.Value))
|
|
continue;
|
|
}
|
|
faceDistanceLength = faceDistanceLengths[j];
|
|
if (faceDistanceLength.NormalizedPixelPercentage is null || faceDistanceLength.Length is null)
|
|
throw new NotSupportedException();
|
|
if (faceDistanceLength.Length == 0)
|
|
continue;
|
|
if (!_IdThenNormalizedPixelPercentageToPersonContainers.TryGetValue(faceDistanceLength.Id, out normalizedPixelPercentageToPersonContainers))
|
|
personKeysRangesCollection = new();
|
|
else
|
|
{
|
|
if (!normalizedPixelPercentageToPersonContainers.ContainsKey(faceDistanceLength.NormalizedPixelPercentage.Value))
|
|
personKeysRangesCollection = new();
|
|
else
|
|
personKeysRangesCollection = GetPersonKeysRangesCollection(normalizedPixelPercentageToPersonContainers[faceDistanceLength.NormalizedPixelPercentage.Value]);
|
|
}
|
|
sorting = ISorting.Get(_Configuration.FaceDistancePermyriad, _Configuration.RangeDistanceTolerance, faceDistanceEncoding, faceDistanceLength, personKeysRangesCollection);
|
|
if (sorting.DistancePermyriad == 0)
|
|
continue;
|
|
if (sorting.Id == faceDistanceEncoding.Id)
|
|
{
|
|
if (sorting.NormalizedPixelPercentage == faceDistanceEncoding.NormalizedPixelPercentage.Value)
|
|
throw new NotSupportedException();
|
|
continue;
|
|
}
|
|
results.Add(sorting);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public int UpdateFromSortingContainers(SortingContainer[] sortingContainers)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
int result = 0;
|
|
string key;
|
|
const int zero = 0;
|
|
string mappingSegmentB;
|
|
string personKeyFormatted;
|
|
PersonBirthday personBirthday;
|
|
const int by = IMapLogic.Sorting;
|
|
PersonContainer[] personContainers;
|
|
List<int> normalizedPixelPercentageCollectionForA;
|
|
List<int> normalizedPixelPercentageCollectionForB;
|
|
Dictionary<string, int> checkKeyValuePairs = new();
|
|
Dictionary<int, List<int>> idToNormalizedPixelPercentageCollectionForA = new();
|
|
Dictionary<int, List<int>> idToNormalizedPixelPercentageCollectionForB = new();
|
|
Dictionary<int, PersonContainer[]>? normalizedPixelPercentageToPersonContainers;
|
|
int totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - _Ticks).TotalSeconds);
|
|
string message = $") {sortingContainers.Length:000} Update From Sorting Container(s) - {totalSeconds} total second(s)";
|
|
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
|
using ProgressBar progressBar = new(sortingContainers.Length, message, options);
|
|
foreach (SortingContainer sortingContainer in sortingContainers)
|
|
{
|
|
progressBar.Tick();
|
|
if (sortingContainer.Mapping is null)
|
|
throw new NotSupportedException();
|
|
if (!idToNormalizedPixelPercentageCollectionForA.ContainsKey(sortingContainer.Mapping.MappingFromItem.Id))
|
|
idToNormalizedPixelPercentageCollectionForA.Add(sortingContainer.Mapping.MappingFromItem.Id, new());
|
|
normalizedPixelPercentageCollectionForA = idToNormalizedPixelPercentageCollectionForA[sortingContainer.Mapping.MappingFromItem.Id];
|
|
if (!idToNormalizedPixelPercentageCollectionForB.ContainsKey(sortingContainer.Mapping.MappingFromItem.Id))
|
|
idToNormalizedPixelPercentageCollectionForB.Add(sortingContainer.Mapping.MappingFromItem.Id, new());
|
|
normalizedPixelPercentageCollectionForB = idToNormalizedPixelPercentageCollectionForB[sortingContainer.Mapping.MappingFromItem.Id];
|
|
if (!_IdThenNormalizedPixelPercentageToPersonContainers.TryGetValue(sortingContainer.Sorting.Id, out normalizedPixelPercentageToPersonContainers))
|
|
{
|
|
personContainers = Array.Empty<PersonContainer>();
|
|
if (normalizedPixelPercentageCollectionForA.Contains(sortingContainer.Mapping.MappingFromLocation.NormalizedPixelPercentage))
|
|
continue;
|
|
key = string.Concat(sortingContainer.Mapping.MappingFromItem.Id, '\t', sortingContainer.Mapping.MappingFromLocation.NormalizedPixelPercentage);
|
|
if (!checkKeyValuePairs.ContainsKey(key))
|
|
checkKeyValuePairs.Add(key, new());
|
|
checkKeyValuePairs[key]++;
|
|
if (checkKeyValuePairs[key] > _Configuration.SortingMaximumPerKey)
|
|
continue;
|
|
sortingContainer.Mapping.UpdateMappingFromUnknownPerson(by, sortingContainer);
|
|
normalizedPixelPercentageCollectionForA.Add(sortingContainer.Mapping.MappingFromLocation.NormalizedPixelPercentage);
|
|
result += 1;
|
|
}
|
|
else
|
|
{
|
|
if (normalizedPixelPercentageCollectionForB.Contains(sortingContainer.Mapping.MappingFromLocation.NormalizedPixelPercentage))
|
|
continue;
|
|
if (!normalizedPixelPercentageToPersonContainers.ContainsKey(sortingContainer.Sorting.NormalizedPixelPercentage))
|
|
personContainers = Array.Empty<PersonContainer>();
|
|
else
|
|
personContainers = normalizedPixelPercentageToPersonContainers[sortingContainer.Sorting.NormalizedPixelPercentage];
|
|
foreach (PersonContainer personContainer in personContainers)
|
|
{
|
|
if (personContainer.Key is null || personContainer.Birthdays is null || !personContainer.Birthdays.Any())
|
|
continue;
|
|
personBirthday = personContainer.Birthdays[zero];
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, personBirthday);
|
|
mappingSegmentB = Stateless.MapLogic.GetMappingSegmentB(_Ticks, personBirthday, personContainer.ApproximateYears, sortingContainer.Mapping.MappingFromItem);
|
|
key = string.Concat(personKeyFormatted, '\t', mappingSegmentB);
|
|
if (!checkKeyValuePairs.ContainsKey(key))
|
|
checkKeyValuePairs.Add(key, new());
|
|
checkKeyValuePairs[key]++;
|
|
if (checkKeyValuePairs[key] > _Configuration.SortingMaximumPerKey)
|
|
continue;
|
|
sortingContainer.Mapping.UpdateMappingFromPerson(personContainer.ApproximateYears, by, personContainer.DisplayDirectoryName, personBirthday, mappingSegmentB, sortingContainer);
|
|
normalizedPixelPercentageCollectionForB.Add(sortingContainer.Mapping.MappingFromLocation.NormalizedPixelPercentage);
|
|
result += 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public void CopyManualFiles(string dFacesContentDirectory, Dictionary<int, Dictionary<int, Mapping>> idToNormalizedPixelPercentageToMapping)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
int? id;
|
|
Mapping mapping;
|
|
string faceFile;
|
|
string checkFile;
|
|
string directory;
|
|
FileInfo fileInfo;
|
|
const int zero = 0;
|
|
string faceFileName;
|
|
string shortcutFile;
|
|
string? directoryName;
|
|
string facesDirectory;
|
|
string mappingSegmentB;
|
|
string personDirectory;
|
|
string personKeyFormatted;
|
|
string personDisplayFileName;
|
|
PersonBirthday personBirthday;
|
|
string? personDisplayDirectory;
|
|
int? normalizedPixelPercentage;
|
|
WindowsShortcut windowsShortcut;
|
|
string by = nameof(IMapLogic.ManualCopy);
|
|
Dictionary<int, Mapping>? normalizedPixelPercentageToMapping;
|
|
Dictionary<int, PersonContainer[]>? normalizedPixelPercentageToPeronContainerCollection;
|
|
string successfull = $"_ {nameof(IMapLogic.ManualCopy).Humanize(LetterCasing.Title)} Successfull";
|
|
foreach (KeyValuePair<long, PersonContainer> keyValuePair in _PersonKeyToPersonContainer)
|
|
{
|
|
if (keyValuePair.Value.Key is null || keyValuePair.Value.Birthdays is null || !keyValuePair.Value.Birthdays.Any())
|
|
continue;
|
|
personBirthday = keyValuePair.Value.Birthdays[zero];
|
|
foreach (string personDisplayDirectoryAllFile in keyValuePair.Value.DisplayDirectoryAllFiles)
|
|
{
|
|
if (!personDisplayDirectoryAllFile.EndsWith(_Configuration.FacesFileNameExtension))
|
|
continue;
|
|
(id, normalizedPixelPercentage, _) = IMapping.GetReversedDeterministicHashCodeKey(_Configuration.FacesFileNameExtension, personDisplayDirectoryAllFile);
|
|
if (id is null || normalizedPixelPercentage is null)
|
|
continue;
|
|
fileInfo = new(personDisplayDirectoryAllFile);
|
|
if (!fileInfo.Exists)
|
|
continue;
|
|
personDisplayFileName = Path.GetFileName(personDisplayDirectoryAllFile);
|
|
personDisplayDirectory = Path.GetDirectoryName(personDisplayDirectoryAllFile);
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, personBirthday);
|
|
mappingSegmentB = Stateless.MapLogic.GetMappingSegmentB(_Ticks, personBirthday, keyValuePair.Value.ApproximateYears, fileInfo.CreationTime, isWrongYear: null);
|
|
directory = Path.Combine(_EDistanceContentTicksDirectory, by, personKeyFormatted, mappingSegmentB);
|
|
personDirectory = Path.Combine(directory, keyValuePair.Value.DisplayDirectoryName, "lnk");
|
|
if (!idToNormalizedPixelPercentageToMapping.TryGetValue(id.Value, out normalizedPixelPercentageToMapping))
|
|
continue;
|
|
if (!normalizedPixelPercentageToMapping.ContainsKey(normalizedPixelPercentage.Value))
|
|
continue;
|
|
mapping = normalizedPixelPercentageToMapping[normalizedPixelPercentage.Value];
|
|
if (string.IsNullOrEmpty(personDisplayDirectory))
|
|
throw new NotSupportedException();
|
|
directoryName = Path.GetDirectoryName(mapping.MappingFromItem.RelativePath);
|
|
if (string.IsNullOrEmpty(directoryName))
|
|
throw new NotSupportedException();
|
|
shortcutFile = Path.Combine(personDisplayDirectory, $"{personDisplayFileName}.lnk");
|
|
facesDirectory = Path.Combine($"{dFacesContentDirectory}{directoryName}", mapping.MappingFromItem.ImageFileHolder.NameWithoutExtension);
|
|
faceFileName = $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacesFileNameExtension}";
|
|
checkFile = Path.Combine(directory, fileInfo.Name);
|
|
if (!_IdThenNormalizedPixelPercentageToPersonContainers.TryGetValue(id.Value, out normalizedPixelPercentageToPeronContainerCollection) || !normalizedPixelPercentageToPeronContainerCollection.ContainsKey(normalizedPixelPercentage.Value))
|
|
{
|
|
if (!Directory.Exists(personDirectory))
|
|
_ = Directory.CreateDirectory(personDirectory);
|
|
if (!File.Exists(checkFile))
|
|
File.Copy(personDisplayDirectoryAllFile, checkFile);
|
|
}
|
|
if (personDisplayDirectoryAllFile.Contains(successfull))
|
|
continue;
|
|
directoryName = Path.Combine(personDisplayDirectory, successfull);
|
|
if (!Directory.Exists(directoryName))
|
|
_ = Directory.CreateDirectory(directoryName);
|
|
checkFile = Path.Combine(directoryName, personDisplayFileName);
|
|
if (File.Exists(checkFile))
|
|
File.Delete(personDisplayDirectoryAllFile);
|
|
else
|
|
File.Move(personDisplayDirectoryAllFile, checkFile);
|
|
faceFile = Path.Combine(facesDirectory, faceFileName);
|
|
if (!File.Exists(faceFile))
|
|
continue;
|
|
if (File.Exists(shortcutFile))
|
|
continue;
|
|
windowsShortcut = new() { Path = faceFile };
|
|
windowsShortcut.Save(shortcutFile);
|
|
windowsShortcut.Dispose();
|
|
if (!File.Exists(shortcutFile))
|
|
continue;
|
|
File.SetLastWriteTime(shortcutFile, mapping.MappingFromItem.MinimumDateTime);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SaveNotMappedTicks()
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
int updated = 0;
|
|
string directory;
|
|
const int zero = 0;
|
|
int totalNotMapped = 0;
|
|
string personKeyFormatted;
|
|
SaveContainer saveContainer;
|
|
PersonBirthday personBirthday;
|
|
List<SaveContainer> saveContainers = new();
|
|
const string facePopulatedKey = nameof(IMapLogic.Sorting);
|
|
foreach (PersonContainer personContainer in _NotMappedPersonContainers)
|
|
{
|
|
if (personContainer.Key is null || personContainer.Birthdays is null || !personContainer.Birthdays.Any())
|
|
continue;
|
|
personBirthday = personContainer.Birthdays[zero];
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, personBirthday);
|
|
directory = Path.Combine(_EDistanceContentTicksDirectory, $"{facePopulatedKey}NotMapped", personKeyFormatted, _Configuration.MappingDefaultName);
|
|
saveContainer = new(directory);
|
|
saveContainers.Add(saveContainer);
|
|
}
|
|
SaveContainers(totalNotMapped, updated, saveContainers);
|
|
}
|
|
|
|
private (string, PersonBirthday?) GetPersonBirthday(string windowsShortcutPath)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
PersonBirthday? personBirthday = null;
|
|
string personKeyFormatted = string.Empty;
|
|
string[] directoryNames = IPath.GetDirectoryNames(windowsShortcutPath);
|
|
foreach (string directoryName in directoryNames)
|
|
{
|
|
personBirthday = IPersonBirthday.GetPersonBirthday(_Configuration.PersonBirthdayFormat, directoryName);
|
|
if (personBirthday is not null)
|
|
{
|
|
personKeyFormatted = directoryName;
|
|
break;
|
|
}
|
|
}
|
|
return new(personKeyFormatted, personBirthday);
|
|
}
|
|
|
|
private List<(string, PersonContainer)> GetPersonContainers(string[] jLinks, string a2PeopleSingletonDirectory, PersonContainer[] personContainers, Dictionary<long, int> personKeyToCount)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
List<(string, PersonContainer)> results = new();
|
|
string[] files;
|
|
const int zero = 0;
|
|
string[] directories;
|
|
string checkDirectory;
|
|
string[] checkDirectories;
|
|
string personKeyFormatted;
|
|
PersonContainer[] matches;
|
|
PersonBirthday? personBirthday;
|
|
string fileNameWithoutExtension;
|
|
WindowsShortcut windowsShortcut;
|
|
List<(long, string)> collection = new();
|
|
foreach (string directoryName in jLinks)
|
|
{
|
|
checkDirectory = Path.Combine(a2PeopleSingletonDirectory, directoryName);
|
|
if (!Directory.Exists(checkDirectory))
|
|
continue;
|
|
checkDirectories = Directory.GetDirectories(checkDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
files = Directory.GetFiles(checkDirectory, "*.lnk", SearchOption.TopDirectoryOnly);
|
|
foreach (string file in files)
|
|
{
|
|
collection.Clear();
|
|
windowsShortcut = WindowsShortcut.Load(file);
|
|
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
|
if (windowsShortcut.Path is null)
|
|
continue;
|
|
if (!Directory.Exists(windowsShortcut.Path) && files.Length != checkDirectories.Length)
|
|
throw new NotSupportedException(fileNameWithoutExtension);
|
|
(personKeyFormatted, personBirthday) = GetPersonBirthday(windowsShortcut.Path);
|
|
if (personBirthday is not null)
|
|
{
|
|
if (!personKeyToCount.ContainsKey(personBirthday.Value.Ticks))
|
|
collection.Add(new(personBirthday.Value.Ticks, Path.Combine(checkDirectory, personKeyFormatted, fileNameWithoutExtension)));
|
|
else
|
|
collection.Add(new(personBirthday.Value.Ticks, Path.Combine(checkDirectory, personKeyFormatted, fileNameWithoutExtension, $"{personKeyToCount[personBirthday.Value.Ticks]} Face(s)")));
|
|
}
|
|
else
|
|
{
|
|
directories = Directory.GetDirectories(windowsShortcut.Path, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string directory in directories)
|
|
{
|
|
personKeyFormatted = Path.GetFileName(directory);
|
|
personBirthday = IPersonBirthday.GetPersonBirthday(_Configuration.PersonBirthdayFormat, personKeyFormatted);
|
|
if (personBirthday is null)
|
|
continue;
|
|
if (!personKeyToCount.ContainsKey(personBirthday.Value.Ticks))
|
|
collection.Add(new(personBirthday.Value.Ticks, Path.Combine(checkDirectory, personKeyFormatted, fileNameWithoutExtension)));
|
|
else
|
|
collection.Add(new(personBirthday.Value.Ticks, Path.Combine(checkDirectory, personKeyFormatted, fileNameWithoutExtension, $"{personKeyToCount[personBirthday.Value.Ticks]} Face(s)")));
|
|
}
|
|
}
|
|
if (!collection.Any())
|
|
throw new NotSupportedException(fileNameWithoutExtension);
|
|
foreach ((long personKey, string displayDirectoryName) in collection)
|
|
{
|
|
matches = (from l in personContainers where l.Key == personKey && l.ApproximateYears.HasValue select l).ToArray();
|
|
if (!matches.Any())
|
|
throw new NotSupportedException(fileNameWithoutExtension);
|
|
if (!Directory.Exists(displayDirectoryName))
|
|
_ = Directory.CreateDirectory(displayDirectoryName);
|
|
results.Add(new(personKeyFormatted, matches[zero]));
|
|
}
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private string[] GetPersonKeyFormattedCollection(string[] jLinks, string a2PeopleSingletonDirectory, PersonContainer[] personContainers, Dictionary<long, int> personKeyToCount)
|
|
{
|
|
string[] results;
|
|
List<(string PersonKeyFormatted, PersonContainer PersonContainer)> collection = GetPersonContainers(jLinks, a2PeopleSingletonDirectory, personContainers, personKeyToCount);
|
|
results = (from l in collection orderby l.PersonKeyFormatted select l.PersonKeyFormatted).ToArray();
|
|
return results;
|
|
}
|
|
|
|
private (int, FileHolder, int, string, string, string, string)[] GetCollectionForSaveResizedImagesByPersonKeyFormatted(string[] jLinks, string a2PeopleSingletonDirectory, PersonContainer[] personContainers, Mapping[] mappingCollection, Dictionary<long, int> personKeyToCount)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
(int, FileHolder, int, string, string, string, string)[] results;
|
|
string checkFile;
|
|
string directory;
|
|
string? directoryName;
|
|
string personDirectory;
|
|
string personKeyFormatted;
|
|
string[] personKeyFormattedCollection = GetPersonKeyFormattedCollection(jLinks, a2PeopleSingletonDirectory, personContainers, personKeyToCount);
|
|
List<(int Id, FileHolder ResizedFileHolder, int ApproximateYears, string PersonKeyFormatted, string CheckFile, string Directory, string PersonDirectory)> collection = new();
|
|
foreach (Mapping mapping in mappingCollection)
|
|
{
|
|
directoryName = Path.GetDirectoryName(mapping.MappingFromItem.RelativePath);
|
|
if (directoryName is null)
|
|
throw new NotSupportedException();
|
|
if (mapping.By is null or IMapLogic.Sorting)
|
|
continue;
|
|
if (mapping.MappingFromPerson?.ApproximateYears is null)
|
|
continue;
|
|
if (string.IsNullOrEmpty(mapping.MappingFromPerson.SegmentB))
|
|
throw new NotSupportedException();
|
|
if (string.IsNullOrEmpty(mapping.MappingFromPerson.DisplayDirectoryName))
|
|
throw new NotSupportedException();
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, mapping.MappingFromPerson.PersonBirthday);
|
|
if (personKeyFormatted == "1501-04-10_00")
|
|
continue;
|
|
if (!personKeyFormattedCollection.Contains(personKeyFormatted))
|
|
continue;
|
|
directory = Path.Combine(_EDistanceContentTicksDirectory, "Images", personKeyFormatted);
|
|
personDirectory = Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName);
|
|
checkFile = Path.Combine(directory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}");
|
|
collection.Add(new(mapping.MappingFromItem.Id, mapping.MappingFromItem.ResizedFileHolder, mapping.MappingFromPerson.ApproximateYears.Value, personKeyFormatted, directory, personDirectory, checkFile));
|
|
}
|
|
results = (from l in collection orderby l.ApproximateYears descending, l.PersonKeyFormatted descending select l).ToArray();
|
|
return results;
|
|
}
|
|
|
|
public void SaveResizedImagesByPersonKeyFormatted(string[] jLinks, string a2PeopleSingletonDirectory, PersonContainer[] personContainers, Mapping[] mappingCollection, Dictionary<long, int> personKeyToCount, int totalNotMapped)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
SaveContainer? saveContainer;
|
|
List<int> distinctCollection = new();
|
|
List<SaveContainer> saveContainers = new();
|
|
(int, FileHolder, int, string, string, string, string)[] collection = GetCollectionForSaveResizedImagesByPersonKeyFormatted(jLinks, a2PeopleSingletonDirectory, personContainers, mappingCollection, personKeyToCount);
|
|
foreach ((int id, FileHolder resizedFileHolder, int approximateYears, string personKeyFormatted, string directory, string personDirectory, string checkFile) in collection)
|
|
{
|
|
if (distinctCollection.Contains(id))
|
|
continue;
|
|
distinctCollection.Add(id);
|
|
saveContainer = new(personDirectory);
|
|
saveContainers.Add(saveContainer);
|
|
saveContainer = new(resizedFileHolder, checkFile, directory);
|
|
saveContainers.Add(saveContainer);
|
|
}
|
|
SaveContainers(totalNotMapped, null, saveContainers);
|
|
}
|
|
|
|
private List<(string, string, string, string)> GetCollectionForSaveShortcuts(string[] jLinks, string a2PeopleSingletonDirectory, PersonContainer[] personContainers, List<Item> filteredItems, Mapping[] mappingCollection, Dictionary<long, int> personKeyToCount)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
long personKey;
|
|
string fileName;
|
|
string directory;
|
|
string? directoryName;
|
|
string personDirectory;
|
|
string personKeyFormatted;
|
|
List<(string, string, string, string)> collection = new();
|
|
string[] personKeyFormattedCollection = GetPersonKeyFormattedCollection(jLinks, a2PeopleSingletonDirectory, personContainers, personKeyToCount);
|
|
foreach (Item item in filteredItems)
|
|
{
|
|
directoryName = Path.GetDirectoryName(item.RelativePath);
|
|
if (directoryName is null)
|
|
throw new NotSupportedException();
|
|
if (item.Property?.Id is null || item.ResizedFileHolder?.DirectoryName is null || !item.ResizedFileHolder.Exists)
|
|
continue;
|
|
directory = Path.Combine(item.ResizedFileHolder.DirectoryName, $"{_PropertyConfiguration.ResultAllInOne}Shortcuts", _PropertyConfiguration.ResultAllInOne);
|
|
personDirectory = Path.Combine(directory, "No Faces");
|
|
fileName = Path.Combine(personDirectory, $"{item.ResizedFileHolder.Name}.lnk");
|
|
collection.Add(new(item.ResizedFileHolder.FullName, personDirectory, fileName, item.Property.Id.Value.ToString()));
|
|
}
|
|
foreach (Mapping mapping in mappingCollection)
|
|
{
|
|
directoryName = Path.GetDirectoryName(mapping.MappingFromItem.RelativePath);
|
|
if (directoryName is null)
|
|
throw new NotSupportedException();
|
|
if (mapping.MappingFromItem.ResizedFileHolder.DirectoryName is null || !mapping.MappingFromItem.ResizedFileHolder.Exists)
|
|
continue;
|
|
if (mapping.By is null or IMapLogic.Sorting || mapping.MappingFromPerson?.ApproximateYears is null)
|
|
{
|
|
directory = Path.Combine(mapping.MappingFromItem.ResizedFileHolder.DirectoryName, $"{_PropertyConfiguration.ResultAllInOne}Shortcuts", _PropertyConfiguration.ResultAllInOne);
|
|
personDirectory = Path.Combine(directory, "Unknown");
|
|
fileName = Path.Combine(personDirectory, $"{mapping.MappingFromItem.ResizedFileHolder.Name}.lnk");
|
|
collection.Add(new(mapping.MappingFromItem.ResizedFileHolder.FullName, personDirectory, fileName, mapping.MappingFromLocation.DeterministicHashCodeKey));
|
|
}
|
|
else
|
|
{
|
|
if (string.IsNullOrEmpty(mapping.MappingFromPerson.SegmentB))
|
|
throw new NotSupportedException();
|
|
if (string.IsNullOrEmpty(mapping.MappingFromPerson.DisplayDirectoryName))
|
|
throw new NotSupportedException();
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, mapping.MappingFromPerson.PersonBirthday);
|
|
if (!personKeyFormattedCollection.Contains(personKeyFormatted))
|
|
continue;
|
|
personKey = mapping.MappingFromPerson.PersonBirthday.Value.Ticks;
|
|
directory = Path.Combine(mapping.MappingFromItem.ResizedFileHolder.DirectoryName, $"{_PropertyConfiguration.ResultAllInOne}Shortcuts", personKeyFormatted);
|
|
if (!personKeyToCount.ContainsKey(personKey))
|
|
personDirectory = Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName);
|
|
else
|
|
personDirectory = Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName, $"{personKeyToCount[personKey]} Face(s)");
|
|
fileName = Path.Combine(directory, $"{mapping.MappingFromItem.ResizedFileHolder.Name}.lnk");
|
|
collection.Add(new(mapping.MappingFromItem.ResizedFileHolder.FullName, personDirectory, fileName, mapping.MappingFromLocation.DeterministicHashCodeKey));
|
|
}
|
|
}
|
|
return collection;
|
|
}
|
|
|
|
public void SaveShortcuts(string a2PeopleSingletonDirectory, PersonContainer[] personContainers, List<Item> filteredItems, Mapping[] mappingCollection, Dictionary<long, int> personKeyToCount)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
WindowsShortcut windowsShortcut;
|
|
string[] jLinks = new string[] { "All" };
|
|
List<(string, string Directory, string, string)> collection = GetCollectionForSaveShortcuts(jLinks, a2PeopleSingletonDirectory, personContainers, filteredItems, mappingCollection, personKeyToCount);
|
|
string[] directories = (from l in collection select l.Directory).Distinct().ToArray();
|
|
foreach (string directory in directories)
|
|
{
|
|
if (string.IsNullOrEmpty(directory))
|
|
continue;
|
|
if (!Directory.Exists(directory))
|
|
_ = Directory.CreateDirectory(directory);
|
|
}
|
|
foreach ((string fullName, string directory, string fileName, string description) in collection)
|
|
{
|
|
if (File.Exists(fileName))
|
|
continue;
|
|
try
|
|
{
|
|
windowsShortcut = new() { Path = fullName, Description = description };
|
|
windowsShortcut.Save(fileName);
|
|
windowsShortcut.Dispose();
|
|
}
|
|
catch (Exception)
|
|
{ }
|
|
}
|
|
}
|
|
|
|
private List<(string, FileHolder, string)> GetCollection(string dFacesContentDirectory, Dictionary<int, Dictionary<int, Mapping>> idToNormalizedPixelPercentageToMapping)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
List<(string, FileHolder, string)> results = new();
|
|
Mapping mapping;
|
|
string checkFile;
|
|
string directory;
|
|
string facesDirectory;
|
|
string? directoryName;
|
|
FileHolder faceFileHolder;
|
|
List<int>? normalizedPixelPercentages;
|
|
string by = nameof(IMapLogic.CopyNotMappedFaces);
|
|
Dictionary<int, PersonContainer[]>? normalizedPixelPercentageToPersonContainers;
|
|
foreach (KeyValuePair<int, Dictionary<int, Mapping>> keyValuePair in idToNormalizedPixelPercentageToMapping)
|
|
{
|
|
_ = _IdThenNormalizedPixelPercentageToPersonContainers.TryGetValue(keyValuePair.Key, out normalizedPixelPercentageToPersonContainers);
|
|
foreach (KeyValuePair<int, Mapping> normalizedPixelPercentageAndMapping in keyValuePair.Value)
|
|
{
|
|
mapping = normalizedPixelPercentageAndMapping.Value;
|
|
if (normalizedPixelPercentageToPersonContainers is not null && normalizedPixelPercentageToPersonContainers.ContainsKey(mapping.MappingFromLocation.NormalizedPixelPercentage))
|
|
continue;
|
|
_ = _SkipCollection.TryGetValue(keyValuePair.Key, out normalizedPixelPercentages);
|
|
if (normalizedPixelPercentages is not null && normalizedPixelPercentages.Contains(mapping.MappingFromLocation.NormalizedPixelPercentage))
|
|
continue;
|
|
directoryName = Path.GetDirectoryName(mapping.MappingFromItem.RelativePath);
|
|
facesDirectory = Path.Combine($"{dFacesContentDirectory}{directoryName}", mapping.MappingFromItem.ImageFileHolder.NameWithoutExtension);
|
|
faceFileHolder = new(Path.Combine(facesDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacesFileNameExtension}"));
|
|
if (directoryName is null || !faceFileHolder.Exists)
|
|
continue;
|
|
directory = Path.Combine(_EDistanceContentTicksDirectory, by, $"{mapping.MappingFromLocation.AreaPermille:0000}A{mapping.MappingFromItem.MinimumDateTime.ToString("yyyy")[..3]}#");
|
|
checkFile = Path.Combine(directory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacesFileNameExtension}");
|
|
results.Add(new(directory, faceFileHolder, checkFile));
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public void CopyNotMappedFaces(int[] rangeFaceAreaPermilleTolerance, string dFacesContentDirectory, Dictionary<int, Dictionary<int, Mapping>> idToNormalizedPixelPercentageToMapping)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
List<(string Directory, FileHolder FaceFileHolder, string CheckFile)> collection = GetCollection(dFacesContentDirectory, idToNormalizedPixelPercentageToMapping);
|
|
string[] directories = (from l in collection select l.Directory).Distinct().ToArray();
|
|
foreach (string directory in directories)
|
|
{
|
|
if (string.IsNullOrEmpty(directory))
|
|
continue;
|
|
if (!Directory.Exists(directory))
|
|
_ = Directory.CreateDirectory(directory);
|
|
}
|
|
foreach ((string directory, FileHolder faceFileHolder, string checkFile) in collection)
|
|
{
|
|
if (File.Exists(checkFile))
|
|
continue;
|
|
File.Copy(faceFileHolder.FullName, checkFile);
|
|
}
|
|
}
|
|
|
|
} |