2022-08-22 09:10:19 -07:00

221 lines
9.2 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Shared.Models;
public class FaceFileSystem : FileSystem, Properties.IFaceFileSystem
{
// protected new string _ClassName;
protected string _Confidence;
// protected new string _DataDisplayFileName;
// protected new string _DataFullFileName;
// protected new string _Display;
protected int? _FaceBottom;
protected string _FaceFullFileName;
protected int? _FaceHeight;
protected int? _FaceLeft;
protected string _FaceRelativePath;
protected int? _FaceRight;
protected int? _FaceTop;
protected int? _FaceWidth;
protected int _ImageHeight;
protected int _ImageWidth;
// protected new DateTime _LastModified;
protected string _LocationDisplayIndex;
protected int? _LocationIndex;
protected bool? _Populated;
protected string _RelativePath;
protected string _SourceFullFileName;
protected string _SourceRelativePath;
protected long? _SourceSize;
// public new string ClassName => _ClassName;
public string Confidence => _Confidence;
// public new string DataDisplayFileName => _DataDisplayFileName;
// public new string DataFullFileName => _DataFullFileName;
// public new string Display => _Display;
public int? FaceBottom => _FaceBottom;
public string FaceFullFileName => _FaceFullFileName;
public int? FaceHeight => _FaceHeight;
public int? FaceLeft => _FaceLeft;
public string FaceRelativePath => _FaceRelativePath;
public int? FaceRight => _FaceRight;
public int? FaceTop => _FaceTop;
public int? FaceWidth => _FaceWidth;
public int ImageHeight => _ImageHeight;
public int ImageWidth => _ImageWidth;
// public new DateTime LastModified => _LastModified;
public string LocationDisplayIndex => _LocationDisplayIndex;
public int? LocationIndex => _LocationIndex;
public bool? Populated => _Populated;
public string RelativePath => _RelativePath;
public string SourceFullFileName => _SourceFullFileName;
public string SourceRelativePath => _SourceRelativePath;
public long? SourceSize => _SourceSize;
[JsonConstructor]
public FaceFileSystem(string className, string confidence, string dataDisplayFileName, string dataFullFileName, string display, int? faceBottom, string faceFullFileName, int? faceHeight, int? faceLeft, string faceRelativePath, int? faceRight, int? faceTop, int? faceWidth, int imageHeight, int imageWidth, DateTime lastModified, string locationDisplayIndex, int? locationIndex, bool? populated, string relativePath, string sourceFullFileName, string sourceRelativePath, long? sourceSize) : base(className, dataDisplayFileName, dataFullFileName, display, lastModified)
{
_Confidence = confidence;
_FaceBottom = faceBottom;
_FaceFullFileName = faceFullFileName;
_FaceHeight = faceHeight;
_FaceLeft = faceLeft;
_FaceRelativePath = faceRelativePath;
_FaceRight = faceRight;
_FaceTop = faceTop;
_FaceWidth = faceWidth;
_ImageHeight = imageHeight;
_ImageWidth = imageWidth;
_LocationDisplayIndex = locationDisplayIndex;
_LocationIndex = locationIndex;
_Populated = populated;
_RelativePath = relativePath;
_SourceFullFileName = sourceFullFileName;
_SourceRelativePath = sourceRelativePath;
_SourceSize = sourceSize;
}
private static void FileExists(Face face, int? faceBottom, int? faceRight, out int imageHeight, out int imageWidth, out long sourceSize, FileInfo fileInfo)
{
sourceSize = fileInfo.Length;
if (face.OutputResolution is not null)
{
imageWidth = face.OutputResolution.Width;
imageHeight = face.OutputResolution.Height;
}
else
{
System.Drawing.Size size = Stateless.Methods.ImageHelper.GetDimensions(fileInfo.FullName, faceRight, faceBottom);
imageWidth = size.Width;
imageHeight = size.Height;
}
}
private static void FileDoesNotExist(string dataFullFileName, Face face, out int imageHeight, out int imageWidth, out long sourceSize, FileInfo fileInfo)
{
sourceSize = 0;
if (face.OutputResolution is null)
{
imageWidth = 0;
imageHeight = 0;
}
else
{
imageWidth = face.OutputResolution.Width;
imageHeight = face.OutputResolution.Height;
}
Stateless.Methods.IFaceFileSystem.SearchForMissingFile(fileInfo.FullName, fileInfo, dataFullFileName);
}
internal FaceFileSystem(string requestPath, int rootResultsDirectoryAbsoluteUriLength, string cResizeContent, string dFacesContentDirectory, string dataFullFileName, Face face) : base(string.Empty, string.Empty, dataFullFileName, string.Empty, DateTime.MinValue)
{
string confidence;
int? faceBottom;
int? faceHeight;
int? faceLeft;
int? faceRight;
int? faceTop;
int? faceWidth;
int imageHeight;
int imageWidth;
string locationDisplayIndex;
int locationIndex;
long sourceSize;
if (face.LocationIndex is null)
{
locationIndex = 0;
locationDisplayIndex = "01";
}
else
{
locationIndex = face.LocationIndex.Value;
locationDisplayIndex = (face.LocationIndex.Value + 1).ToString("00");
}
if (face.Location is null)
{
faceTop = null;
faceLeft = null;
faceRight = null;
faceWidth = null;
faceBottom = null;
faceHeight = null;
confidence = "0";
}
else
{
faceTop = face.Location.Top;
faceLeft = face.Location.Left;
faceRight = face.Location.Right;
faceBottom = face.Location.Bottom;
faceWidth = face.Location.Right - face.Location.Left;
faceHeight = face.Location.Bottom - face.Location.Top;
confidence = face.Location.Confidence.ToString("00.000");
}
string? directoryName = Path.GetDirectoryName(face.RelativePath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(face.RelativePath);
string sourceFullFileName = string.Concat(cResizeContent, face.RelativePath);
if (directoryName is null)
throw new Exception();
string faceFullFileName = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, fileNameWithoutExtension, $"{locationIndex} - {fileNameWithoutExtension}.png"));
string faceRelativePath = string.Concat(requestPath, new Uri(faceFullFileName).AbsoluteUri[rootResultsDirectoryAbsoluteUriLength..]);
string sourceRelativePath = string.Concat(requestPath, new Uri(sourceFullFileName).AbsoluteUri[rootResultsDirectoryAbsoluteUriLength..]);
FileInfo fileInfo = new(sourceFullFileName);
if (face.FaceEncoding is not null && face.Location?.NormalizedPixelPercentage is not null && !File.Exists(faceFullFileName))
Stateless.Methods.IFaceFileSystem.SearchForMissingFile(faceFullFileName, fileInfo: new FileInfo(string.Empty), dataFullFileName);
if (fileInfo.Exists)
FileExists(face, faceBottom, faceRight, out imageHeight, out imageWidth, out sourceSize, fileInfo);
else
FileDoesNotExist(dataFullFileName, face, out imageHeight, out imageWidth, out sourceSize, fileInfo);
_ClassName = "file";
_FaceBottom = faceBottom;
_Confidence = confidence;
_DataDisplayFileName = Path.GetFileName(dataFullFileName);
_DataFullFileName = dataFullFileName;
_Display = string.Concat(locationDisplayIndex, " - ", Path.GetFileName(sourceFullFileName));
_FaceFullFileName = faceFullFileName;
_FaceRelativePath = faceRelativePath;
_FaceHeight = faceHeight;
_ImageHeight = imageHeight;
_LastModified = face.DateTime;
_FaceLeft = faceLeft;
_LocationDisplayIndex = locationDisplayIndex;
_LocationIndex = face.LocationIndex;
_Populated = face.FaceEncoding is not null && face.Location?.NormalizedPixelPercentage is not null;
_RelativePath = string.Empty;
_FaceRight = faceRight;
_SourceFullFileName = sourceFullFileName;
_SourceRelativePath = sourceRelativePath;
_SourceSize = sourceSize;
_FaceTop = faceTop;
_FaceWidth = faceWidth;
_ImageWidth = imageWidth;
}
public override string ToString()
{
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
return result;
}
public string GetSize()
{
string result;
if (_SourceSize is null)
result = string.Empty;
else
{
int order = 0;
double len = _SourceSize.Value;
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
while (len >= 1024 && order < sizes.Length - 1)
{
order++;
len /= 1024;
}
result = string.Format("{0:0.##} {1}", len, sizes[order]);
}
return result;
}
}