1279 lines
73 KiB
C#
1279 lines
73 KiB
C#
using Humanizer;
|
|
using ShellProgressBar;
|
|
using System.Collections.ObjectModel;
|
|
using System.Text.Json;
|
|
using System.Text.RegularExpressions;
|
|
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 : Shared.Models.Methods.IMapLogic
|
|
{
|
|
|
|
protected readonly List<PersonContainer> _NotMappedPersonContainers;
|
|
protected readonly ReadOnlyDictionary<int, List<int>> _SkipCollection;
|
|
protected readonly ReadOnlyDictionary<int, List<int>> _SkipNotSkipCollection;
|
|
protected readonly ReadOnlyDictionary<long, PersonContainer> _PersonKeyToPersonContainer;
|
|
protected readonly ReadOnlyDictionary<int, Dictionary<int, PersonContainer[]>> _IdThenNormalizedRectangleToPersonContainers;
|
|
|
|
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.Properties.IPropertyConfiguration _PropertyConfiguration;
|
|
|
|
public MapLogic(int maxDegreeOfParallelism, Shared.Models.Properties.IPropertyConfiguration propertyConfiguration, bool saveIndividually, Configuration? configuration, PersonContainer[] personContainers, long ticks, string a2PeopleSingletonDirectory, string eDistanceContentDirectory)
|
|
{
|
|
_Ticks = ticks;
|
|
_Configuration = configuration;
|
|
_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<int, List<int>> skipNotSkipCollection = new();
|
|
Dictionary<long, PersonContainer> personKeyToPersonContainer = new();
|
|
string? rootDirectoryParent = Path.GetDirectoryName(propertyConfiguration.RootDirectory);
|
|
string eDistanceContentTicksDirectory = Path.Combine(eDistanceContentDirectory, $"({ticks})");
|
|
Dictionary<int, Dictionary<int, PersonContainer[]>> idThenNormalizedRectangleToPersonContainers = new();
|
|
if (!saveIndividually)
|
|
{
|
|
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 (configuration is not null)
|
|
{
|
|
List<PersonContainer> personContainerCollection = new(personContainers);
|
|
Stateless.MapLogic.Set(configuration,
|
|
ticks,
|
|
personContainerCollection,
|
|
a2PeopleSingletonDirectory,
|
|
eDistanceContentDirectory,
|
|
personKeyToPersonContainer,
|
|
notMappedPersonContainers,
|
|
skipCollection,
|
|
skipNotSkipCollection,
|
|
idThenNormalizedRectangleToPersonContainers);
|
|
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 = new(skipCollection);
|
|
_SkipNotSkipCollection = new(skipNotSkipCollection);
|
|
_PersonKeyToPersonContainer = new(personKeyToPersonContainer);
|
|
_EDistanceContentTicksDirectory = eDistanceContentTicksDirectory;
|
|
_NotMappedPersonContainers = notMappedPersonContainers.OrderByDescending(l => l.Key).ToList();
|
|
_IdThenNormalizedRectangleToPersonContainers = new(idThenNormalizedRectangleToPersonContainers);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
|
|
return result;
|
|
}
|
|
|
|
public Dictionary<long, List<int>> GetPersonKeyToIds()
|
|
{
|
|
Dictionary<long, List<int>> results = new();
|
|
long personKey;
|
|
const int zero = 0;
|
|
List<int>? collection;
|
|
PersonBirthday personBirthday;
|
|
List<string> shouldMove = new();
|
|
foreach (KeyValuePair<int, Dictionary<int, PersonContainer[]>> idToCollection in _IdThenNormalizedRectangleToPersonContainers)
|
|
{
|
|
foreach (KeyValuePair<int, PersonContainer[]> normalizedRectangleToPersonContainers in idToCollection.Value)
|
|
{
|
|
foreach (PersonContainer personContainer in normalizedRectangleToPersonContainers.Value)
|
|
{
|
|
if (personContainer.Key is null || personContainer.Birthdays is null || !personContainer.Birthdays.Any())
|
|
continue;
|
|
if (personContainer.DisplayDirectoryName.Contains(@"{}\~\"))
|
|
shouldMove.Add(personContainer.DisplayDirectoryName);
|
|
personBirthday = personContainer.Birthdays[zero];
|
|
personKey = personBirthday.Value.Ticks;
|
|
if (!results.TryGetValue(personKey, out collection))
|
|
{
|
|
results.Add(personKey, new());
|
|
if (!results.TryGetValue(personKey, out collection))
|
|
throw new Exception();
|
|
}
|
|
if (collection.Contains(idToCollection.Key))
|
|
continue;
|
|
collection.Add(idToCollection.Key);
|
|
}
|
|
}
|
|
}
|
|
if (shouldMove.Any())
|
|
throw new Exception(string.Join(Environment.NewLine, shouldMove));
|
|
return results;
|
|
}
|
|
|
|
(bool, Dictionary<int, PersonContainer[]>?) Shared.Models.Methods.IMapLogic.GetNormalizedRectangleToPersonContainers(int id)
|
|
{
|
|
bool result = _IdThenNormalizedRectangleToPersonContainers.TryGetValue(id, out Dictionary<int, PersonContainer[]>? normalizedRectangleToPersonContainers);
|
|
return new(result, normalizedRectangleToPersonContainers);
|
|
}
|
|
|
|
public int UpdateMappingFromPerson(Mapping mapping)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
int result = 0;
|
|
long personKey;
|
|
const int zero = 0;
|
|
string mappingSegmentB;
|
|
PersonBirthday personBirthday;
|
|
PersonContainer[]? personContainers;
|
|
Dictionary<int, PersonContainer[]>? normalizedRectangleToPersonContainers;
|
|
for (int i = 1; i < 2; i++)
|
|
{
|
|
if (mapping.MappingFromLocation is null)
|
|
continue;
|
|
if (!_IdThenNormalizedRectangleToPersonContainers.TryGetValue(mapping.MappingFromItem.Id, out normalizedRectangleToPersonContainers))
|
|
{
|
|
if (mapping.MappingFromFilter.InSkipCollection is not null && mapping.MappingFromFilter.InSkipCollection.Value)
|
|
continue;
|
|
result += 1;
|
|
continue;
|
|
}
|
|
if (!normalizedRectangleToPersonContainers.TryGetValue(mapping.MappingFromLocation.NormalizedRectangle, out personContainers))
|
|
{
|
|
if (mapping.MappingFromFilter.InSkipCollection is not null && mapping.MappingFromFilter.InSkipCollection.Value)
|
|
continue;
|
|
result += 1;
|
|
continue;
|
|
}
|
|
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;
|
|
mappingSegmentB = Stateless.MapLogic.GetMappingSegmentB(_Ticks, personBirthday, personContainer.ApproximateYears, mapping.MappingFromItem);
|
|
mapping.UpdateMappingFromPerson(personContainer.ApproximateYears, personContainer.DisplayDirectoryName, personBirthday, mappingSegmentB);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public int UpdateMappingFromPerson(Mapping[] mappingCollection)
|
|
{
|
|
int result = 0;
|
|
foreach (Mapping mapping in mappingCollection)
|
|
{
|
|
if (mapping.MappingFromLocation is null)
|
|
continue;
|
|
result += UpdateMappingFromPerson(mapping);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public 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)
|
|
{
|
|
progressBar.Tick();
|
|
if (string.IsNullOrEmpty(saveContainer.Directory) || string.IsNullOrEmpty(saveContainer.CheckFile) || saveContainer.FaceFileHolder is null)
|
|
continue;
|
|
if (saveContainer.FacePartsFileHolder is null && saveContainer.HiddenFaceFileHolder is null && saveContainer.ResizedFileHolder is null)
|
|
{
|
|
checkFile = saveContainer.CheckFile;
|
|
sourceFile = saveContainer.FaceFileHolder.FullName;
|
|
}
|
|
else 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
|
|
{
|
|
string description = saveContainer.FaceFileHolder is not null ? saveContainer.FaceFileHolder.Name : string.Empty;
|
|
windowsShortcut = new() { Path = saveContainer.ResizedFileHolder.FullName, Description = description };
|
|
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;
|
|
if (mapping.MappingFromLocation is null)
|
|
result = null;
|
|
else
|
|
{
|
|
string checkFile;
|
|
string? facesDirectory = Stateless.MapLogic.GetFacesDirectory(dFacesContentDirectory, mapping.MappingFromItem);
|
|
if (facesDirectory is null)
|
|
result = null;
|
|
else
|
|
{
|
|
FileHolder faceFileHolder = new(Path.Combine(facesDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacesFileNameExtension}"));
|
|
if (!faceFileHolder.Exists)
|
|
result = null;
|
|
else
|
|
{
|
|
string shortcutFile = string.Empty;
|
|
string? facePartsDirectory = Stateless.MapLogic.GetFacePartsDirectory(d2FacePartsContentDirectory, mapping.MappingFromItem);
|
|
if (facePartsDirectory is null)
|
|
result = null;
|
|
else
|
|
{
|
|
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 string GetDirectory(bool saveIndividually, int padLeft, string? segmentC, 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.NormalizedRectangle.ToString());
|
|
for (int i = _NotMappedPersonContainers.Count - 1; i > 0; 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);
|
|
if (!saveIndividually || segmentC is null)
|
|
result = Path.Combine(_EDistanceContentTicksDirectory, by, personKeyFormatted, mappingSegmentB);
|
|
else
|
|
result = Path.Combine(_EDistanceContentTicksDirectory, by, segmentC.PadLeft(padLeft, '0'), personKeyFormatted, mappingSegmentB);
|
|
_NotMappedPersonContainers.RemoveAt(i);
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private List<SaveContainer> GetSaveContainers(string dFacesContentDirectory, string d2FacePartsContentDirectory, string d2FacePartsContentCollectionDirectory, Mapping[] mappingCollection, Dictionary<int, Dictionary<int, Mapping>> idToNormalizedRectangleToMapping, Dictionary<long, List<int>> personKeyToIds, int? useFiltersCounter, bool saveMapped, bool saveIndividually, bool sortingContainersAny)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
List<SaveContainer> results = new();
|
|
string by;
|
|
long ticks;
|
|
List<int>? ids;
|
|
long personKey;
|
|
bool isByMapping;
|
|
bool isBySorting;
|
|
string checkFile;
|
|
string directory;
|
|
string shortcutFile;
|
|
Mapping? keyMapping;
|
|
string? directoryName;
|
|
string personDirectory;
|
|
string? facesDirectory;
|
|
FileHolder faceFileHolder;
|
|
string personKeyFormatted;
|
|
List<int> distinct = new();
|
|
string? facePartsDirectory;
|
|
SaveContainer? saveContainer;
|
|
FileHolder facePartsFileHolder;
|
|
FileHolder hiddenFaceFileHolder;
|
|
string? facePartsContentCollectionFile;
|
|
Dictionary<int, Mapping>? normalizedRectangleToMapping;
|
|
int padLeft = _Configuration.FaceDistancePermyriad.ToString().Length;
|
|
string forceSingleImageHumanized = nameof(Shared.Models.Stateless.IMapLogic.ForceSingleImage).Humanize(LetterCasing.Title);
|
|
foreach (Mapping mapping in mappingCollection)
|
|
{
|
|
if (mapping.MappingFromLocation is null)
|
|
continue;
|
|
directoryName = Path.GetDirectoryName(mapping.MappingFromItem.RelativePath);
|
|
if (directoryName is null)
|
|
throw new NotSupportedException();
|
|
if (mapping.MappingFromFilter.InSkipCollection is not null && mapping.MappingFromFilter.InSkipCollection.Value)
|
|
continue;
|
|
(by, isByMapping, isBySorting) = Stateless.MapLogic.Get(useFiltersCounter, saveIndividually, sortingContainersAny, forceSingleImageHumanized, mapping);
|
|
if (isByMapping && !saveMapped)
|
|
continue;
|
|
if (mapping.MappingFromPerson is null)
|
|
{
|
|
if (!_Configuration.SaveSortingWithoutPerson)
|
|
continue;
|
|
if (mapping.SortingContainer is null)
|
|
{
|
|
if (sortingContainersAny)
|
|
continue;
|
|
mapping.UpdateMappingFromUnknownPerson(new(mapping, new(mapping, mapping.MappingFromLocation)));
|
|
if (mapping.SortingContainer is null)
|
|
continue;
|
|
}
|
|
if (distinct.Contains(mapping.MappingFromItem.Id))
|
|
continue;
|
|
if (distinct.Contains(mapping.SortingContainer.Sorting.Id))
|
|
continue;
|
|
ticks = DateTime.Now.Ticks;
|
|
directory = GetDirectory(saveIndividually, padLeft, mapping.SegmentC, by, mapping.MappingFromItem, mapping.SortingContainer);
|
|
personDirectory = Path.Combine(directory, $"Z]{ticks}");
|
|
if (saveIndividually)
|
|
{
|
|
directory = Path.Combine(directory, mapping.MappingFromItem.Id.ToString());
|
|
results.Add(new(Path.Combine(directory, $"Z]{ticks}")));
|
|
}
|
|
distinct.Add(mapping.MappingFromItem.Id);
|
|
distinct.Add(mapping.SortingContainer.Sorting.Id);
|
|
}
|
|
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);
|
|
if (string.IsNullOrEmpty(mapping.SegmentC))
|
|
directory = Path.Combine(_EDistanceContentTicksDirectory, by, personKeyFormatted, mapping.MappingFromPerson.SegmentB);
|
|
else if (saveIndividually)
|
|
directory = Path.Combine(_EDistanceContentTicksDirectory, by, mapping.SegmentC.PadLeft(padLeft, '0'), personKeyFormatted, mapping.MappingFromPerson.SegmentB);
|
|
else
|
|
directory = Path.Combine(_EDistanceContentTicksDirectory, by, personKeyFormatted, mapping.MappingFromPerson.SegmentB, mapping.SegmentC);
|
|
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 (saveIndividually)
|
|
{
|
|
directory = Path.Combine(directory, mapping.MappingFromItem.Id.ToString());
|
|
results.Add(new(Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName)));
|
|
}
|
|
if (isByMapping && personKeyToIds.TryGetValue(personKey, out ids))
|
|
results.Add(new(Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName, $"{ids.Count} Face(s)")));
|
|
}
|
|
results.Add(new(personDirectory));
|
|
if (!isBySorting || mapping.SortingContainer is null)
|
|
keyMapping = null;
|
|
else
|
|
{
|
|
if (!idToNormalizedRectangleToMapping.TryGetValue(mapping.SortingContainer.Sorting.Id, out normalizedRectangleToMapping))
|
|
continue;
|
|
if (!normalizedRectangleToMapping.ContainsKey(mapping.SortingContainer.Sorting.NormalizedRectangle))
|
|
continue;
|
|
keyMapping = normalizedRectangleToMapping[mapping.SortingContainer.Sorting.NormalizedRectangle];
|
|
if (keyMapping.MappingFromLocation is null)
|
|
continue;
|
|
if (keyMapping.MappingFromLocation.NormalizedRectangle == mapping.MappingFromLocation.NormalizedRectangle)
|
|
results.Add(new(Path.Combine(directory, "Maybe")));
|
|
}
|
|
facesDirectory = Stateless.MapLogic.GetFacesDirectory(dFacesContentDirectory, mapping.MappingFromItem);
|
|
if (facesDirectory is null)
|
|
continue;
|
|
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
|
|
{
|
|
if (saveIndividually)
|
|
{
|
|
facePartsContentCollectionFile = Stateless.MapLogic.GetFacePartsContentCollectionFile(_Configuration.FacePartsFileNameExtension, d2FacePartsContentCollectionDirectory, mapping.MappingFromItem);
|
|
if (facePartsContentCollectionFile is null)
|
|
continue;
|
|
(saveContainer, SaveContainer extraSaveContainer) = Stateless.MapLogic.GetContainers(_Configuration.FacesFileNameExtension, _Configuration.FacePartsFileNameExtension, directory, faceFileHolder, facePartsContentCollectionFile, mapping);
|
|
results.Add(extraSaveContainer);
|
|
}
|
|
else
|
|
{
|
|
facePartsDirectory = Stateless.MapLogic.GetFacePartsDirectory(d2FacePartsContentDirectory, mapping.MappingFromItem);
|
|
if (facePartsDirectory is null)
|
|
continue;
|
|
shortcutFile = Path.Combine(personDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}.lnk");
|
|
checkFile = Path.Combine(directory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}");
|
|
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 || keyMapping is null)
|
|
continue;
|
|
if (!saveIndividually && isBySorting && mapping.MappingFromPerson is null)
|
|
{
|
|
saveContainer = GetMatchSaveContainer(dFacesContentDirectory, d2FacePartsContentDirectory, directory, keyMapping);
|
|
if (saveContainer is not null)
|
|
results.Add(saveContainer);
|
|
}
|
|
if (!saveIndividually)
|
|
saveContainer = Stateless.MapLogic.GetDebugSaveContainer(directory, mapping.SortingContainer, keyMapping);
|
|
else
|
|
{
|
|
(saveContainer, SaveContainer? extraSaveContainer) = Stateless.MapLogic.GetContainers(_Configuration.FacesFileNameExtension, _Configuration.FacePartsFileNameExtension, dFacesContentDirectory, d2FacePartsContentCollectionDirectory, directory, mapping.SortingContainer, keyMapping);
|
|
if (saveContainer is null || extraSaveContainer is null)
|
|
continue;
|
|
results.Add(extraSaveContainer);
|
|
}
|
|
results.Add(saveContainer);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public List<SaveContainer> GetSaveContainers(bool saveIndividually, string dFacesContentDirectory, string d2FacePartsContentDirectory, string d2FacePartsContentCollectionDirectory, Mapping[] mappingCollection, Dictionary<int, Dictionary<int, Mapping>> idToNormalizedRectangleToMapping, int? useFiltersCounter, bool sortingContainersAny)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
List<SaveContainer> results;
|
|
bool saveMapped = false;
|
|
Dictionary<long, List<int>> personKeyToIds = new();
|
|
results = GetSaveContainers(dFacesContentDirectory, d2FacePartsContentDirectory, d2FacePartsContentCollectionDirectory, mappingCollection, idToNormalizedRectangleToMapping, personKeyToIds, useFiltersCounter, saveMapped, saveIndividually, sortingContainersAny);
|
|
return results;
|
|
}
|
|
|
|
public void SaveMapped(string dFacesContentDirectory, string d2FacePartsContentDirectory, string d2FacePartsContentCollectionDirectory, Dictionary<long, List<int>> personKeyToIds, Mapping[] mappingCollection, Dictionary<int, Dictionary<int, Mapping>> idToNormalizedRectangleToMapping, int totalNotMapped)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
int? updated = null;
|
|
bool saveMapped = true;
|
|
int? useFiltersCounter = null;
|
|
string mappingDirectory = Path.Combine(_EDistanceContentTicksDirectory, nameof(Shared.Models.Stateless.IMapLogic.Mapping));
|
|
List<SaveContainer> saveContainers = GetSaveContainers(dFacesContentDirectory, d2FacePartsContentDirectory, d2FacePartsContentCollectionDirectory, mappingCollection, idToNormalizedRectangleToMapping, personKeyToIds, useFiltersCounter, saveMapped, sortingContainersAny: true, saveIndividually: false);
|
|
SaveContainers(totalNotMapped, updated, saveContainers);
|
|
if (!string.IsNullOrEmpty(_EDistanceContentTicksDirectory) && Directory.Exists(mappingDirectory))
|
|
Stateless.MapLogic.SaveMappingShortcuts(mappingDirectory);
|
|
}
|
|
|
|
public bool Used(FaceDistance faceDistanceEncoding)
|
|
{
|
|
bool result = false;
|
|
if (faceDistanceEncoding.NormalizedRectangle is null)
|
|
throw new NotSupportedException();
|
|
List<int>? normalizedRectangles;
|
|
Dictionary<int, PersonContainer[]>? normalizedRectangleToPersonContainers;
|
|
if (_SkipCollection.TryGetValue(faceDistanceEncoding.Id, out normalizedRectangles) && normalizedRectangles.Contains(faceDistanceEncoding.NormalizedRectangle.Value))
|
|
result = true;
|
|
if (!result && _IdThenNormalizedRectangleToPersonContainers.TryGetValue(faceDistanceEncoding.Id, out normalizedRectangleToPersonContainers))
|
|
{
|
|
if (normalizedRectangleToPersonContainers.ContainsKey(faceDistanceEncoding.NormalizedRectangle.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>? normalizedRectangles;
|
|
bool skipNotSkipCollectionAny = _SkipNotSkipCollection.Any();
|
|
Dictionary<int, PersonContainer[]>? normalizedRectangleToPersonContainers;
|
|
for (int j = 0; j < faceDistanceLengths.Count; j++)
|
|
{
|
|
if (faceDistanceEncoding.NormalizedRectangle is null)
|
|
throw new NotSupportedException();
|
|
if (j == i)
|
|
continue;
|
|
if (_SkipCollection.TryGetValue(faceDistanceEncoding.Id, out normalizedRectangles) && normalizedRectangles.Contains(faceDistanceEncoding.NormalizedRectangle.Value))
|
|
continue;
|
|
if (skipNotSkipCollectionAny && (!_SkipNotSkipCollection.TryGetValue(faceDistanceEncoding.Id, out normalizedRectangles) || !normalizedRectangles.Contains(faceDistanceEncoding.NormalizedRectangle.Value)))
|
|
continue;
|
|
if (_IdThenNormalizedRectangleToPersonContainers.TryGetValue(faceDistanceEncoding.Id, out normalizedRectangleToPersonContainers))
|
|
{
|
|
if (normalizedRectangleToPersonContainers.ContainsKey(faceDistanceEncoding.NormalizedRectangle.Value))
|
|
continue;
|
|
}
|
|
faceDistanceLength = faceDistanceLengths[j];
|
|
if (faceDistanceLength.NormalizedRectangle is null || faceDistanceLength.Length is null)
|
|
throw new NotSupportedException();
|
|
if (faceDistanceLength.Length == 0)
|
|
continue;
|
|
sorting = ISorting.Get(_Configuration.FaceDistancePermyriad, _Configuration.RangeDistanceTolerance, faceDistanceEncoding, faceDistanceLength);
|
|
if (sorting.DistancePermyriad == 0)
|
|
continue;
|
|
if (sorting.Id == faceDistanceEncoding.Id)
|
|
{
|
|
if (sorting.NormalizedRectangle == faceDistanceEncoding.NormalizedRectangle.Value)
|
|
continue;
|
|
continue;
|
|
}
|
|
results.Add(sorting);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public int UpdateFromSortingContainers(bool saveIndividually, Shared.Models.Methods.IDistanceLimits distanceLimits, SortingContainer[] sortingContainers)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
if (distanceLimits is not null)
|
|
{
|
|
string counts = distanceLimits.GetCounts();
|
|
_ = Directory.CreateDirectory(Path.Combine(_EDistanceContentTicksDirectory, counts));
|
|
}
|
|
int result = 0;
|
|
string key;
|
|
const int zero = 0;
|
|
string mappingSegmentB;
|
|
string personKeyFormatted;
|
|
PersonBirthday personBirthday;
|
|
PersonContainer[]? personContainers;
|
|
Dictionary<string, int> keyToCount = new();
|
|
List<int> normalizedRectangleCollectionForA;
|
|
List<int> normalizedRectangleCollectionForB;
|
|
Dictionary<string, string> keyToSegmentC = new();
|
|
Dictionary<int, List<int>> idToNormalizedRectangleCollectionForA = new();
|
|
Dictionary<int, List<int>> idToNormalizedRectangleCollectionForB = new();
|
|
Dictionary<int, PersonContainer[]>? normalizedRectangleToPersonContainers;
|
|
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?.MappingFromLocation is null)
|
|
throw new NotSupportedException();
|
|
if (!idToNormalizedRectangleCollectionForA.ContainsKey(sortingContainer.Mapping.MappingFromItem.Id))
|
|
idToNormalizedRectangleCollectionForA.Add(sortingContainer.Mapping.MappingFromItem.Id, new());
|
|
normalizedRectangleCollectionForA = idToNormalizedRectangleCollectionForA[sortingContainer.Mapping.MappingFromItem.Id];
|
|
if (!idToNormalizedRectangleCollectionForB.ContainsKey(sortingContainer.Mapping.MappingFromItem.Id))
|
|
idToNormalizedRectangleCollectionForB.Add(sortingContainer.Mapping.MappingFromItem.Id, new());
|
|
normalizedRectangleCollectionForB = idToNormalizedRectangleCollectionForB[sortingContainer.Mapping.MappingFromItem.Id];
|
|
if (!_IdThenNormalizedRectangleToPersonContainers.TryGetValue(sortingContainer.Sorting.Id, out normalizedRectangleToPersonContainers) || !normalizedRectangleToPersonContainers.TryGetValue(sortingContainer.Sorting.NormalizedRectangle, out personContainers))
|
|
{
|
|
if (!_Configuration.SaveSortingWithoutPerson)
|
|
continue;
|
|
if (normalizedRectangleCollectionForA.Contains(sortingContainer.Mapping.MappingFromLocation.NormalizedRectangle))
|
|
continue;
|
|
sortingContainer.Mapping.UpdateMappingFromUnknownPerson(sortingContainer);
|
|
normalizedRectangleCollectionForA.Add(sortingContainer.Mapping.MappingFromLocation.NormalizedRectangle);
|
|
result += 1;
|
|
}
|
|
else
|
|
{
|
|
if (normalizedRectangleCollectionForB.Contains(sortingContainer.Mapping.MappingFromLocation.NormalizedRectangle))
|
|
continue;
|
|
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 (!keyToCount.ContainsKey(key))
|
|
keyToCount.Add(key, new());
|
|
if (!keyToCount.ContainsKey(key))
|
|
keyToCount.Add(key, 0);
|
|
if (!keyToSegmentC.ContainsKey(key))
|
|
keyToSegmentC.Add(key, string.Empty);
|
|
keyToCount[key]++;
|
|
if (saveIndividually || keyToCount[key] > _Configuration.SortingMaximumPerKey)
|
|
{
|
|
keyToCount[key] = 0;
|
|
keyToSegmentC[key] = sortingContainer.Sorting.DistancePermyriad.ToString();
|
|
}
|
|
sortingContainer.Mapping.UpdateMappingFromPerson(personContainer.ApproximateYears, personContainer.DisplayDirectoryName, personBirthday, mappingSegmentB, keyToSegmentC[key], sortingContainer);
|
|
normalizedRectangleCollectionForB.Add(sortingContainer.Mapping.MappingFromLocation.NormalizedRectangle);
|
|
result += 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public void CopyManualFiles(string dFacesContentDirectory, Dictionary<int, Dictionary<int, Mapping>> idToNormalizedRectangleToMapping)
|
|
{
|
|
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 mappingSegmentB;
|
|
string personDirectory;
|
|
string? facesDirectory;
|
|
int? normalizedRectangle;
|
|
string personKeyFormatted;
|
|
string personDisplayFileName;
|
|
PersonBirthday personBirthday;
|
|
string? personDisplayDirectory;
|
|
WindowsShortcut windowsShortcut;
|
|
Dictionary<int, Mapping>? normalizedRectangleToMapping;
|
|
string by = nameof(Shared.Models.Stateless.IMapLogic.ManualCopy);
|
|
Dictionary<int, PersonContainer[]>? normalizedRectangleToPeronContainerCollection;
|
|
string successful = $"_ {nameof(Shared.Models.Stateless.IMapLogic.ManualCopy).Humanize(LetterCasing.Title)} Successful";
|
|
foreach (KeyValuePair<long, PersonContainer> personKeyToPersonContainer in _PersonKeyToPersonContainer)
|
|
{
|
|
if (personKeyToPersonContainer.Value.Key is null || personKeyToPersonContainer.Value.Birthdays is null || !personKeyToPersonContainer.Value.Birthdays.Any())
|
|
continue;
|
|
personBirthday = personKeyToPersonContainer.Value.Birthdays[zero];
|
|
foreach (string personDisplayDirectoryAllFile in personKeyToPersonContainer.Value.DisplayDirectoryAllFiles)
|
|
{
|
|
if (!personDisplayDirectoryAllFile.EndsWith(_Configuration.FacesFileNameExtension))
|
|
continue;
|
|
(id, normalizedRectangle) = IMapping.GetConverted(_Configuration.FacesFileNameExtension, personDisplayDirectoryAllFile);
|
|
if (id is null || normalizedRectangle 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, personKeyToPersonContainer.Value.ApproximateYears, fileInfo.CreationTime, isWrongYear: null);
|
|
directory = Path.Combine(_EDistanceContentTicksDirectory, by, personKeyFormatted, mappingSegmentB);
|
|
personDirectory = Path.Combine(directory, personKeyToPersonContainer.Value.DisplayDirectoryName, "lnk");
|
|
if (!idToNormalizedRectangleToMapping.TryGetValue(id.Value, out normalizedRectangleToMapping))
|
|
continue;
|
|
if (!normalizedRectangleToMapping.ContainsKey(normalizedRectangle.Value))
|
|
continue;
|
|
mapping = normalizedRectangleToMapping[normalizedRectangle.Value];
|
|
if (mapping.MappingFromLocation is null)
|
|
continue;
|
|
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 = Stateless.MapLogic.GetFacesDirectory(dFacesContentDirectory, mapping.MappingFromItem);
|
|
if (facesDirectory is null)
|
|
continue;
|
|
faceFileName = $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacesFileNameExtension}";
|
|
checkFile = Path.Combine(directory, fileInfo.Name);
|
|
if (!_IdThenNormalizedRectangleToPersonContainers.TryGetValue(id.Value, out normalizedRectangleToPeronContainerCollection) || !normalizedRectangleToPeronContainerCollection.ContainsKey(normalizedRectangle.Value))
|
|
{
|
|
if (!Directory.Exists(personDirectory))
|
|
_ = Directory.CreateDirectory(personDirectory);
|
|
if (!File.Exists(checkFile))
|
|
File.Copy(personDisplayDirectoryAllFile, checkFile);
|
|
}
|
|
if (personDisplayDirectoryAllFile.Contains(successful))
|
|
continue;
|
|
directoryName = Path.Combine(personDisplayDirectory, successful);
|
|
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 CopyAtLeastOneMappedFiles(string dFacesContentDirectory, string a2PeopleSingletonDirectory, Mapping[] mappingCollection)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
long personKey;
|
|
string fileName;
|
|
string faceFile;
|
|
string directory;
|
|
string faceFileName;
|
|
string? directoryName;
|
|
string? facesDirectory;
|
|
PersonContainer? personContainer;
|
|
Dictionary<long, int> personKeyToCount = new();
|
|
foreach (Mapping mapping in mappingCollection)
|
|
{
|
|
directoryName = Path.GetDirectoryName(mapping.MappingFromItem.RelativePath);
|
|
if (directoryName is null)
|
|
throw new NotSupportedException();
|
|
if (mapping.MappingFromLocation is null || mapping.MappingFromPerson is null)
|
|
continue;
|
|
if (mapping.MappingFromItem.ResizedFileHolder.DirectoryName is null || !mapping.MappingFromItem.ResizedFileHolder.Exists)
|
|
continue;
|
|
if (mapping.By is null or Shared.Models.Stateless.IMapLogic.Sorting)
|
|
continue;
|
|
if (string.IsNullOrEmpty(mapping.MappingFromPerson.SegmentB))
|
|
throw new NotSupportedException();
|
|
if (string.IsNullOrEmpty(mapping.MappingFromPerson.DisplayDirectoryName))
|
|
throw new NotSupportedException();
|
|
facesDirectory = Stateless.MapLogic.GetFacesDirectory(dFacesContentDirectory, mapping.MappingFromItem);
|
|
if (facesDirectory is null)
|
|
continue;
|
|
faceFileName = $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacesFileNameExtension}";
|
|
faceFile = Path.Combine(facesDirectory, faceFileName);
|
|
personKey = mapping.MappingFromPerson.PersonBirthday.Value.Ticks;
|
|
if (!personKeyToCount.ContainsKey(personKey))
|
|
personKeyToCount.Add(personKey, 0);
|
|
if (personKeyToCount[personKey] > _Configuration.PersonCharactersCopyCount)
|
|
continue;
|
|
if (!_PersonKeyToPersonContainer.TryGetValue(personKey, out personContainer))
|
|
continue;
|
|
if (personContainer.Char is null || !_Configuration.PersonCharacters.Contains(personContainer.Char.Value))
|
|
continue;
|
|
if (personContainer.DisplayDirectoryAllFiles.Any(l => l.EndsWith(faceFileName)))
|
|
continue;
|
|
if (!File.Exists(faceFile))
|
|
continue;
|
|
directory = Path.Combine(a2PeopleSingletonDirectory, personContainer.Char.Value.ToString(), personContainer.DisplayDirectoryName);
|
|
if (!Directory.Exists(directory))
|
|
continue;
|
|
fileName = Path.Combine(directory, faceFileName);
|
|
if (File.Exists(fileName))
|
|
continue;
|
|
personKeyToCount[personKey] += 1;
|
|
File.Copy(faceFile, fileName);
|
|
}
|
|
}
|
|
|
|
private (string, PersonBirthday?) GetPersonBirthday(string[] directoryNames)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
PersonBirthday? personBirthday = null;
|
|
string personKeyFormatted = string.Empty;
|
|
foreach (string directoryName in directoryNames)
|
|
{
|
|
personBirthday = IPersonBirthday.GetPersonBirthday(_Configuration.PersonBirthdayFormat, directoryName);
|
|
if (personBirthday is not null)
|
|
{
|
|
personKeyFormatted = directoryName;
|
|
break;
|
|
}
|
|
}
|
|
return new(personKeyFormatted, personBirthday);
|
|
}
|
|
|
|
private (string, PersonBirthday?) GetPersonBirthday(string windowsShortcutPath)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
string[] directoryNames = IPath.GetDirectoryNames(windowsShortcutPath);
|
|
(string personKeyFormatted, PersonBirthday? personBirthday) = GetPersonBirthday(directoryNames);
|
|
if (personBirthday is null)
|
|
{
|
|
string[] directories = Directory.GetDirectories(windowsShortcutPath, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string directory in directories)
|
|
{
|
|
directoryNames = IPath.GetDirectoryNames(directory);
|
|
(personKeyFormatted, personBirthday) = GetPersonBirthday(directoryNames);
|
|
if (personBirthday is not null)
|
|
break;
|
|
}
|
|
}
|
|
return new(personKeyFormatted, personBirthday);
|
|
}
|
|
|
|
private List<string> GetPersonKeyFormattedCollection(string[] jLinks, string a2PeopleContentDirectory, PersonContainer[] personContainers, Dictionary<long, List<int>> personKeyToIds)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
List<string> results = new();
|
|
string[] files;
|
|
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(a2PeopleContentDirectory, 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 (string.IsNullOrEmpty(windowsShortcut.Path))
|
|
continue;
|
|
if (!Directory.Exists(windowsShortcut.Path))
|
|
{
|
|
if (files.Length != checkDirectories.Length)
|
|
throw new NotSupportedException(fileNameWithoutExtension);
|
|
continue;
|
|
}
|
|
(personKeyFormatted, personBirthday) = GetPersonBirthday(windowsShortcut.Path);
|
|
if (personBirthday is null)
|
|
throw new NotSupportedException(fileNameWithoutExtension);
|
|
if (!personKeyToIds.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, $"{personKeyToIds[personBirthday.Value.Ticks].Count} Face(s)")));
|
|
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())
|
|
continue;
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, personKey);
|
|
if (!displayDirectoryName.Contains(personKeyFormatted))
|
|
continue;
|
|
if (!Directory.Exists(displayDirectoryName))
|
|
_ = Directory.CreateDirectory(displayDirectoryName);
|
|
results.Add(personKeyFormatted);
|
|
}
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private (int, FileHolder, int, string, string, string, string)[] GetCollectionForSaveFilteredOriginalImagesFromJLinks(string[] jLinks, string a2PeopleContentDirectory, PersonContainer[] personContainers, Mapping[] mappingCollection, Dictionary<long, List<int>> personKeyToIds)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
(int, FileHolder, int, string, string, string, string)[] results;
|
|
int count = 0;
|
|
int group = 65;
|
|
string checkFile;
|
|
string directory;
|
|
string? directoryName;
|
|
string personDirectory;
|
|
string personKeyFormatted;
|
|
List<int> distinctCollection = new();
|
|
bool usePersonKeyAndDeterministicHashCodeKey = false;
|
|
List<string> personKeyFormattedCollection = GetPersonKeyFormattedCollection(jLinks, a2PeopleContentDirectory, personContainers, personKeyToIds);
|
|
List<(int Id, FileHolder ImageFileHolder, int ApproximateYears, string PersonKeyFormatted, string CheckFile, string Directory, string PersonDirectory)> collection = new();
|
|
foreach (Mapping mapping in mappingCollection)
|
|
{
|
|
if (distinctCollection.Contains(mapping.MappingFromItem.Id))
|
|
continue;
|
|
directoryName = Path.GetDirectoryName(mapping.MappingFromItem.RelativePath);
|
|
if (directoryName is null)
|
|
throw new NotSupportedException();
|
|
if (mapping.By is null or Shared.Models.Stateless.IMapLogic.Sorting)
|
|
continue;
|
|
if (mapping.MappingFromPerson?.ApproximateYears is null || mapping.MappingFromLocation 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;
|
|
if (!usePersonKeyAndDeterministicHashCodeKey)
|
|
{
|
|
if (count > 499)
|
|
{
|
|
count = 0;
|
|
group += 1;
|
|
}
|
|
directory = Path.Combine(_EDistanceContentTicksDirectory, ((char)group).ToString());
|
|
personDirectory = Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName);
|
|
checkFile = Path.Combine(directory, $"{mapping.MappingFromItem.Id}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}");
|
|
}
|
|
else
|
|
{
|
|
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.ImageFileHolder, mapping.MappingFromPerson.ApproximateYears.Value, personKeyFormatted, directory, personDirectory, checkFile));
|
|
distinctCollection.Add(mapping.MappingFromItem.Id);
|
|
count += 1;
|
|
}
|
|
results = (from l in collection orderby l.ApproximateYears descending, l.PersonKeyFormatted descending select l).ToArray();
|
|
return results;
|
|
}
|
|
|
|
public void SaveFilteredOriginalImagesFromJLinks(string[] jLinks, PersonContainer[] personContainers, string a2PeopleContentDirectory, Dictionary<long, List<int>> personKeyToIds, Mapping[] mappingCollection, int totalNotMapped)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
SaveContainer? saveContainer;
|
|
List<SaveContainer> saveContainers = new();
|
|
Stateless.MapLogic.BeforeSaveFilteredOriginalImagesFromJLinks(jLinks, a2PeopleContentDirectory);
|
|
(int, FileHolder, int, string, string, string, string)[] collection = GetCollectionForSaveFilteredOriginalImagesFromJLinks(jLinks, a2PeopleContentDirectory, personContainers, mappingCollection, personKeyToIds);
|
|
foreach ((int id, FileHolder imageFileHolder, int approximateYears, string personKeyFormatted, string directory, string personDirectory, string checkFile) in collection)
|
|
{
|
|
saveContainer = new(personDirectory);
|
|
saveContainers.Add(saveContainer);
|
|
saveContainer = new(imageFileHolder, checkFile, directory);
|
|
saveContainers.Add(saveContainer);
|
|
}
|
|
SaveContainers(totalNotMapped, null, saveContainers);
|
|
}
|
|
|
|
private List<SaveShortcutsForOutputResolutions> GetCollectionForSaveShortcutsForOutputResolutionsPreMapLogic(string eDistanceContentDirectory, Dictionary<long, List<int>> personKeyToIds, Dictionary<int, List<LocationContainer<MetadataExtractor.Directory>>> idToLocationContainers, Mapping[] mappingCollection)
|
|
{
|
|
List<SaveShortcutsForOutputResolutions> results = new();
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
string? model;
|
|
long personKey;
|
|
string fileName;
|
|
string directory;
|
|
string directoryName;
|
|
List<long>? personKeys;
|
|
string personKeyFormatted;
|
|
IReadOnlyList<MetadataExtractor.Directory> directories;
|
|
List<LocationContainer<MetadataExtractor.Directory>>? locationContainers;
|
|
Dictionary<int, List<long>> idToPersonKeys = Stateless.Methods.IMapLogic.GetIdToPersonKeys(personKeyToIds);
|
|
foreach (Mapping mapping in mappingCollection)
|
|
{
|
|
if (mapping.MappingFromLocation is null || mapping.MappingFromItem.ImageFileHolder.DirectoryName is null || mapping.MappingFromPerson is null)
|
|
continue;
|
|
if (!idToLocationContainers.TryGetValue(mapping.MappingFromItem.Id, out locationContainers) || !locationContainers.Any())
|
|
directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(mapping.MappingFromItem.ResizedFileHolder.FullName);
|
|
else
|
|
{
|
|
directories = locationContainers.First().Directories;
|
|
model = Metadata.Models.Stateless.Methods.IMetadata.GetModel(directories);
|
|
if (model is null)
|
|
directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(mapping.MappingFromItem.ResizedFileHolder.FullName);
|
|
}
|
|
model = Metadata.Models.Stateless.Methods.IMetadata.GetModel(directories);
|
|
directoryName = Path.GetFileName(mapping.MappingFromItem.ImageFileHolder.DirectoryName);
|
|
if (!string.IsNullOrEmpty(model) && !string.IsNullOrEmpty(model.Trim()))
|
|
{
|
|
// Remove-Item -LiteralPath "\\?\D:\Tmp\a\EX-Z70 "
|
|
model = Regex.Replace(model.Trim(), @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_");
|
|
directory = Path.Combine($"{eDistanceContentDirectory}---", "Model Shortcuts", model, directoryName);
|
|
fileName = Path.Combine(directory, $"{mapping.MappingFromItem.ImageFileHolder.Name}.lnk");
|
|
results.Add(new(mapping.MappingFromItem.ImageFileHolder.FullName, directory, mapping.MappingFromItem.MinimumDateTime, fileName, mapping.MappingFromLocation.DeterministicHashCodeKey, MakeAllHidden: false));
|
|
}
|
|
if (!idToPersonKeys.TryGetValue(mapping.MappingFromItem.Id, out personKeys))
|
|
continue;
|
|
personKey = mapping.MappingFromPerson.PersonBirthday.Value.Ticks;
|
|
if (!personKeys.Contains(mapping.MappingFromPerson.PersonBirthday.Value.Ticks))
|
|
continue;
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, mapping.MappingFromPerson.PersonBirthday);
|
|
directory = Path.Combine($"{eDistanceContentDirectory}---", "Person Key Shortcuts", personKeyFormatted, directoryName);
|
|
fileName = Path.Combine(directory, $"{mapping.MappingFromItem.ImageFileHolder.Name}.lnk");
|
|
results.Add(new(mapping.MappingFromItem.ImageFileHolder.FullName, directory, mapping.MappingFromItem.MinimumDateTime, fileName, mapping.MappingFromLocation.DeterministicHashCodeKey, MakeAllHidden: false));
|
|
if (IPerson.IsDefaultName(_Configuration.MappingDefaultName, mapping.MappingFromPerson.DisplayDirectoryName))
|
|
continue;
|
|
directory = Path.Combine($"{eDistanceContentDirectory}---", "Name Shortcuts", mapping.MappingFromPerson.DisplayDirectoryName, directoryName);
|
|
fileName = Path.Combine(directory, $"{mapping.MappingFromItem.ImageFileHolder.Name}.lnk");
|
|
results.Add(new(mapping.MappingFromItem.ImageFileHolder.FullName, directory, mapping.MappingFromItem.MinimumDateTime, fileName, mapping.MappingFromLocation.DeterministicHashCodeKey, MakeAllHidden: false));
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public void SaveShortcutsForOutputResolutionsPreMapLogic(string eDistanceContentDirectory, Dictionary<long, List<int>> personKeyToIds, Dictionary<int, List<LocationContainer<MetadataExtractor.Directory>>> idToLocationContainers, Mapping[] mappingCollection)
|
|
{
|
|
string hiddenFile;
|
|
WindowsShortcut windowsShortcut;
|
|
List<SaveShortcutsForOutputResolutions> collection = new();
|
|
collection = GetCollectionForSaveShortcutsForOutputResolutionsPreMapLogic(eDistanceContentDirectory, personKeyToIds, idToLocationContainers, mappingCollection);
|
|
string[] distinctDirectories = (from l in collection select l.Directory).Distinct().ToArray();
|
|
foreach (string directory in distinctDirectories)
|
|
{
|
|
if (string.IsNullOrEmpty(directory))
|
|
continue;
|
|
if (!Directory.Exists(directory))
|
|
_ = Directory.CreateDirectory(directory);
|
|
}
|
|
foreach (SaveShortcutsForOutputResolutions s in collection)
|
|
{
|
|
hiddenFile = $"{s.FileName}.lvs";
|
|
if (File.Exists(hiddenFile))
|
|
continue;
|
|
if (s.Description is not null)
|
|
{
|
|
File.WriteAllLines(hiddenFile, new string[] { s.FullName, s.Description });
|
|
File.SetAttributes(hiddenFile, FileAttributes.Hidden);
|
|
File.SetLastWriteTime(hiddenFile, s.DateTime);
|
|
}
|
|
if (File.Exists(s.FileName))
|
|
continue;
|
|
try
|
|
{
|
|
windowsShortcut = new() { Path = s.FullName, Description = s.Description };
|
|
windowsShortcut.Save(s.FileName);
|
|
windowsShortcut.Dispose();
|
|
File.SetLastWriteTime(s.FileName, s.DateTime);
|
|
}
|
|
catch (Exception)
|
|
{ }
|
|
}
|
|
}
|
|
|
|
private (List<(string, DateTime[])>, List<SaveShortcutsForOutputResolutions>) GetCollectionForSaveShortcutsForOutputResolutionsDuringMapLogic(Dictionary<long, List<int>> personKeyToIds, string dFacesContentDirectory, List<Item> filteredItems, Mapping[] mappingCollection)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
long personKey;
|
|
string fileName;
|
|
string fullName;
|
|
string directory;
|
|
string? directoryName;
|
|
string personDirectory;
|
|
string? facesDirectory;
|
|
string personKeyFormatted;
|
|
List<string> distinct = new();
|
|
List<SaveShortcutsForOutputResolutions> collection = new();
|
|
List<(string, DateTime[])> directoriesAndDateTimes = new();
|
|
foreach (Item item in filteredItems)
|
|
{
|
|
if (item.ResizedFileHolder is null)
|
|
continue;
|
|
if (item.Faces.Any(l => l.FaceEncoding is not null))
|
|
continue;
|
|
foreach (Face face in item.Faces)
|
|
{
|
|
if (face.Mapping is null)
|
|
continue;
|
|
directoryName = Path.GetDirectoryName(face.Mapping.MappingFromItem.RelativePath);
|
|
if (directoryName is null)
|
|
throw new NotSupportedException();
|
|
if (item.ResizedFileHolder?.DirectoryName is null || !item.ResizedFileHolder.Exists || item.ImageFileHolder.LastWriteTime is null)
|
|
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, item.ImageFileHolder.LastWriteTime.Value, fileName, face.Mapping.MappingFromItem.Id.ToString(), MakeAllHidden: false));
|
|
if (face.Mapping.MappingFromItem.ContainerDateTimes.Any() && !distinct.Contains(item.ResizedFileHolder.DirectoryName))
|
|
{
|
|
distinct.Add(item.ResizedFileHolder.DirectoryName);
|
|
directoriesAndDateTimes.Add(new(item.ResizedFileHolder.DirectoryName, face.Mapping.MappingFromItem.ContainerDateTimes));
|
|
}
|
|
}
|
|
}
|
|
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 Shared.Models.Stateless.IMapLogic.Sorting || mapping.MappingFromPerson?.ApproximateYears is null)
|
|
{
|
|
if (mapping.MappingFromItem.ContainerDateTimes.Any() && !distinct.Contains(mapping.MappingFromItem.ResizedFileHolder.DirectoryName))
|
|
{
|
|
distinct.Add(mapping.MappingFromItem.ResizedFileHolder.DirectoryName);
|
|
directoriesAndDateTimes.Add(new(mapping.MappingFromItem.ResizedFileHolder.DirectoryName, mapping.MappingFromItem.ContainerDateTimes));
|
|
}
|
|
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, mapping.MappingFromItem.MinimumDateTime, fileName, mapping.MappingFromLocation?.DeterministicHashCodeKey, MakeAllHidden: false));
|
|
facesDirectory = Stateless.MapLogic.GetFacesDirectory(dFacesContentDirectory, mapping.MappingFromItem);
|
|
if (facesDirectory is null || mapping.MappingFromLocation is null)
|
|
continue;
|
|
fullName = Path.Combine(facesDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_Configuration.FacesFileNameExtension}");
|
|
fileName = Path.Combine(personDirectory, $"{mapping.MappingFromLocation.DeterministicHashCodeKey}{mapping.MappingFromItem.ResizedFileHolder.ExtensionLowered}{_Configuration.FacesFileNameExtension}.lnk");
|
|
collection.Add(new(fullName, personDirectory, mapping.MappingFromItem.MinimumDateTime, fileName, mapping.MappingFromLocation.DeterministicHashCodeKey, MakeAllHidden: true));
|
|
}
|
|
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);
|
|
personKey = mapping.MappingFromPerson.PersonBirthday.Value.Ticks;
|
|
if (mapping.MappingFromItem.ContainerDateTimes.Any() && !distinct.Contains(mapping.MappingFromItem.ResizedFileHolder.DirectoryName))
|
|
{
|
|
distinct.Add(mapping.MappingFromItem.ResizedFileHolder.DirectoryName);
|
|
directoriesAndDateTimes.Add(new(mapping.MappingFromItem.ResizedFileHolder.DirectoryName, mapping.MappingFromItem.ContainerDateTimes));
|
|
}
|
|
directory = Path.Combine(mapping.MappingFromItem.ResizedFileHolder.DirectoryName, $"{_PropertyConfiguration.ResultAllInOne}Shortcuts", personKeyFormatted);
|
|
if (!personKeyToIds.ContainsKey(personKey))
|
|
personDirectory = Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName);
|
|
else
|
|
personDirectory = Path.Combine(directory, mapping.MappingFromPerson.DisplayDirectoryName, $"{personKeyToIds[personKey].Count} Face(s)");
|
|
fileName = Path.Combine(directory, $"{mapping.MappingFromItem.ResizedFileHolder.Name}.lnk");
|
|
collection.Add(new(mapping.MappingFromItem.ResizedFileHolder.FullName, personDirectory, mapping.MappingFromItem.MinimumDateTime, fileName, mapping.MappingFromLocation?.DeterministicHashCodeKey, MakeAllHidden: false));
|
|
}
|
|
}
|
|
return new(directoriesAndDateTimes, collection);
|
|
}
|
|
|
|
public void SaveShortcutsForOutputResolutionsDuringMapLogic(Dictionary<long, List<int>> personKeyToIds, string dFacesContentDirectory, List<Item> filteredItems, Mapping[] mappingCollection)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
WindowsShortcut windowsShortcut;
|
|
List<(string, DateTime[])> directoriesAndDateTimes;
|
|
List<SaveShortcutsForOutputResolutions> collection;
|
|
(directoriesAndDateTimes, collection) = GetCollectionForSaveShortcutsForOutputResolutionsDuringMapLogic(personKeyToIds, dFacesContentDirectory, filteredItems, mappingCollection);
|
|
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 (SaveShortcutsForOutputResolutions s in collection)
|
|
{
|
|
if (File.Exists(s.FileName))
|
|
continue;
|
|
try
|
|
{
|
|
windowsShortcut = new() { Path = s.FullName, Description = s.Description };
|
|
windowsShortcut.Save(s.FileName);
|
|
windowsShortcut.Dispose();
|
|
if (s.MakeAllHidden)
|
|
File.SetAttributes(s.FileName, FileAttributes.Hidden);
|
|
File.SetLastWriteTime(s.FileName, s.DateTime);
|
|
}
|
|
catch (Exception)
|
|
{ }
|
|
}
|
|
foreach ((string directory, DateTime[] dateTimes) in directoriesAndDateTimes)
|
|
{
|
|
if (!dateTimes.Any())
|
|
continue;
|
|
Directory.SetCreationTime(directory, dateTimes[0]);
|
|
Directory.SetLastWriteTime(directory, dateTimes[1]);
|
|
}
|
|
}
|
|
|
|
public Dictionary<int, Dictionary<int, PersonContainer[]>> GetMissing(Dictionary<int, Dictionary<int, Mapping>> idToNormalizedRectangleToMapping)
|
|
{
|
|
Dictionary<int, Dictionary<int, PersonContainer[]>> results = new();
|
|
foreach (KeyValuePair<int, Dictionary<int, PersonContainer[]>> idToCollection in _IdThenNormalizedRectangleToPersonContainers)
|
|
{
|
|
if (idToNormalizedRectangleToMapping.ContainsKey(idToCollection.Key))
|
|
continue;
|
|
results.Add(idToCollection.Key, idToCollection.Value);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public bool InSkipCollection(int id, MappingFromLocation mappingFromLocation)
|
|
{
|
|
bool result;
|
|
result = _SkipCollection.TryGetValue(id, out List<int>? normalizedRectangles) && normalizedRectangles.Contains(mappingFromLocation.NormalizedRectangle);
|
|
return result;
|
|
}
|
|
|
|
} |