Facebook logic, IsDefaultName, GetFiles update and
HardCoded tests
This commit is contained in:
@ -28,8 +28,8 @@ public class PersonContainer : Properties.IPersonContainer
|
||||
FilteredIndividualsLines = filteredIndividualsLines;
|
||||
}
|
||||
|
||||
public PersonContainer(char[] personCharacters, PersonBirthday birthday, string displayDirectoryName) :
|
||||
this(Stateless.Methods.IAge.GetApproximateYears(personCharacters, displayDirectoryName), null, Stateless.Methods.IPerson.GetPerson(personCharacters, displayDirectoryName, birthday.Value.Ticks, birthday), new PersonBirthday[] { birthday }, Array.Empty<string>(), displayDirectoryName, birthday.Value.Ticks, null)
|
||||
public PersonContainer(string mappingDefaultName, char[] personCharacters, PersonBirthday birthday, string displayDirectoryName) :
|
||||
this(Stateless.Methods.IAge.GetApproximateYears(personCharacters, displayDirectoryName), null, Stateless.Methods.IPerson.GetPerson(mappingDefaultName, personCharacters, displayDirectoryName, birthday.Value.Ticks, birthday), new PersonBirthday[] { birthday }, Array.Empty<string>(), displayDirectoryName, birthday.Value.Ticks, null)
|
||||
{ }
|
||||
|
||||
public PersonContainer(int? approximateYears, PersonBirthday birthdays, string displayDirectoryName, long key) :
|
||||
|
@ -5,24 +5,34 @@ public interface IPerson
|
||||
|
||||
// ...
|
||||
|
||||
bool TestStatic_IsDefaultName(string mappingDefaultName, string value) =>
|
||||
IsDefaultName(mappingDefaultName, value);
|
||||
static bool IsDefaultName(string mappingDefaultName, string value) =>
|
||||
value == mappingDefaultName || (value.Length > 1 && value[0] == 'Z' && value[1] == ']');
|
||||
|
||||
bool TestStatic_IsDefaultName(string mappingDefaultName, Models.Person person) =>
|
||||
IsDefaultName(mappingDefaultName, person);
|
||||
static bool IsDefaultName(string mappingDefaultName, Models.Person person) =>
|
||||
IsDefaultName(mappingDefaultName, person.Name.Alias is null ? string.Empty : person.Name.Alias.Value);
|
||||
|
||||
string TestStatic_GetFileFullName(Properties.IStorage storage, string personBirthdayFormat, Models.Person person) =>
|
||||
GetFileFullName(storage, personBirthdayFormat, person);
|
||||
static string GetFileFullName(Properties.IStorage storage, string personBirthdayFormat, Models.Person person) =>
|
||||
IPersonBirthday.GetFileFullName(storage, personBirthdayFormat, person.Birthday);
|
||||
|
||||
Models.Person TestStatic_GetPerson(char[] personCharacters, string personDisplayDirectoryName, long personKey, Models.PersonBirthday personBirthday) =>
|
||||
GetPerson(personCharacters, personDisplayDirectoryName, personKey, personBirthday);
|
||||
static Models.Person GetPerson(char[] personCharacters, string personDisplayDirectoryName, long personKey, Models.PersonBirthday personBirthday) =>
|
||||
Person.GetPerson(Array.Empty<string>(), null, personKey, personBirthday, personDisplayDirectoryName.Split(personCharacters), null);
|
||||
Models.Person TestStatic_GetPerson(string mappingDefaultName, char[] personCharacters, string personDisplayDirectoryName, long personKey, Models.PersonBirthday personBirthday) =>
|
||||
GetPerson(mappingDefaultName, personCharacters, personDisplayDirectoryName, personKey, personBirthday);
|
||||
static Models.Person GetPerson(string mappingDefaultName, char[] personCharacters, string personDisplayDirectoryName, long personKey, Models.PersonBirthday personBirthday) =>
|
||||
Person.GetPerson(mappingDefaultName, personCharacters, personDisplayDirectoryName, Array.Empty<string>(), null, personKey, personBirthday, null);
|
||||
|
||||
Models.Person TestStatic_GetPerson(string[] personDisplayDirectoryAllFiles, string personKeyFormatted, long personKey, string[] segments, string[]? filteredIndividualsLines) =>
|
||||
GetPerson(personDisplayDirectoryAllFiles, personKeyFormatted, personKey, segments, filteredIndividualsLines);
|
||||
static Models.Person GetPerson(string[] personDisplayDirectoryAllFiles, string personKeyFormatted, long personKey, string[] segments, string[]? filteredIndividualsLines) =>
|
||||
Person.GetPerson(personDisplayDirectoryAllFiles, personKeyFormatted, personKey, IPersonBirthday.GetPersonBirthday(personKey), segments, filteredIndividualsLines);
|
||||
Models.Person TestStatic_GetPerson(string mappingDefaultName, char[] personCharacters, string personDisplayDirectoryName, string[] personDisplayDirectoryAllFiles, string personKeyFormatted, long personKey, string[]? filteredIndividualsLines) =>
|
||||
GetPerson(mappingDefaultName, personCharacters, personDisplayDirectoryName, personDisplayDirectoryAllFiles, personKeyFormatted, personKey, filteredIndividualsLines);
|
||||
static Models.Person GetPerson(string mappingDefaultName, char[] personCharacters, string personDisplayDirectoryName, string[] personDisplayDirectoryAllFiles, string personKeyFormatted, long personKey, string[]? filteredIndividualsLines) =>
|
||||
Person.GetPerson(mappingDefaultName, personCharacters, personDisplayDirectoryName, personDisplayDirectoryAllFiles, personKeyFormatted, personKey, IPersonBirthday.GetPersonBirthday(personKey), filteredIndividualsLines);
|
||||
|
||||
(string[] headerLines, Dictionary<string, List<string>> individuals, string[] footerLines) TestStatic_GetIndividuals(string gedCOMFile) =>
|
||||
(string[] headerLines, Dictionary<string, List<string>> individuals, string[] footerLines) TestStatic_GetIndividuals(string? gedCOMFile) =>
|
||||
GetIndividuals(gedCOMFile);
|
||||
static (string[] headerLines, Dictionary<string, List<string>> individuals, string[] footerLines) GetIndividuals(string gedCOMFile) =>
|
||||
static (string[] headerLines, Dictionary<string, List<string>> individuals, string[] footerLines) GetIndividuals(string? gedCOMFile) =>
|
||||
Person.GetIndividuals(gedCOMFile);
|
||||
|
||||
string[] TestStatic_GetFiltered(List<string> individualsLines) =>
|
||||
|
@ -28,6 +28,11 @@ public interface IPersonBirthday
|
||||
static Models.PersonBirthday GetPersonBirthday(long ticks) =>
|
||||
new(new(ticks));
|
||||
|
||||
DateTime? TestStatic_GetDate(string month, string day, string year) =>
|
||||
GetDate(month, day, year);
|
||||
static DateTime? GetDate(string month, string day, string year) =>
|
||||
PersonBirthday.GetDate(month, day, year);
|
||||
|
||||
string TestStatic_GetFileName(string personBirthdayFormat, Models.PersonBirthday personBirthday) =>
|
||||
GetFileName(personBirthdayFormat, personBirthday);
|
||||
static string GetFileName(string personBirthdayFormat, Models.PersonBirthday personBirthday) =>
|
||||
|
@ -5,10 +5,10 @@ public interface IPersonContainer
|
||||
|
||||
// ...
|
||||
|
||||
Models.PersonContainer[] TestStatic_GetPersonContainers(Properties.IStorage storage, string personBirthdayFormat, char[] personCharacters, string facesFileNameExtension, Dictionary<string, List<string>> individuals) =>
|
||||
GetPersonContainers(storage, personBirthdayFormat, personCharacters, facesFileNameExtension, individuals);
|
||||
static Models.PersonContainer[] GetPersonContainers(Properties.IStorage storage, string personBirthdayFormat, char[] personCharacters, string facesFileNameExtension, Dictionary<string, List<string>> individuals) =>
|
||||
PersonContainer.GetPersonContainers(storage, personBirthdayFormat, personCharacters, facesFileNameExtension, individuals);
|
||||
Models.PersonContainer[] TestStatic_GetPersonContainers(Properties.IStorage storage, string mappingDefaultName, string personBirthdayFormat, char[] personCharacters, string facesFileNameExtension, Dictionary<string, List<string>> individuals) =>
|
||||
GetPersonContainers(storage, mappingDefaultName, personBirthdayFormat, personCharacters, facesFileNameExtension, individuals);
|
||||
static Models.PersonContainer[] GetPersonContainers(Properties.IStorage storage, string mappingDefaultName, string personBirthdayFormat, char[] personCharacters, string facesFileNameExtension, Dictionary<string, List<string>> individuals) =>
|
||||
PersonContainer.GetPersonContainers(storage, mappingDefaultName, personBirthdayFormat, personCharacters, facesFileNameExtension, individuals);
|
||||
|
||||
List<(long?, string)> TestStatic_GetDisplay(string personBirthdayFormat, Models.PersonContainer personContainer) =>
|
||||
GetDisplay(personBirthdayFormat, personContainer);
|
||||
|
@ -1,3 +1,5 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
|
||||
internal abstract class Person
|
||||
@ -49,19 +51,20 @@ internal abstract class Person
|
||||
File.WriteAllLines(exportFile, cleanLines);
|
||||
}
|
||||
|
||||
internal static (string[] headerLines, Dictionary<string, List<string>> individuals, string[] footerLines) GetIndividuals(string gedCOMFile)
|
||||
internal static (string[] headerLines, Dictionary<string, List<string>> individuals, string[] footerLines) GetIndividuals(string? gedCOMFile)
|
||||
{
|
||||
Dictionary<string, List<string>> results = new();
|
||||
string? nick;
|
||||
int startAt = 0;
|
||||
List<string> lines = new();
|
||||
const string startsWith = "0 @";
|
||||
List<string> headerLines = new();
|
||||
List<string> footerLines = new();
|
||||
string[] sourceLines = File.ReadAllLines(gedCOMFile);
|
||||
string[] sourceLines = string.IsNullOrEmpty(gedCOMFile) ? Array.Empty<string>() : File.ReadAllLines(gedCOMFile);
|
||||
for (int i = 0; i < sourceLines.Length; i++)
|
||||
{
|
||||
lines.Add(sourceLines[i]);
|
||||
if (sourceLines[i].EndsWith("@ INDI"))
|
||||
if (sourceLines[i].StartsWith(startsWith))
|
||||
{
|
||||
lines.RemoveAt(lines.Count - 1);
|
||||
headerLines.AddRange(lines);
|
||||
@ -72,41 +75,44 @@ internal abstract class Person
|
||||
}
|
||||
for (int i = startAt; i < sourceLines.Length; i++)
|
||||
{
|
||||
if (!sourceLines[i].StartsWith("0 @"))
|
||||
if (!sourceLines[i].StartsWith(startsWith))
|
||||
continue;
|
||||
nick = null;
|
||||
lines.Add(sourceLines[i]);
|
||||
for (int j = i + 1; j < sourceLines.Length; j++)
|
||||
if (sourceLines[i].EndsWith("@ FAM"))
|
||||
{
|
||||
if (sourceLines[j].StartsWith("0 @I"))
|
||||
break;
|
||||
lines.Add(sourceLines[j]);
|
||||
if (!sourceLines[j].StartsWith("2 NICK "))
|
||||
continue;
|
||||
nick = sourceLines[j][7..];
|
||||
for (int j = i + 1; j < sourceLines.Length; j++)
|
||||
lines.Add(sourceLines[j]);
|
||||
footerLines.AddRange(lines);
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(nick))
|
||||
else if (sourceLines[i].EndsWith("@ INDI"))
|
||||
{
|
||||
if (lines[^1] != "0 TRLR")
|
||||
throw new Exception(string.Join(Environment.NewLine, lines));
|
||||
else
|
||||
for (int j = i + 1; j < sourceLines.Length; j++)
|
||||
{
|
||||
footerLines.AddRange(lines);
|
||||
break;
|
||||
if (sourceLines[j].StartsWith(startsWith))
|
||||
break;
|
||||
lines.Add(sourceLines[j]);
|
||||
if (!sourceLines[j].StartsWith("2 NICK "))
|
||||
continue;
|
||||
nick = sourceLines[j][7..];
|
||||
}
|
||||
if (string.IsNullOrEmpty(nick))
|
||||
throw new Exception(string.Join(Environment.NewLine, lines));
|
||||
results.Add(nick, new());
|
||||
if (lines.Count > 25)
|
||||
lines.Clear();
|
||||
results[nick].AddRange(lines);
|
||||
lines.Clear();
|
||||
}
|
||||
results.Add(nick, new());
|
||||
results[nick].AddRange(lines);
|
||||
lines.Clear();
|
||||
else
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
return (headerLines.ToArray(), results, footerLines.ToArray());
|
||||
}
|
||||
|
||||
private static void WriteGedFile(string personKeyFormatted, Models.PersonBirthday personBirthday, string[]? filteredIndividualsLines, Models.PersonName name, string[] matches)
|
||||
private static void WriteGedComFile(string personKeyFormatted, Models.PersonBirthday personBirthday, string[]? filteredIndividualsLines, Models.PersonName name, bool isDefaultName, string directory)
|
||||
{
|
||||
string? directory = Path.GetDirectoryName(matches[0]);
|
||||
if (directory is null)
|
||||
throw new Exception();
|
||||
string? sexLine;
|
||||
string? deathLine = null;
|
||||
string jrOrSr;
|
||||
@ -115,13 +121,13 @@ internal abstract class Person
|
||||
else
|
||||
{
|
||||
if (name.Alias.Value.Contains(" Jr"))
|
||||
jrOrSr = "Jr";
|
||||
jrOrSr = " Jr";
|
||||
else if (name.Alias.Value.Contains(" Sr"))
|
||||
jrOrSr = "Sr";
|
||||
jrOrSr = " Sr";
|
||||
else
|
||||
jrOrSr = string.Empty;
|
||||
}
|
||||
string nameLine = $"1 NAME {name.First.Value}/{name.Last.Value}/{jrOrSr}";
|
||||
string nameLine = $"1 NAME {name.First.Value} /{name.Last.Value}/{jrOrSr}";
|
||||
if (personKeyFormatted[^2..] is "23" or "21" or "19" or "17" or "15")
|
||||
{
|
||||
sexLine = "1 SEX M";
|
||||
@ -160,18 +166,18 @@ internal abstract class Person
|
||||
sexLine = "1 SEX U";
|
||||
else
|
||||
{
|
||||
string sex;
|
||||
string code;
|
||||
if (deathLine is null or not "1 DEAT Y")
|
||||
sex = sexLine[6] is 'M' ? "05" : sexLine[6] is 'F' ? "04" : sexLine[6] is 'U' ? "02" : throw new NotImplementedException();
|
||||
code = sexLine[6] is 'M' ? "05" : sexLine[6] is 'F' ? "04" : sexLine[6] is 'U' ? "02" : throw new NotImplementedException();
|
||||
else
|
||||
sex = sexLine[6] is 'M' ? "15" : sexLine[6] is 'F' ? "14" : sexLine[6] is 'U' ? "03" : throw new NotImplementedException();
|
||||
code = sexLine[6] is 'M' ? "15" : sexLine[6] is 'F' ? "14" : sexLine[6] is 'U' ? "03" : throw new NotImplementedException();
|
||||
if (directory.EndsWith("00"))
|
||||
directory = string.Concat(directory[..^2], sex);
|
||||
directory = string.Concat(directory[..^2], code);
|
||||
else if (directory.EndsWith("01"))
|
||||
directory = string.Concat(directory[..^2], sex);
|
||||
directory = string.Concat(directory[..^2], code);
|
||||
else
|
||||
throw new NotImplementedException();
|
||||
personKeyFormatted = $"{personKeyFormatted[..^2]}{sex}";
|
||||
personKeyFormatted = $"{personKeyFormatted[..^2]}{code}";
|
||||
}
|
||||
}
|
||||
List<string> pGedLines = new();
|
||||
@ -197,6 +203,8 @@ internal abstract class Person
|
||||
pGedLines.Add("1 BIRT");
|
||||
pGedLines.Add($"2 DATE {personBirthday.Value:dd MMM yyyy}");
|
||||
}
|
||||
if (isDefaultName)
|
||||
pGedLines.Add("9 NOTE");
|
||||
if (filteredIndividualsLines is not null)
|
||||
{
|
||||
for (int i = 1; i < filteredIndividualsLines.Length; i++)
|
||||
@ -257,13 +265,11 @@ internal abstract class Person
|
||||
{
|
||||
if (personContainer.Key is null || personContainer.Birthdays is null || personContainer.Person is null || !personContainer.Birthdays.Any())
|
||||
continue;
|
||||
if (personContainer.DisplayDirectoryName == mappingDefaultName || personContainer.Person.Name.Alias.Value == "Z")
|
||||
if (IPerson.IsDefaultName(mappingDefaultName, personContainer.DisplayDirectoryName) || IPerson.IsDefaultName(mappingDefaultName, personContainer.Person))
|
||||
continue;
|
||||
if (distinct.Contains(personContainer.Key.Value))
|
||||
continue;
|
||||
distinct.Add(personContainer.Key.Value);
|
||||
if (!personKeyToIds.ContainsKey(personContainer.Key.Value))
|
||||
continue;
|
||||
personBirthday = personContainer.Birthdays[zero];
|
||||
personKeyFormatted = IPersonBirthday.GetFormatted(personBirthdayFormat, personBirthday);
|
||||
by = IPersonBirthday.IsCounterPersonBirthday(personBirthday) ? resultAllInOne : "People";
|
||||
@ -275,6 +281,8 @@ internal abstract class Person
|
||||
continue;
|
||||
pGedLines = File.ReadAllLines(pGedFiles[0]);
|
||||
lines.AddRange(pGedLines);
|
||||
if (!personKeyToIds.ContainsKey(personContainer.Key.Value))
|
||||
lines.Add("1 NOTE");
|
||||
// segments = personContainer.DisplayDirectoryName.Split(_Configuration.PersonCharacters.ToArray());
|
||||
// if (segments.Length < 2)
|
||||
// directory = Path.Combine(rootDirectory, $"000 {personKeyFormatted} {personContainer.DisplayDirectoryName}");
|
||||
@ -288,9 +296,10 @@ internal abstract class Person
|
||||
File.WriteAllLines(Path.Combine(a2PeopleContentDirectory, $"{ticks}.ged"), lines);
|
||||
}
|
||||
|
||||
internal static Models.Person GetPerson(string[] personDisplayDirectoryAllFiles, string? personKeyFormatted, long personKey, Models.PersonBirthday personBirthday, string[] segments, string[]? filteredIndividualsLines)
|
||||
internal static Models.Person GetPerson(string mappingDefaultName, char[] personCharacters, string personDisplayDirectoryName, string[] personDisplayDirectoryAllFiles, string? personKeyFormatted, long personKey, Models.PersonBirthday personBirthday, string[]? filteredIndividualsLines)
|
||||
{
|
||||
Models.Person result;
|
||||
string[] matches;
|
||||
const int zero = 0;
|
||||
List<Models.PersonURL> urls = new();
|
||||
Models.PersonId id = new(personKey);
|
||||
@ -298,10 +307,29 @@ internal abstract class Person
|
||||
List<Models.PersonNumber> numbers = new();
|
||||
List<Models.PersonComment> comments = new();
|
||||
List<Models.PersonAddress> addresses = new();
|
||||
Models.PersonName name = PersonName.Create(segments[zero]);
|
||||
string[] matches = (from l in personDisplayDirectoryAllFiles where !string.IsNullOrEmpty(personKeyFormatted) && l.Contains(personKeyFormatted) select l).ToArray();
|
||||
string checkFileName = $"{personDisplayDirectoryName[zero]}.json";
|
||||
bool isDefaultName = IPerson.IsDefaultName(mappingDefaultName, personDisplayDirectoryName);
|
||||
string nameWithoutApproximateYears = !isDefaultName ? personDisplayDirectoryName.Split(personCharacters)[zero] : personDisplayDirectoryName;
|
||||
matches = (from l in personDisplayDirectoryAllFiles where Path.GetFileName(l) == checkFileName select l).ToArray();
|
||||
Models.PersonName? name;
|
||||
if (!matches.Any())
|
||||
name = PersonName.Create(nameWithoutApproximateYears);
|
||||
else
|
||||
{
|
||||
string json = File.ReadAllText(matches[zero]);
|
||||
name = JsonSerializer.Deserialize<Models.PersonName>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
name ??= PersonName.Create(nameWithoutApproximateYears);
|
||||
if (name.Last is null || string.IsNullOrEmpty(name.Last.Value))
|
||||
name = PersonName.Create(nameWithoutApproximateYears);
|
||||
}
|
||||
matches = (from l in personDisplayDirectoryAllFiles where !string.IsNullOrEmpty(personKeyFormatted) && l.Contains(personKeyFormatted) select l).ToArray();
|
||||
if (!string.IsNullOrEmpty(personKeyFormatted) && matches.Any())
|
||||
WriteGedFile(personKeyFormatted, personBirthday, filteredIndividualsLines, name, matches);
|
||||
{
|
||||
string? directory = Path.GetDirectoryName(matches[zero]);
|
||||
if (directory is null)
|
||||
throw new Exception();
|
||||
WriteGedComFile(personKeyFormatted, personBirthday, filteredIndividualsLines, name, isDefaultName, directory);
|
||||
}
|
||||
result = new(id, personBirthday, name, comments, urls, numbers, emails, addresses);
|
||||
return result;
|
||||
}
|
||||
|
@ -132,4 +132,77 @@ internal abstract class PersonBirthday
|
||||
return results;
|
||||
}
|
||||
|
||||
private static string? GetMonthShortForm(string month)
|
||||
{
|
||||
string? result = month.ToLower()[0] switch
|
||||
{
|
||||
// 'j' => "jan",
|
||||
'f' => "feb",
|
||||
// 'm' => "mar",
|
||||
// 'a' => "apr",
|
||||
// 'm' => "may",
|
||||
// 'j' => "jun",
|
||||
// 'j' => "jul",
|
||||
// 'a' => "aug",
|
||||
's' => "sep",
|
||||
'o' => "oct",
|
||||
'n' => "nov",
|
||||
'd' => "dec",
|
||||
_ => null
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static DateTime? GetDate(string month, string day, string year)
|
||||
{
|
||||
DateTime? result;
|
||||
DateTime dayDateTime;
|
||||
DateTime yearDateTime;
|
||||
DateTime monthDateTime;
|
||||
string? monthShortHand = string.IsNullOrEmpty(month) ? "x" : GetMonthShortForm(month);
|
||||
if (month.Length > 3)
|
||||
{
|
||||
if (!DateTime.TryParseExact($"{month},1,1500", "MMMM,d,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out monthDateTime))
|
||||
monthDateTime = DateTime.MinValue;
|
||||
}
|
||||
else if (month.Length == 3)
|
||||
{
|
||||
if (!DateTime.TryParseExact($"{month},1,1500", "MMM,d,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out monthDateTime))
|
||||
monthDateTime = DateTime.MinValue;
|
||||
}
|
||||
else if (month.Length == 1 && monthShortHand is not null)
|
||||
{
|
||||
if (!DateTime.TryParseExact($"{monthShortHand},1,1500", "MMM,d,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out monthDateTime))
|
||||
monthDateTime = DateTime.MinValue;
|
||||
}
|
||||
else if (int.TryParse(month, out int _))
|
||||
{
|
||||
if (!DateTime.TryParseExact($"{month.PadLeft(2, '0')[..2]},1,1500", "MM,d,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out monthDateTime))
|
||||
monthDateTime = DateTime.MinValue;
|
||||
}
|
||||
else
|
||||
monthDateTime = DateTime.MinValue;
|
||||
if (!int.TryParse(day, out int _))
|
||||
dayDateTime = DateTime.MinValue;
|
||||
else
|
||||
{
|
||||
if (!DateTime.TryParseExact($"01,{day.PadLeft(2, '0')[..2]},1500", "MM,d,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dayDateTime))
|
||||
dayDateTime = DateTime.MinValue;
|
||||
}
|
||||
if (year.Length == 2 && int.TryParse(year, out int _))
|
||||
{
|
||||
if (!DateTime.TryParseExact($"01,01,{year.PadLeft(4, '0')[..4]}", "MM,dd,yy", CultureInfo.InvariantCulture, DateTimeStyles.None, out yearDateTime))
|
||||
yearDateTime = DateTime.MinValue;
|
||||
}
|
||||
else if (year.Length == 4 && int.TryParse(year, out int _))
|
||||
{
|
||||
if (!DateTime.TryParseExact($"01,01,{year.PadLeft(4, '0')[..4]}", "MM,dd,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out yearDateTime))
|
||||
yearDateTime = DateTime.MinValue;
|
||||
}
|
||||
else
|
||||
yearDateTime = DateTime.MinValue;
|
||||
result = monthDateTime == DateTime.MinValue ? null : dayDateTime == DateTime.MinValue ? null : yearDateTime == DateTime.MinValue ? null : new(yearDateTime.Year, monthDateTime.Month, dayDateTime.Day);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -3,41 +3,96 @@ namespace View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
internal abstract class PersonContainer
|
||||
{
|
||||
|
||||
private static string[] GetFiles(string personDisplayDirectory)
|
||||
{
|
||||
List<string> results = new();
|
||||
string[] files;
|
||||
string extension;
|
||||
string checkFile;
|
||||
string directoryName;
|
||||
List<string> distinct = new();
|
||||
string fileNameWithoutExtension;
|
||||
string personDisplayDirectoryName = Path.GetFileName(personDisplayDirectory);
|
||||
string[] directories = Directory.GetDirectories(personDisplayDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
directoryName = Path.GetFileName(directory);
|
||||
files = Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
extension = Path.GetExtension(file);
|
||||
if (extension is not ".json" and not ".pged")
|
||||
{
|
||||
results.Add(file);
|
||||
continue;
|
||||
}
|
||||
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
||||
if (string.IsNullOrEmpty(fileNameWithoutExtension) || string.IsNullOrEmpty(personDisplayDirectoryName))
|
||||
continue;
|
||||
else if (fileNameWithoutExtension.Length == 1 && fileNameWithoutExtension[0] == personDisplayDirectoryName[0])
|
||||
{
|
||||
if (distinct.Contains(file))
|
||||
throw new NotSupportedException($"Move / Delete <{file}>");
|
||||
distinct.Add(file);
|
||||
}
|
||||
else if (fileNameWithoutExtension != directoryName)
|
||||
{
|
||||
checkFile = Path.Combine(directory, $"{fileNameWithoutExtension}{extension}");
|
||||
if (!File.Exists(checkFile))
|
||||
{
|
||||
File.Move(file, checkFile);
|
||||
results.Add(checkFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
checkFile = Path.Combine(directory, $"{fileNameWithoutExtension}.txt");
|
||||
if (File.Exists(checkFile))
|
||||
File.Delete(checkFile);
|
||||
File.Move(file, checkFile);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
results.Add(file);
|
||||
}
|
||||
}
|
||||
return results.ToArray();
|
||||
}
|
||||
|
||||
private static string[] GetFiles(string facesFileNameExtension, string personDisplayDirectory)
|
||||
{
|
||||
string[] results = Directory.GetFiles(personDisplayDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
string[] results;
|
||||
int? id;
|
||||
string checkFile;
|
||||
string? checkDirectory;
|
||||
int? normalizedRectangle;
|
||||
foreach (string personDisplayDirectoryAllFile in results)
|
||||
string[] files = Directory.GetFiles(personDisplayDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
if (personDisplayDirectoryAllFile.EndsWith(".lnk"))
|
||||
if (file.EndsWith(".lnk"))
|
||||
continue;
|
||||
(id, normalizedRectangle) = IMapping.GetConverted(facesFileNameExtension, personDisplayDirectoryAllFile);
|
||||
(id, normalizedRectangle) = IMapping.GetConverted(facesFileNameExtension, file);
|
||||
if (id is not null && normalizedRectangle is not null)
|
||||
continue;
|
||||
checkDirectory = Path.GetDirectoryName(personDisplayDirectoryAllFile);
|
||||
checkDirectory = Path.GetDirectoryName(file);
|
||||
if (string.IsNullOrEmpty(checkDirectory))
|
||||
continue;
|
||||
checkDirectory = Path.Combine(checkDirectory, "_ Invalid");
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
checkFile = Path.Combine(checkDirectory, Path.GetFileName(personDisplayDirectoryAllFile));
|
||||
checkFile = Path.Combine(checkDirectory, Path.GetFileName(file));
|
||||
if (File.Exists(checkFile))
|
||||
File.Delete(personDisplayDirectoryAllFile);
|
||||
File.Delete(file);
|
||||
else
|
||||
File.Move(personDisplayDirectoryAllFile, checkFile);
|
||||
File.Move(file, checkFile);
|
||||
}
|
||||
results = Directory.GetFiles(personDisplayDirectory, "*", SearchOption.AllDirectories);
|
||||
results = GetFiles(personDisplayDirectory);
|
||||
return results;
|
||||
}
|
||||
|
||||
private static List<(long?, Models.PersonContainer)> GetPersonContainersCollections(string facesFileNameExtension, char[] personCharacters, Dictionary<string, List<string>> individuals, char @char, char numberSign, string personDisplayDirectory, string personDisplayDirectoryName, int? approximateYears, List<(string PersonKeyFormatted, Models.PersonBirthday PersonBirthday)> collection)
|
||||
private static List<(long?, Models.PersonContainer)> GetPersonContainersCollections(string mappingDefaultName, string facesFileNameExtension, char[] personCharacters, Dictionary<string, List<string>> individuals, char @char, char numberSign, string personDisplayDirectory, string personDisplayDirectoryName, int? approximateYears, List<(string PersonKeyFormatted, Models.PersonBirthday PersonBirthday)> collection)
|
||||
{
|
||||
List<(long?, Models.PersonContainer)> results = new();
|
||||
long personKey;
|
||||
string[] segments;
|
||||
const int zero = 0;
|
||||
Models.Person person;
|
||||
List<string>? individualsLines;
|
||||
@ -47,7 +102,6 @@ internal abstract class PersonContainer
|
||||
string[] personDisplayDirectoryAllFiles = GetFiles(facesFileNameExtension, personDisplayDirectory);
|
||||
foreach ((string personKeyFormatted, Models.PersonBirthday personBirthday) in collection)
|
||||
{
|
||||
segments = personDisplayDirectoryName.Split(personCharacters);
|
||||
orderedPersonBirthdays = (from l in collection where !l.PersonKeyFormatted.Contains(numberSign) orderby l.PersonBirthday.Value.Ticks descending select l.PersonBirthday).ToArray();
|
||||
if (!orderedPersonBirthdays.Any())
|
||||
personKey = collection[zero].PersonBirthday.Value.Ticks;
|
||||
@ -59,7 +113,7 @@ internal abstract class PersonContainer
|
||||
}
|
||||
_ = individuals.TryGetValue(personKeyFormatted, out individualsLines);
|
||||
filteredIndividualsLines = individualsLines is null ? null : IPerson.GetFiltered(individualsLines);
|
||||
person = IPerson.GetPerson(personDisplayDirectoryAllFiles, personKeyFormatted, personKey, segments, filteredIndividualsLines);
|
||||
person = IPerson.GetPerson(mappingDefaultName, personCharacters, personDisplayDirectoryName, personDisplayDirectoryAllFiles, personKeyFormatted, personKey, filteredIndividualsLines);
|
||||
personContainer = new(approximateYears, @char, person, orderedPersonBirthdays, personDisplayDirectoryAllFiles, personDisplayDirectoryName, personKey, filteredIndividualsLines);
|
||||
results.Add(new(personKey, personContainer));
|
||||
}
|
||||
@ -105,7 +159,7 @@ internal abstract class PersonContainer
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<(long?, Models.PersonContainer)> GetPersonContainersGroup(string personBirthdayFormat, string facesFileNameExtension, char[] personCharacters, Dictionary<string, List<string>> individuals, char @char, string[] personDisplayDirectories)
|
||||
private static List<(long?, Models.PersonContainer)> GetPersonContainersGroup(string mappingDefaultName, string personBirthdayFormat, string facesFileNameExtension, char[] personCharacters, Dictionary<string, List<string>> individuals, char @char, string[] personDisplayDirectories)
|
||||
{
|
||||
List<(long?, Models.PersonContainer)> results = new();
|
||||
string? minusOne;
|
||||
@ -134,7 +188,7 @@ internal abstract class PersonContainer
|
||||
if (changes.Any(l => l is not null))
|
||||
continue;
|
||||
if (collection.Any())
|
||||
results.AddRange(GetPersonContainersCollections(facesFileNameExtension, personCharacters, individuals, @char, numberSign, personDisplayDirectory, personDisplayDirectoryName, approximateYears, collection));
|
||||
results.AddRange(GetPersonContainersCollections(mappingDefaultName, facesFileNameExtension, personCharacters, individuals, @char, numberSign, personDisplayDirectory, personDisplayDirectoryName, approximateYears, collection));
|
||||
else
|
||||
{
|
||||
personContainer = GetPersonContainer(facesFileNameExtension, @char, personDisplayDirectory, personDisplayDirectoryName, approximateYears);
|
||||
@ -146,7 +200,7 @@ internal abstract class PersonContainer
|
||||
return results;
|
||||
}
|
||||
|
||||
private static Models.PersonContainer[] GetPersonContainersGroups(string personBirthdayFormat, string facesFileNameExtension, char[] personCharacters, Dictionary<string, List<string>> individuals, string[] groupDirectories)
|
||||
private static Models.PersonContainer[] GetPersonContainersGroups(string mappingDefaultName, string personBirthdayFormat, string facesFileNameExtension, char[] personCharacters, Dictionary<string, List<string>> individuals, string[] groupDirectories)
|
||||
{
|
||||
Models.PersonContainer[] results;
|
||||
const int zero = 0;
|
||||
@ -160,14 +214,14 @@ internal abstract class PersonContainer
|
||||
if (!personCharacters.Contains(groupDirectoryName[zero]))
|
||||
continue;
|
||||
personDisplayDirectories = Directory.GetDirectories(groupDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
collection = GetPersonContainersGroup(personBirthdayFormat, facesFileNameExtension, personCharacters, individuals, groupDirectoryName[zero], personDisplayDirectories);
|
||||
collection = GetPersonContainersGroup(mappingDefaultName, personBirthdayFormat, facesFileNameExtension, personCharacters, individuals, groupDirectoryName[zero], personDisplayDirectories);
|
||||
personContainers.AddRange(collection);
|
||||
}
|
||||
results = (from l in personContainers orderby l.PersonKey is not null, l.PersonKey select l.PersonContainer).ToArray();
|
||||
return results;
|
||||
}
|
||||
|
||||
internal static Models.PersonContainer[] GetPersonContainers(Properties.IStorage storage, string personBirthdayFormat, char[] personCharacters, string facesFileNameExtension, Dictionary<string, List<string>> individuals)
|
||||
internal static Models.PersonContainer[] GetPersonContainers(Properties.IStorage storage, string mappingDefaultName, string personBirthdayFormat, char[] personCharacters, string facesFileNameExtension, Dictionary<string, List<string>> individuals)
|
||||
{
|
||||
Models.PersonContainer[] results;
|
||||
string a2PeopleSingletonDirectory = Path.Combine(storage.PeopleRootDirectory, "{}");
|
||||
@ -184,7 +238,7 @@ internal abstract class PersonContainer
|
||||
if (!groupDirectories.Any())
|
||||
results = Array.Empty<Models.PersonContainer>();
|
||||
else
|
||||
results = GetPersonContainersGroups(personBirthdayFormat, facesFileNameExtension, personCharacters, individuals, groupDirectories);
|
||||
results = GetPersonContainersGroups(mappingDefaultName, personBirthdayFormat, facesFileNameExtension, personCharacters, individuals, groupDirectories);
|
||||
return results;
|
||||
}
|
||||
|
||||
@ -205,5 +259,5 @@ internal abstract class PersonContainer
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -35,6 +35,7 @@ internal abstract partial class XDirectory
|
||||
fileSearchFilter = string.Concat('*', fileSearchFilter);
|
||||
if (!directorySearchFilter.Contains('*'))
|
||||
directorySearchFilter = string.Concat('*', directorySearchFilter);
|
||||
results.Add(Directory.GetFiles(directory, fileSearchFilter, SearchOption.TopDirectoryOnly));
|
||||
string[] directories = Directory.GetDirectories(directory, directorySearchFilter, SearchOption.TopDirectoryOnly);
|
||||
foreach (string innerDirectory in directories)
|
||||
results.Add(Directory.GetFiles(innerDirectory, fileSearchFilter, SearchOption.AllDirectories));
|
||||
|
@ -3,7 +3,7 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<LangVersion>10.0</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
||||
<RuntimeIdentifiers>win-x64;linux-x64</RuntimeIdentifiers>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
|
Reference in New Issue
Block a user