95 lines
4.2 KiB
C#
95 lines
4.2 KiB
C#
namespace View_by_Distance.Shared.Models.Stateless.Methods;
|
|
|
|
internal abstract class Mapping
|
|
{
|
|
|
|
private static void IfNotAlreadyFileMove(string facesFileNameExtension, 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}{facesFileNameExtension}");
|
|
if (!File.Exists(checkFile))
|
|
File.Move(file, checkFile);
|
|
}
|
|
|
|
private static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKeysFromSegments(int locationDigits, string facesFileNameExtension, bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file, string fileName)
|
|
{
|
|
int? id;
|
|
List<Models.Face>? faces;
|
|
int? normalizedPixelPercentage;
|
|
(string? Id, string? NormalizedPixelPercentage, string? ExtensionLowered, bool? Check) segments = GetSegments(locationDigits, facesFileNameExtension, fileName);
|
|
if (string.IsNullOrEmpty(segments.Id) || string.IsNullOrEmpty(segments.NormalizedPixelPercentage) || string.IsNullOrEmpty(segments.ExtensionLowered) || segments.Check is null)
|
|
{
|
|
id = null;
|
|
faces = null;
|
|
normalizedPixelPercentage = null;
|
|
}
|
|
else if (!int.TryParse(segments.Id, out int idValue) || !int.TryParse(ILocation.GetRightPadded(locationDigits, segments.NormalizedPixelPercentage), out int normalizedPixelPercentageValue))
|
|
{
|
|
id = null;
|
|
faces = null;
|
|
normalizedPixelPercentage = null;
|
|
}
|
|
else
|
|
{
|
|
id = idValue;
|
|
normalizedPixelPercentage = normalizedPixelPercentageValue;
|
|
if (segments.Check.Value || segments.NormalizedPixelPercentage.Length != locationDigits)
|
|
IfNotAlreadyFileMove(facesFileNameExtension, file, idValue, normalizedPixelPercentageValue, segments.ExtensionLowered);
|
|
if (!keyValuePairsAny || !keyValuePairs.ContainsKey(idValue))
|
|
faces = null;
|
|
else
|
|
faces = keyValuePairs[idValue];
|
|
}
|
|
return new(id, normalizedPixelPercentage, faces);
|
|
}
|
|
|
|
internal static (string?, string?, string?, bool?) GetSegments(int locationDigits, string facesFileNameExtension, string fileName)
|
|
{
|
|
string[] segments = fileName.Split('.');
|
|
string? id;
|
|
string? extensionLowered;
|
|
bool? needsFacesFileNameExtension;
|
|
string? normalizedPixelPercentage;
|
|
if (segments.Length < 3 || (segments.Length == 4 && $".{segments[3]}" != facesFileNameExtension))
|
|
{
|
|
id = null;
|
|
extensionLowered = null;
|
|
normalizedPixelPercentage = null;
|
|
needsFacesFileNameExtension = null;
|
|
}
|
|
else
|
|
{
|
|
id = segments[0];
|
|
extensionLowered = $".{segments[2]}";
|
|
needsFacesFileNameExtension = segments.Length == 3;
|
|
normalizedPixelPercentage = ILocation.GetRightPadded(locationDigits, segments[1]);
|
|
}
|
|
return new(id, normalizedPixelPercentage, extensionLowered, needsFacesFileNameExtension);
|
|
}
|
|
|
|
internal static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKey(int locationDigits, string facesFileNameExtension, 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
|
|
(id, normalizedPixelPercentage, faces) = GetReversedDeterministicHashCodeKeysFromSegments(
|
|
locationDigits,
|
|
facesFileNameExtension,
|
|
keyValuePairsAny,
|
|
keyValuePairs,
|
|
file,
|
|
fileName);
|
|
return new(id, normalizedPixelPercentage, faces);
|
|
}
|
|
|
|
} |