This commit is contained in:
2022-05-08 12:28:50 -07:00
commit 4a3e24236f
313 changed files with 22395 additions and 0 deletions

View File

@ -0,0 +1,99 @@
using System.Text.Json;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Face
{
internal static string GetJson(string jsonFileFullName)
{
string result;
FileInfo fileInfo;
string fileSegment = "FileSegment";
result = File.ReadAllText(jsonFileFullName);
if (result.Contains(fileSegment))
{
fileInfo = new FileInfo(jsonFileFullName);
result = result.Replace(fileSegment, nameof(Properties.IIndex.RelativePaths)).Replace("\\\\", "/");
File.WriteAllText(fileInfo.FullName, result);
File.SetLastWriteTime(fileInfo.FullName, fileInfo.LastWriteTime);
}
if (result.Contains("/u0"))
{
fileInfo = new FileInfo(jsonFileFullName);
result = result.Replace("/u0", "\\u0");
File.WriteAllText(fileInfo.FullName, result);
File.SetLastWriteTime(fileInfo.FullName, fileInfo.LastWriteTime);
}
return result;
}
private static JsonElement[] GetJsonElements(string jsonFileFullName)
{
string json = GetJson(jsonFileFullName);
JsonElement[]? jsonElements = JsonSerializer.Deserialize<JsonElement[]>(json);
if (jsonElements is null)
throw new Exception();
return jsonElements;
}
private static List<Models.Face> GetFaces(string jsonFileFullName, int? maximum)
{
List<Models.Face> results = new();
Tuple<Models.Face, string>? tuple;
JsonElement[] jsonElements = GetJsonElements(jsonFileFullName);
foreach (JsonElement jsonElement in jsonElements)
{
tuple = JsonSerializer.Deserialize<Tuple<Models.Face, string>>(jsonElement.ToString());
if (tuple is null || tuple.Item1 is null || string.IsNullOrEmpty(tuple.Item1.RelativePath))
continue;
results.Add(tuple.Item1);
if (maximum.HasValue && results.Count >= maximum)
break;
}
return results;
}
internal static Models.Face GetFace(string jsonFileFullName)
{
Models.Face? result;
List<Models.Face> results = GetFaces(jsonFileFullName, maximum: 1);
if (!results.Any())
throw new Exception();
result = results[0];
return result;
}
internal static Models.Face[] GetFaces(string jsonFileFullName)
{
List<Models.Face> results = GetFaces(jsonFileFullName, maximum: null);
return results.ToArray();
}
internal static double Getα(int x1, int x2, int y1, int y2)
{
double result;
if (y1 == y2)
result = 0;
else
{
int b;
if (x1 > x2)
b = x1 - x2;
else
b = x2 - x1;
int a;
if (y1 > y2)
a = y1 - y2;
else
a = y2 - y1;
double c = Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2));
if (y1 < y2)
result = (180 / Math.PI) * Math.Asin(a / c);
else
result = (180 / Math.PI) * Math.Asin(a / c) * -1;
}
return result;
}
}

View File

@ -0,0 +1,83 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class FaceFileSystem
{ // ...
internal static void SearchForMissingFile(string fullFileName, FileInfo fileInfo, string dataFullFileName)
{
if (fileInfo is null || string.IsNullOrEmpty(fileInfo.FullName))
fileInfo = new FileInfo(fullFileName);
if (Directory.Exists(fileInfo.DirectoryName))
throw new Exception($"File [{fileInfo.Name}] <{fullFileName}> doesn't exist!");
else
{
string? parentDirectoryName = Path.GetDirectoryName(fileInfo.DirectoryName);
if (!Directory.Exists(parentDirectoryName))
throw new Exception($"Parent directory <{parentDirectoryName}> doesn't exist!");
else
{
string[] files = Directory.GetFiles(parentDirectoryName, Path.GetFileName(fullFileName), SearchOption.AllDirectories);
if (!files.Any())
throw new Exception($"File [{fileInfo.Name}] <{fullFileName}> doesn't exist (deep search)!");
else
{
if (string.IsNullOrEmpty(dataFullFileName) && !dataFullFileName.EndsWith(".json"))
throw new Exception($"Maybe file <{Path.GetFileNameWithoutExtension(fullFileName)}> was moved to <{files[0]}>!");
else
{
string json = File.ReadAllText(dataFullFileName);
if (!json.Contains("MesaFab"))
throw new Exception($"Maybe file <{Path.GetFileNameWithoutExtension(fullFileName)}> was moved to <{files[0]}> (unkown)!");
else
{
json = json.Replace("MesaFab", "Mesa Fab");
fileInfo = new FileInfo(dataFullFileName);
File.WriteAllText(fileInfo.FullName, json);
File.SetLastWriteTime(fileInfo.FullName, fileInfo.LastWriteTime);
_ = new FileInfo(fullFileName);
}
}
}
}
}
}
internal static Models.FaceFileSystem[] GetFaceFileSystemCollection(string requestPath, (string RootResultsDirectoryAbsoluteUri, string C_ResizeContentDirectory, string D_FacesContentDirectory, string E_DistanceCollectionDirectory) tuple, string selectedFileFullName)
{
List<Models.FaceFileSystem> results = new();
FileInfo fileInfo;
int? locationIndex;
string jsonFileName;
string? directoryName;
string fileNameWithoutExtension;
Models.FaceFileSystem faceFileSystem;
string eDistanceCollectionfileFullName;
Models.Face[] face = Face.GetFaces(selectedFileFullName);
string extension = Path.GetExtension(selectedFileFullName);
for (int i = 0; i < face.Length; i++)
{
if (face[i] is null)
continue;
locationIndex = face[i].LocationIndex;
if (locationIndex is null)
locationIndex = 0;
else
locationIndex = locationIndex.Value;
directoryName = Path.GetDirectoryName(face[i].RelativePath);
if (directoryName is null)
continue;
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(face[i].RelativePath);
jsonFileName = string.Concat(locationIndex.Value, " - ", fileNameWithoutExtension, extension);
eDistanceCollectionfileFullName = string.Concat(tuple.E_DistanceCollectionDirectory, Path.Combine(directoryName, fileNameWithoutExtension, jsonFileName));
if (i == 0 && extension is ".json" && eDistanceCollectionfileFullName != selectedFileFullName)
throw new Exception();
fileInfo = new(eDistanceCollectionfileFullName);
if (!fileInfo.Exists)
throw new Exception($"File <{eDistanceCollectionfileFullName}> doesn't exist!");
faceFileSystem = new Models.FaceFileSystem(requestPath, tuple.RootResultsDirectoryAbsoluteUri.Length, tuple.C_ResizeContentDirectory, tuple.D_FacesContentDirectory, fileInfo.FullName, face[i]);
results.Add(faceFileSystem);
}
return results.ToArray();
}
}

View File

@ -0,0 +1,50 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class FileSystem
{ // ...
internal static List<Models.FileSystem> GetFileSystemCollection(string requestPath, (string RootResultsDirectoryAbsoluteUri, string C_ResizeContentDirectory, string D_FacesContentDirectory, string E_DistanceCollectionDirectory) tuple, string[] directories, string[] files, bool all)
{
List<Models.FileSystem> results = new();
Models.Face face;
DirectoryInfo directoryInfo;
Models.FileSystem fileSystem;
Models.FaceFileSystem faceFileSystem;
IEnumerator<string> subDirectoryFiles;
List<Models.FaceFileSystem> faceFileSystemCollection = new();
for (int i = 0; i < directories.Length; i++)
{
subDirectoryFiles = Directory.EnumerateFiles(directories[i], "*.json", SearchOption.TopDirectoryOnly).GetEnumerator();
if (!subDirectoryFiles.MoveNext())
{
directoryInfo = new(directories[i]);
fileSystem = new Models.DirectoryFileSystem(directoryInfo);
results.Add(fileSystem);
}
else
{
for (int j = 0; j < int.MaxValue; j++)
{
face = Face.GetFace(subDirectoryFiles.Current);
faceFileSystem = new Models.FaceFileSystem(requestPath, tuple.RootResultsDirectoryAbsoluteUri.Length, tuple.C_ResizeContentDirectory, tuple.D_FacesContentDirectory, subDirectoryFiles.Current, face);
if (!all)
results.Add(faceFileSystem);
else
faceFileSystemCollection.Add(faceFileSystem);
if (!all || !subDirectoryFiles.MoveNext())
break;
}
}
}
for (int i = 0; i < files.Length; i++)
{
face = Face.GetFace(files[i]);
faceFileSystem = new Models.FaceFileSystem(requestPath, tuple.RootResultsDirectoryAbsoluteUri.Length, tuple.C_ResizeContentDirectory, tuple.D_FacesContentDirectory, files[i], face);
results.Add(faceFileSystem);
}
if (all)
results.AddRange(faceFileSystemCollection);
return results;
}
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IBackgroundPage
{
void OnGet(bool? message_clear = null, bool? exceptions_clear = null, bool? set_is_primary_instance = null);
static string GetRouteName() => nameof(IBackgroundPage).Replace("Page", string.Empty)[1..];
}

View File

@ -0,0 +1,6 @@
namespace View_by_Distance.Shared.Models.Methods;
public interface IDirectoryFileSystem
{
}

View File

@ -0,0 +1,16 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IFace
{ // ...
double TestStatic_Getα(int x1, int x2, int y1, int y2);
string TestStatic_GetJson(string jsonFileFullName);
Models.Face TestStatic_GetFace(string jsonFileFullName);
Models.Face[] TestStatic_GetFaces(string jsonFileFullName);
static double Getα(int x1, int x2, int y1, int y2) => Face.Getα(x1, x2, y1, y2);
static string GetJson(string jsonFileFullName) => Face.GetJson(jsonFileFullName);
static Models.Face GetFace(string jsonFileFullName) => Face.GetFace(jsonFileFullName);
static Models.Face[] GetFaces(string jsonFileFullName) => Face.GetFaces(jsonFileFullName);
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IFaceFileSystem : IFileSystem
{ // ...
void TestStatic_SearchForMissingFile(string fullFileName, FileInfo fileInfo, string dataFullFileName);
Models.FaceFileSystem[][] TestStatic_GetFaceFileSystemCollection(string requestPath, string rootResultsDirectory, (string RootResultsDirectoryAbsoluteUri, string C_ResizeContentDirectory, string D_FacesContentDirectory, string E_DistanceCollectionDirectory) tuple, string selectedFileFullName);
static void SearchForMissingFile(string fullFileName, FileInfo fileInfo, string dataFullFileName) => FaceFileSystem.SearchForMissingFile(fullFileName, fileInfo, dataFullFileName);
static Models.FaceFileSystem[] GetFaceFileSystemCollection(string requestPath, (string RootResultsDirectoryAbsoluteUri, string C_ResizeContentDirectory, string D_FacesContentDirectory, string E_DistanceCollectionDirectory) tuple, string selectedFileFullName) => FaceFileSystem.GetFaceFileSystemCollection(requestPath, tuple, selectedFileFullName);
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IFileSystem
{ // ...
List<Models.FileSystem> TestStatic_GetFileSystemCollection(string requestPath, string rootResultsDirectory, (string RootResultsDirectoryAbsoluteUri, string C_ResizeContentDirectory, string D_FacesContentDirectory, string E_DistanceCollectionDirectory) tuple, string[] directories, string[] files, bool all);
static List<Models.FileSystem> GetFileSystemCollection(string requestPath, (string RootResultsDirectoryAbsoluteUri, string C_ResizeContentDirectory, string D_FacesContentDirectory, string E_DistanceCollectionDirectory) tuple, string[] directories, string[] files, bool all) => FileSystem.GetFileSystemCollection(requestPath, tuple, directories, files, all);
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IIndex
{ // ...
string TestStatic_GetJson(string jsonFileFullName, FileInfo fileInfo);
static string GetJson(string jsonFileFullName, FileInfo fileInfo) => Index.GetJson(jsonFileFullName, fileInfo);
}

View File

@ -0,0 +1,8 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IMetadataFile
{
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IMetadataFileCollection
{
Dictionary<string, List<KeyValuePair<string, string>>> TestStatic_GetDefaultValue() => MetadataFileCollection.GetDefaultValue(); // {{1}}SingletonValue
static Dictionary<string, List<KeyValuePair<string, string>>> GetDefaultValue() => MetadataFileCollection.GetDefaultValue(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IMetadataFileId
{
string TestStatic_GetDefaultValue() => MetadataFileId.GetDefaultValue(); // {{1}}SingletonValue
static string GetDefaultValue() => MetadataFileId.GetDefaultValue(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,10 @@
using System.Runtime.CompilerServices;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IMethodName
{
static string? GetActualAsyncMethodName([CallerMemberName] string? name = null) => name;
}

View File

@ -0,0 +1,23 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPerson
{
// ...
Dictionary<DateTime, string[]> TestStatic_Split(string knownPeopleFile);
static Dictionary<DateTime, string[]> Split(string knownPeopleFile) => Person.Split(knownPeopleFile);
Models.Person[] TestStatic_GetPeople(Models.Properties.IStorage storage);
static Models.Person[] GetPeople(Models.Properties.IStorage storage) => Person.GetPeople(storage);
void TestStatic_SavePerson(Models.Properties.IStorage storage, Models.Person person);
static void SavePerson(Models.Properties.IStorage storage, Models.Person person) => Person.SavePerson(storage, person);
string TestStatic_GetFileFullName(Models.Properties.IStorage storage, Models.Person person);
static string GetFileFullName(Models.Properties.IStorage storage, Models.Person person) => PersonBirthday.GetFileFullName(storage, person.Birthday);
Models.Person TestStatic_CreatePerson(Models.Properties.IStorage storage, Models.PersonBirthday birthday, Models.PersonName name, List<Models.PersonComment> comments, List<Models.PersonURL> urls, List<Models.PersonNumber> numbers, List<Models.PersonEmail> emails, List<Models.PersonAddress> addresses);
static Models.Person CreatePerson(Models.Properties.IStorage storage, Models.PersonBirthday birthday, Models.PersonName name, List<Models.PersonComment> comments, List<Models.PersonURL> urls, List<Models.PersonNumber> numbers, List<Models.PersonEmail> emails, List<Models.PersonAddress> addresses) => Person.CreatePerson(storage, birthday, name, comments, urls, numbers, emails, addresses);
}

View File

@ -0,0 +1,8 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonAddress
{
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonAddressCity
{
string TestStatic_GetDefaultValue() => PersonAddressCity.GetDefaultValue(); // {{1}}SingletonValue
static string GetDefaultValue() => PersonAddressCity.GetDefaultValue(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonAddressState
{
string TestStatic_GetDefaultValue() => PersonAddressState.GetDefaultValue(); // {{1}}SingletonValue
static string GetDefaultValue() => PersonAddressState.GetDefaultValue(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonAddressStreet
{
string TestStatic_GetDefaultValue() => PersonAddressStreet.GetDefaultValue(); // {{1}}SingletonValue
static string GetDefaultValue() => PersonAddressStreet.GetDefaultValue(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonAddressZipCode
{
string TestStatic_GetDefaultValue() => PersonAddressZipCode.GetDefaultValue(); // {{1}}SingletonValue
static string GetDefaultValue() => PersonAddressZipCode.GetDefaultValue(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,30 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonBirthday
{
DateTime TestStatic_GetDefaultValue() => PersonBirthday.GetDefaultValue(); // {{1}}SingletonValue
static DateTime GetDefaultValue() => PersonBirthday.GetDefaultValue(); // {{1}}SingletonValue
// ...
string TestStatic_GetFormat() => PersonBirthday.GetFormat();
static string GetFormat() => PersonBirthday.GetFormat();
Models.PersonBirthday TestStatic_GetNextBirthdate(Models.Properties.IStorage storage) => PersonBirthday.GetNextBirthdate(storage);
static Models.PersonBirthday GetNextBirthdate(Models.Properties.IStorage storage) => PersonBirthday.GetNextBirthdate(storage);
string TestStatic_GetFormated(Models.PersonBirthday personBirthday) => PersonBirthday.GetFormated(personBirthday);
static string GetFormated(Models.PersonBirthday personBirthday) => PersonBirthday.GetFormated(personBirthday);
string TestStatic_GetFileName(Models.PersonBirthday personBirthday) => PersonBirthday.GetFileName(personBirthday);
static string GetFileName(Models.PersonBirthday personBirthday) => PersonBirthday.GetFileName(personBirthday);
bool TestStatic_DoesBirthDateExits(Models.Properties.IStorage storage, Models.PersonBirthday personBirthday) => DoesBirthDateExits(storage, personBirthday);
static bool DoesBirthDateExits(Models.Properties.IStorage storage, Models.PersonBirthday personBirthday) => DoesBirthDateExits(storage, personBirthday);
string TestStatic_GetFileFullName(Models.Properties.IStorage storage, Models.PersonBirthday personBirthday) => PersonBirthday.GetFileFullName(storage, personBirthday);
static string GetFileFullName(Models.Properties.IStorage storage, Models.PersonBirthday personBirthday) => PersonBirthday.GetFileFullName(storage, personBirthday);
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonComment
{
string TestStatic_GetDefaultValue() => PersonComment.GetDefaultValue(); // <{1}>PluralValue
static string GetDefaultValue() => PersonComment.GetDefaultValue(); // <{1}>PluralValue
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonEmail
{
string TestStatic_GetDefaultValue() => PersonEmail.GetDefaultValue(); // <{1}>PluralValue
static string GetDefaultValue() => PersonEmail.GetDefaultValue(); // <{1}>PluralValue
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonId
{
long TestStatic_GetDefaultValue() => PersonId.GetDefaultValue(); // {{1}}SingletonValue
static long GetDefaultValue() => PersonId.GetDefaultValue(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,8 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonName
{
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonNameAlias
{
string TestStatic_GetDefaultValue() => PersonNameAlias.GetDefaultValue(); // {{1}}SingletonValue
static string GetDefaultValue() => PersonNameAlias.GetDefaultValue(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonNameFirst
{
string TestStatic_GetDefaultValue() => PersonNameFirst.GetDefaultValue(); // {{1}}SingletonValue
static string GetDefaultValue() => PersonNameFirst.GetDefaultValue(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonNameLast
{
string TestStatic_GetDefaultValue() => PersonNameLast.GetDefaultValue(); // {{1}}SingletonValue
static string GetDefaultValue() => PersonNameLast.GetDefaultValue(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonNameMiddle
{
string TestStatic_GetDefaultValue() => PersonNameMiddle.GetDefaultValue(); // {{1}}SingletonValue
static string GetDefaultValue() => PersonNameMiddle.GetDefaultValue(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonNumber
{
string TestStatic_GetDefaultValue() => PersonNumber.GetDefaultValue(); // <{1}>PluralValue
static string GetDefaultValue() => PersonNumber.GetDefaultValue(); // <{1}>PluralValue
// ...
}

View File

@ -0,0 +1,12 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonURL
{
string TestStatic_GetDefaultValue() => PersonURL.GetDefaultValue(); // <{1}>PluralValue
static string GetDefaultValue() => PersonURL.GetDefaultValue(); // <{1}>PluralValue
// ...
}

View File

@ -0,0 +1,14 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IStorage
{
// ...
bool TestStatic_WriteAllText(string path, string contents, bool compareBeforeWrite);
static bool WriteAllText(string path, string contents, bool compareBeforeWrite) => Storage.WriteAllText(path, contents, compareBeforeWrite);
(string RootResultsDirectoryAbsoluteUri, string C_ResizeContentDirectory, string D_FacesContentDirectory, string E_DistanceCollectionDirectory) TestStatic_GetTuple(Models.Properties.IStorage storage);
static (string RootResultsDirectoryAbsoluteUri, string C_ResizeContentDirectory, string D_FacesContentDirectory, string E_DistanceCollectionDirectory) GetTuple(Models.Properties.IStorage storage) => new(new Uri(storage.RootResultsDirectory).AbsoluteUri, Path.Combine(storage.ResizeRootDirectory, "()"), Path.Combine(storage.FaceRootDirectory, "()"), Path.Combine(storage.DistanceResultRootDirectory, "[]"));
}

View File

@ -0,0 +1,8 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IWorkingDirectory
{
static string GetWorkingDirectory(string? executingAssemblyName, string subDirectoryName) => WorkingDirectory.GetWorkingDirectory(executingAssemblyName, subDirectoryName);
}

View File

@ -0,0 +1,130 @@
using System.Drawing;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class ImageHelper
{
private static bool StartsWith(byte[] thisBytes, byte[] thatBytes)
{
for (int i = 0; i < thatBytes.Length; i += 1)
{
if (thisBytes[i] != thatBytes[i])
return false;
}
return true;
}
private static short ReadLittleEndianInt16(BinaryReader binaryReader)
{
byte[] bytes = new byte[sizeof(short)];
for (int i = 0; i < sizeof(short); i += 1)
bytes[sizeof(short) - 1 - i] = binaryReader.ReadByte();
return BitConverter.ToInt16(bytes, 0);
}
private static int ReadLittleEndianInt32(BinaryReader binaryReader)
{
byte[] bytes = new byte[sizeof(int)];
for (int i = 0; i < sizeof(int); i += 1)
bytes[sizeof(int) - 1 - i] = binaryReader.ReadByte();
return BitConverter.ToInt32(bytes, 0);
}
private static Size DecodeBitmap(BinaryReader binaryReader)
{
_ = binaryReader.ReadBytes(16);
int width = binaryReader.ReadInt32();
int height = binaryReader.ReadInt32();
return new Size(width, height);
}
private static Size DecodeGif(BinaryReader binaryReader)
{
int width = binaryReader.ReadInt16();
int height = binaryReader.ReadInt16();
return new Size(width, height);
}
private static Size DecodePng(BinaryReader binaryReader)
{
_ = binaryReader.ReadBytes(8);
int width = ReadLittleEndianInt32(binaryReader);
int height = ReadLittleEndianInt32(binaryReader);
return new Size(width, height);
}
private static Size DecodeJfif(BinaryReader binaryReader)
{
while (binaryReader.ReadByte() == 0xff)
{
byte marker = binaryReader.ReadByte();
short chunkLength = ReadLittleEndianInt16(binaryReader);
if (marker == 0xc0)
{
_ = binaryReader.ReadByte();
int height = ReadLittleEndianInt16(binaryReader);
int width = ReadLittleEndianInt16(binaryReader);
return new Size(width, height);
}
_ = binaryReader.ReadBytes(chunkLength - 2);
}
throw new ArgumentException("Could not recognize image format.");
}
/// <summary>
/// Gets the dimensions of an image.
/// </summary>
/// <param name="path">The path of the image to get the dimensions of.</param>
/// <returns>The dimensions of the specified image.</returns>
/// <exception cref="ArgumentException">The image was of an unrecognized format.</exception>
public static Size GetDimensions(BinaryReader binaryReader, int? faceRight, int? faceBottom)
{
Size? result = null;
Dictionary<byte[], Func<BinaryReader, Size>> _ImageFormatDecoders = new()
{
{ new byte[] { 0x42, 0x4D }, DecodeBitmap },
{ new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif },
{ new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif },
{ new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng },
{ new byte[] { 0xff, 0xd8 }, DecodeJfif },
};
int maxMagicBytesLength = _ImageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;
byte[] magicBytes = new byte[maxMagicBytesLength];
for (int i = 0; i < maxMagicBytesLength; i += 1)
{
magicBytes[i] = binaryReader.ReadByte();
foreach (KeyValuePair<byte[], Func<BinaryReader, Size>> kvPair in _ImageFormatDecoders)
{
if (!StartsWith(magicBytes, kvPair.Key))
continue;
result = kvPair.Value(binaryReader);
break;
}
if (result is not null)
break;
}
if (result is null)
{
if (faceRight is null || faceBottom is null)
throw new Exception("face is null!");
result = new(faceRight.Value, faceBottom.Value);
}
return result.Value;
}
/// <summary>
/// Gets the dimensions of an image.
/// </summary>
/// <param name="path">The path of the image to get the dimensions of.</param>
/// <returns>The dimensions of the specified image.</returns>
/// <exception cref="ArgumentException">The image was of an unrecognized format.</exception>
public static Size GetDimensions(string path, int? faceRight, int? faceBottom)
{
Size result;
using BinaryReader binaryReader = new(File.OpenRead(path));
result = GetDimensions(binaryReader, faceRight, faceBottom);
return result;
}
}

View File

@ -0,0 +1,31 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Index
{
internal static string GetJson(string jsonFileFullName, FileInfo fileInfo)
{
string result;
string fileSegment = "FileSegment";
string fileSegmentCollection = "FileSegmentCollection";
result = File.ReadAllText(jsonFileFullName).Replace("Goolgle", "Google");
if (result.Contains(fileSegment) || result.Contains(fileSegmentCollection))
{
if (fileInfo is null)
fileInfo = new FileInfo(jsonFileFullName);
result = result.Replace(fileSegmentCollection, nameof(Properties.IIndex.RelativePaths)).Replace("\\\\", "/");
File.WriteAllText(fileInfo.FullName, result);
File.SetLastWriteTime(fileInfo.FullName, fileInfo.LastWriteTime);
}
if (result.Contains("/u0"))
{
if (fileInfo is null)
fileInfo = new FileInfo(jsonFileFullName);
result = result.Replace("/u0", "\\u0");
File.WriteAllText(fileInfo.FullName, result);
File.SetLastWriteTime(fileInfo.FullName, fileInfo.LastWriteTime);
}
return result;
}
}

View File

@ -0,0 +1,8 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class MetadataFile
{
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class MetadataFileCollection
{
internal static Dictionary<string, List<KeyValuePair<string, string>>> GetDefaultValue() => new(); // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class MetadataFileId
{
internal static string GetDefaultValue() => string.Empty; // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,229 @@
using System.Text.Json;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Person
{
// ...
private static List<string> ValidatePerson(Models.Properties.IStorage storage, Models.PersonId id, Models.PersonBirthday birthday, Models.PersonName name)
{
List<string> results = new();
if (birthday is null)
throw new Exception("Birthday must be supplied!");
if (birthday.Value > DateTime.Now)
results.Add("Birthday must be in the past!");
if (id is null)
throw new Exception("Birthday must be supplied!");
if (id.Value != birthday.Value.Ticks)
results.Add("Id must be Birthday ticks!");
if (name.First is null || string.IsNullOrEmpty(name.First.Value))
results.Add("Fist Name must be supplied!");
if (PersonBirthday.DoesBirthDateExits(storage, birthday))
results.Add("Birthdate already exits!");
return results;
}
internal static Models.Person CreatePerson(Models.Properties.IStorage storage, Models.PersonBirthday birthday, Models.PersonName name, List<Models.PersonComment> comments, List<Models.PersonURL> urls, List<Models.PersonNumber> numbers, List<Models.PersonEmail> emails, List<Models.PersonAddress> addresses)
{
Models.Person result;
Models.PersonId id = new(birthday.Value.Ticks);
if (birthday.Value == DateTime.MinValue)
birthday = PersonBirthday.GetNextBirthdate(storage);
List<string> results = ValidatePerson(storage, id, birthday, name);
if (results.Any())
throw new Exception(string.Join(Environment.NewLine, results));
if (comments is null)
comments = new();
if (urls is null)
urls = new();
if (numbers is null)
numbers = new();
if (emails is null)
emails = new();
if (addresses is null)
addresses = new();
result = new(id, birthday, name, comments, urls, numbers, emails, addresses);
return result;
}
internal static Dictionary<DateTime, string[]> Split(string knownPeopleFile)
{
Dictionary<DateTime, string[]> results = new();
string[] segments;
DateTime personKey;
List<string> temporarySegments;
const string KeyFormat = "yyyy-MM-dd_HH";
DateTime incrementDate = new(1500, 1, 1);
string[] lines = File.ReadAllLines(knownPeopleFile);
_ = incrementDate.AddDays(lines.Length);
System.Globalization.CultureInfo cultureInfo = System.Globalization.CultureInfo.InvariantCulture;
foreach (string line in lines)
{
if (string.IsNullOrEmpty(line))
continue;
segments = line.Replace(" //", "\t//").Split('\t');
if (segments.Length < 1)
continue;
if (segments[0].Length != KeyFormat.Length || !segments[0].Contains('-') || !segments[0].Contains('_'))
{
temporarySegments = segments.ToList();
temporarySegments.Insert(0, incrementDate.ToString(KeyFormat));
segments = temporarySegments.ToArray();
incrementDate = incrementDate.AddDays(1);
}
personKey = DateTime.ParseExact(segments[0], KeyFormat, cultureInfo);
if (results.ContainsKey(personKey))
continue;
results.Add(personKey, segments);
}
int countBefore = results.Count;
DateTime minimumDateTime = results.Keys.Min();
for (int i = 1; i < (1000 - countBefore); i++)
{
personKey = minimumDateTime.AddDays(i * -1);
results.Add(personKey, new string[] { personKey.ToString(KeyFormat) });
}
return results.OrderBy(l => l.Key).ToDictionary(l => l.Key, l => l.Value);
}
internal static Dictionary<DateTime, PersonImport> GetPersonCollection(string knownPeopleFile)
{
Dictionary<DateTime, PersonImport> results = new();
string name;
DateTime key;
string comment;
string oldName;
string mergeName;
PersonImport person;
Dictionary<DateTime, string[]> splitLines = Split(knownPeopleFile);
foreach (KeyValuePair<DateTime, string[]> splitLine in splitLines)
{
name = string.Empty;
key = splitLine.Key;
comment = string.Empty;
oldName = string.Empty;
mergeName = string.Empty;
if (splitLine.Value.Length > 1)
{
foreach (string segment in splitLine.Value)
{
if (!segment.Contains('*'))
continue;
mergeName = segment.Split('*')[1].Split('\t')[0];
}
if (splitLine.Value[1].StartsWith("//"))
comment = splitLine.Value[1];
else
name = splitLine.Value[1].Split('\t')[0];
if (splitLine.Value.Length > 2)
comment = splitLine.Value[2];
}
person = new(key, name, mergeName, oldName, comment);
results.Add(splitLine.Key, person);
}
return results;
}
internal static void SavePerson(Models.Properties.IStorage storage, Models.Person person)
{
string fileName = IPerson.GetFileFullName(storage, person);
string json = JsonSerializer.Serialize(person, new JsonSerializerOptions { WriteIndented = true });
_ = IStorage.WriteAllText(fileName, json, compareBeforeWrite: true);
}
private static List<Models.Person> GetPeopleFromText(Models.Properties.IStorage storage, string localKnownPeopleFile)
{
List<Models.Person> results = new();
string comment;
Models.Person person;
Models.PersonName name;
List<Models.PersonURL> urls;
Models.PersonBirthday birthday;
List<Models.PersonEmail> emails = new();
List<Models.PersonNumber> numbers = new();
List<Models.PersonComment> comments = new();
List<Models.PersonAddress> addresses = new();
Dictionary<DateTime, PersonImport> keyValuePairs = GetPersonCollection(localKnownPeopleFile);
foreach (KeyValuePair<DateTime, PersonImport> keyValuePair in keyValuePairs)
{
if (string.IsNullOrEmpty(keyValuePair.Value.Name))
continue;
urls = new();
birthday = new(keyValuePair.Key);
name = PersonName.Create(keyValuePair.Value.Name);
if (name.First is null || string.IsNullOrEmpty(name.First.Value))
continue;
if (!string.IsNullOrEmpty(keyValuePair.Value.Comment))
{
comment = keyValuePair.Value.Comment[2..];
if (!string.IsNullOrEmpty(comment))
{
if (comment.StartsWith("http://") || comment.StartsWith("https://"))
urls.Add(new(new(comment)));
else
comments.Add(new(new(comment)));
}
}
if (!string.IsNullOrEmpty(keyValuePair.Value.OldName))
comments.Add(new(new(keyValuePair.Value.OldName)));
person = IPerson.CreatePerson(storage, birthday, name, comments, urls, numbers, emails, addresses);
SavePerson(storage, person);
results.Add(person);
}
return results;
}
internal static Models.Person[] GetPeople(Models.Properties.IStorage storage)
{
List<Models.Person> results = new();
string json;
string[] files;
FileInfo fileInfo;
Models.Person? person;
string localKnownPeopleFile;
DateTime dateTime = DateTime.MinValue;
string directory = Path.Combine(storage.PeopleRootDirectory, "{}");
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
if (!Directory.Exists(storage.RootDirectory))
localKnownPeopleFile = string.Empty;
else
{
files = Directory.GetFiles(storage.RootDirectory, "*People*.txt", SearchOption.TopDirectoryOnly);
if (files.Any())
localKnownPeopleFile = files[0];
else
localKnownPeopleFile = string.Empty;
}
files = Directory.GetFiles(directory, "*.json", SearchOption.TopDirectoryOnly);
if (!files.Any() && string.IsNullOrEmpty(localKnownPeopleFile))
throw new Exception("Copy \"KnownPeople.txt\" file from server!");
foreach (string file in files)
{
fileInfo = new(file);
if (dateTime < fileInfo.LastWriteTime)
dateTime = fileInfo.LastWriteTime;
json = File.ReadAllText(file);
person = JsonSerializer.Deserialize<Models.Person>(json);
if (person is null)
continue;
results.Add(person);
}
if (!results.Any())
results = GetPeopleFromText(storage, localKnownPeopleFile);
else if (!string.IsNullOrEmpty(localKnownPeopleFile))
{
fileInfo = new FileInfo(localKnownPeopleFile);
if (fileInfo.LastWriteTime > dateTime)
{
foreach (string file in files)
File.Delete(file);
results = GetPeopleFromText(storage, localKnownPeopleFile);
}
}
return results.ToArray();
}
}

View File

@ -0,0 +1,8 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonAddress
{
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonAddressCity
{
internal static string GetDefaultValue() => string.Empty; // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonAddressState
{
internal static string GetDefaultValue() => string.Empty; // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonAddressStreet
{
internal static string GetDefaultValue() => string.Empty; // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonAddressZipCode
{
internal static string GetDefaultValue() => string.Empty; // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,16 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonBirthday
{
internal static DateTime GetDefaultValue() => DateTime.MinValue; // {{1}}SingletonValue
// ...
internal static string GetFormat() => "yyyy-MM-dd_HH";
internal static Models.PersonBirthday GetNextBirthdate(Models.Properties.IStorage storage) => throw new Exception(storage.ToString()); // Person.GetNextBirthdate(storage);
internal static string GetFormated(Models.PersonBirthday personBirthday) => personBirthday.Value.ToString(GetFormat());
internal static string GetFileName(Models.PersonBirthday personBirthday) => $"{personBirthday.Value.ToString(GetFormat())}.json";
internal static bool DoesBirthDateExits(Models.Properties.IStorage storage, Models.PersonBirthday personBirthday) => File.Exists(GetFileFullName(storage, personBirthday));
internal static string GetFileFullName(Models.Properties.IStorage storage, Models.PersonBirthday personBirthday) => Path.Combine(storage.PeopleRootDirectory, "{}", GetFileName(personBirthday));
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonComment
{
internal static string GetDefaultValue() => string.Empty; // <{1}>PluralValue
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonEmail
{
internal static string GetDefaultValue() => string.Empty; // <{1}>PluralValue
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonId
{
internal static long GetDefaultValue() => long.MinValue; // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,60 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonName
{
// ...
internal static Models.PersonName Create(string name)
{
Models.PersonName result;
Models.PersonNameLast personNameLast;
Models.PersonNameAlias personNameAlias;
Models.PersonNameFirst personNameFirst;
Models.PersonNameMiddle personNameMiddle;
string[] segments = name.Split(' ');
if (segments.Length == 1)
{
personNameFirst = new(string.Empty);
personNameLast = new(string.Empty);
personNameMiddle = new(string.Empty);
personNameAlias = new(segments[0]);
}
else if (segments.Length == 2)
{
personNameFirst = new(segments[0]);
personNameLast = new(segments[1]);
personNameMiddle = new(string.Empty);
personNameAlias = new(string.Empty);
}
else if (segments.Length == 3 && segments[2] is "Jr." or "Jr" or "Sr")
{
personNameFirst = new(segments[0]);
personNameLast = new(segments[1]);
personNameMiddle = new(string.Empty);
personNameAlias = new(string.Join(' ', segments));
}
else
{
string[] comment = name.Split(new string[] { " (" }, StringSplitOptions.None);
if (comment.Length == 1)
{
personNameFirst = new(segments[0]);
personNameLast = new(segments[^1]);
personNameMiddle = new(string.Empty);
personNameAlias = new(string.Join(' ', segments));
}
else
{
segments = comment[0].Split(' ');
personNameFirst = new(segments[0]);
personNameLast = new(segments[^1]);
personNameMiddle = new(string.Empty);
personNameAlias = new(string.Concat(string.Join(' ', segments), " (", comment[1]));
}
}
result = new(personNameFirst, personNameMiddle, personNameLast, personNameAlias);
return result;
}
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonNameAlias
{
internal static string GetDefaultValue() => string.Empty; // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonNameFirst
{
internal static string GetDefaultValue() => string.Empty; // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonNameLast
{
internal static string GetDefaultValue() => string.Empty; // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonNameMiddle
{
internal static string GetDefaultValue() => string.Empty; // {{1}}SingletonValue
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonNumber
{
internal static string GetDefaultValue() => string.Empty; // <{1}>PluralValue
// ...
}

View File

@ -0,0 +1,10 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class PersonURL
{
internal static string GetDefaultValue() => string.Empty; // <{1}>PluralValue
// ...
}

View File

@ -0,0 +1,27 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Storage
{
// ...
internal static bool WriteAllText(string path, string contents, bool compareBeforeWrite)
{
bool result;
string text;
if (!compareBeforeWrite)
result = true;
else
{
if (!File.Exists(path))
text = string.Empty;
else
text = File.ReadAllText(path);
result = text != contents;
}
if (result)
File.WriteAllText(path, contents);
return result;
}
}

View File

@ -0,0 +1,50 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class WorkingDirectory
{
internal static string GetWorkingDirectory(string? executingAssemblyName, string subDirectoryName)
{
string result = string.Empty;
if (executingAssemblyName is null)
throw new Exception();
string traceFile;
List<string> directories = new();
Environment.SpecialFolder[] specialFolders = new Environment.SpecialFolder[]
{
Environment.SpecialFolder.LocalApplicationData,
Environment.SpecialFolder.ApplicationData,
Environment.SpecialFolder.History,
Environment.SpecialFolder.CommonApplicationData,
Environment.SpecialFolder.InternetCache
};
foreach (Environment.SpecialFolder specialFolder in specialFolders)
directories.Add(Path.Combine(Environment.GetFolderPath(specialFolder), subDirectoryName, executingAssemblyName));
foreach (string directory in directories)
{
for (int i = 1; i < 3; i++)
{
if (i == 1)
result = directory;
else
result = string.Concat("D", directory[1..]);
try
{
if (!Directory.Exists(result))
_ = Directory.CreateDirectory(result);
traceFile = string.Concat(result, @"\", DateTime.Now.Ticks, ".txt");
File.WriteAllText(traceFile, traceFile);
File.Delete(traceFile);
break;
}
catch (Exception) { result = string.Empty; }
}
if (!string.IsNullOrEmpty(result))
break;
}
if (string.IsNullOrEmpty(result))
throw new Exception("Unable to set working directory!");
return result;
}
}