2022-09-16 21:05:34 -07:00

68 lines
2.7 KiB
C#

namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Mapping
{
private static void IfNotAlreadyFileMove(string file, int idValue, int normalizedPixelPercentageValue, string extensionLowered)
{
string? directoryName = Path.GetDirectoryName(file);
if (string.IsNullOrEmpty(directoryName))
throw new Exception();
string checkFile = Path.Combine(directoryName, $"{IMapping.GetDeterministicHashCodeKey(idValue, normalizedPixelPercentageValue)}{extensionLowered}");
if (!File.Exists(checkFile))
File.Move(file, checkFile);
}
private static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKeysFromSegments(int locationDigits, bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file, string[] segments)
{
int? id;
List<Models.Face>? faces;
int? normalizedPixelPercentage;
if (segments.Length != 3)
{
id = null;
faces = null;
normalizedPixelPercentage = null;
}
else if (!int.TryParse(segments[0], out int idValue) || !int.TryParse(segments[1].PadRight(locationDigits, '0'), out int normalizedPixelPercentageValue))
{
id = null;
faces = null;
normalizedPixelPercentage = null;
}
else
{
id = idValue;
string extensionLowered = $".{segments[2]}";
normalizedPixelPercentage = normalizedPixelPercentageValue;
if (segments[1].Length != locationDigits)
IfNotAlreadyFileMove(file, idValue, normalizedPixelPercentageValue, extensionLowered);
if (!keyValuePairsAny || !keyValuePairs.ContainsKey(idValue))
faces = null;
else
faces = keyValuePairs[idValue];
}
return new(id, normalizedPixelPercentage, faces);
}
internal static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKey(int locationDigits, bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file)
{
int? id;
List<Models.Face>? faces;
int? normalizedPixelPercentage;
string fileName = Path.GetFileName(file);
if (fileName.Length < 2 || fileName[1..].Contains('-'))
{
id = null;
faces = null;
normalizedPixelPercentage = null;
}
else
{
string[] segments = fileName.Split('.');
(id, normalizedPixelPercentage, faces) = GetReversedDeterministicHashCodeKeysFromSegments(locationDigits, keyValuePairsAny, keyValuePairs, file, segments);
}
return new(id, normalizedPixelPercentage, faces);
}
}