2022-09-24 21:41:50 -07:00

81 lines
3.1 KiB
C#

namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Mapping
{
internal static (string?, string?, string?, bool?) GetSegments(string facesFileNameExtension, string fileName)
{
string[] segments = fileName.Split('.');
string? id;
string? extensionLowered;
bool? needsFacesFileNameExtension;
string? normalizedPixelPercentage;
if (segments.Length < 4 || $".{segments[3]}" != facesFileNameExtension)
{
id = null;
extensionLowered = null;
normalizedPixelPercentage = null;
needsFacesFileNameExtension = null;
}
else
{
id = segments[0];
extensionLowered = $".{segments[2]}";
normalizedPixelPercentage = segments[1];
needsFacesFileNameExtension = segments.Length == 3;
}
return new(id, normalizedPixelPercentage, extensionLowered, needsFacesFileNameExtension);
}
private static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKeysFromSegments(string facesFileNameExtension, bool idToFacesAny, Dictionary<int, List<Models.Face>> idToFaces, string fileName)
{
int? id;
List<Models.Face>? faces;
int? normalizedPixelPercentage;
(string? Id, string? NormalizedPixelPercentage, string? ExtensionLowered, bool? Check) segments = GetSegments(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(segments.NormalizedPixelPercentage, out int normalizedPixelPercentageValue))
{
id = null;
faces = null;
normalizedPixelPercentage = null;
}
else
{
id = idValue;
normalizedPixelPercentage = normalizedPixelPercentageValue;
if (!idToFacesAny || !idToFaces.ContainsKey(idValue))
faces = null;
else
faces = idToFaces[idValue];
}
return new(id, normalizedPixelPercentage, faces);
}
internal static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKey(string facesFileNameExtension, bool idToFacesAny, Dictionary<int, List<Models.Face>> idToFaces, 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(
facesFileNameExtension,
idToFacesAny,
idToFaces,
fileName);
return new(id, normalizedPixelPercentage, faces);
}
}