Handle export from phpMyAdmin
This commit is contained in:
@ -46,14 +46,29 @@ public class F_PhotoPrism
|
||||
private static Dictionary<string, List<Shared.Models.Marker>> GetFileUIdToMarkers(string fPhotoPrismSingletonDirectory)
|
||||
{
|
||||
Dictionary<string, List<Shared.Models.Marker>> results = new();
|
||||
string fileUid;
|
||||
Marker[]? markers = GetMarkers(fPhotoPrismSingletonDirectory);
|
||||
if (markers is null)
|
||||
throw new NullReferenceException(nameof(markers));
|
||||
foreach (Marker marker in markers)
|
||||
{
|
||||
if (!results.ContainsKey(marker.FileUid))
|
||||
results.Add(marker.FileUid, new());
|
||||
results[marker.FileUid].Add(Marker.Map(marker));
|
||||
fileUid = HexStringToString(marker.FileUid);
|
||||
if (!results.ContainsKey(fileUid))
|
||||
results.Add(fileUid, new());
|
||||
results[fileUid].Add(Marker.Map(marker));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static Dictionary<int, List<MappingFromPhotoPrism>>? GetCollectionFile(string fileName)
|
||||
{
|
||||
Dictionary<int, List<MappingFromPhotoPrism>>? results;
|
||||
if (!File.Exists(fileName))
|
||||
results = null;
|
||||
else
|
||||
{
|
||||
string json = File.ReadAllText(fileName);
|
||||
results = JsonSerializer.Deserialize<Dictionary<int, List<MappingFromPhotoPrism>>>(json);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
@ -80,43 +95,86 @@ public class F_PhotoPrism
|
||||
return results;
|
||||
}
|
||||
|
||||
public static Dictionary<string, List<MappingFromPhotoPrism>> GetFileNameToCollection(string fPhotoPrismSingletonDirectory)
|
||||
public static Dictionary<int, List<MappingFromPhotoPrism>> GetFileNameToCollection(string fPhotoPrismSingletonDirectory)
|
||||
{
|
||||
Dictionary<string, List<MappingFromPhotoPrism>> results = new();
|
||||
List<Shared.Models.Marker>? makers;
|
||||
MappingFromPhotoPrism mappingFromPhotoPrism;
|
||||
List<MappingFromPhotoPrism>? mappingFromPhotoPrismCollection;
|
||||
List<Shared.Models.DatabaseFile>? databaseFiles = GetDatabaseFiles(fPhotoPrismSingletonDirectory);
|
||||
if (databaseFiles is not null)
|
||||
Dictionary<int, List<MappingFromPhotoPrism>>? results;
|
||||
string fileName = Path.Combine(fPhotoPrismSingletonDirectory, "collection.json");
|
||||
results = GetCollectionFile(fileName);
|
||||
if (results is null)
|
||||
{
|
||||
Dictionary<string, List<Shared.Models.Marker>> fileUIdToMarkers = GetFileUIdToMarkers(fPhotoPrismSingletonDirectory);
|
||||
foreach (Shared.Models.DatabaseFile databaseFile in databaseFiles)
|
||||
int id;
|
||||
results = new();
|
||||
string fileNameWithoutExtension;
|
||||
List<Shared.Models.Marker>? makers;
|
||||
MappingFromPhotoPrism mappingFromPhotoPrism;
|
||||
List<MappingFromPhotoPrism>? mappingFromPhotoPrismCollection;
|
||||
List<Shared.Models.DatabaseFile>? databaseFiles = GetDatabaseFiles(fPhotoPrismSingletonDirectory);
|
||||
if (databaseFiles is not null)
|
||||
{
|
||||
if (!results.TryGetValue(databaseFile.FileName, out mappingFromPhotoPrismCollection))
|
||||
Dictionary<string, List<Shared.Models.Marker>> fileUIdToMarkers = GetFileUIdToMarkers(fPhotoPrismSingletonDirectory);
|
||||
foreach (Shared.Models.DatabaseFile databaseFile in databaseFiles)
|
||||
{
|
||||
results.Add(databaseFile.FileName, new());
|
||||
if (!results.TryGetValue(databaseFile.FileName, out mappingFromPhotoPrismCollection))
|
||||
throw new Exception();
|
||||
if (databaseFile.FileName is null || databaseFile.FileUid is null)
|
||||
continue;
|
||||
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(databaseFile.FileName);
|
||||
if (!int.TryParse(fileNameWithoutExtension, out id))
|
||||
continue;
|
||||
|
||||
if (!results.TryGetValue(id, out mappingFromPhotoPrismCollection))
|
||||
{
|
||||
results.Add(id, new());
|
||||
if (!results.TryGetValue(id, out mappingFromPhotoPrismCollection))
|
||||
throw new Exception();
|
||||
}
|
||||
if (!fileUIdToMarkers.TryGetValue(databaseFile.FileUid, out makers))
|
||||
mappingFromPhotoPrism = new(databaseFile, new());
|
||||
else
|
||||
mappingFromPhotoPrism = new(databaseFile, makers);
|
||||
mappingFromPhotoPrismCollection.Add(mappingFromPhotoPrism);
|
||||
}
|
||||
if (!fileUIdToMarkers.TryGetValue(databaseFile.FileUid, out makers))
|
||||
mappingFromPhotoPrism = new(databaseFile, new());
|
||||
else
|
||||
mappingFromPhotoPrism = new(databaseFile, makers);
|
||||
mappingFromPhotoPrismCollection.Add(mappingFromPhotoPrism);
|
||||
}
|
||||
string json = JsonSerializer.Serialize(results);
|
||||
_ = IPath.WriteAllText(fileName, json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public static void WriteMatches(string fPhotoPrismContentDirectory, string personBirthdayFormat, long ticks, List<Face> distinctFilteredFaces, Shared.Models.Methods.IMapLogic mapLogic)
|
||||
private static void PopulateSubjects(string mappingDefaultName, string personBirthdayFormat, List<string> subjects, StringBuilder stringBuilder, PersonContainer[] personContainers, (MappingFromPhotoPrism MappingFromPhotoPrism, Shared.Models.Marker Marker, double Percent)[] sortedCollection)
|
||||
{
|
||||
long? personKey;
|
||||
string personName;
|
||||
const int zero = 0;
|
||||
int? normalizedRectangle;
|
||||
string personKeyFormatted;
|
||||
List<string> subjects = new();
|
||||
PersonBirthday personBirthday;
|
||||
string personDisplayDirectoryName;
|
||||
foreach ((MappingFromPhotoPrism mappingFromPhotoPrism, Shared.Models.Marker marker, double percent) in sortedCollection)
|
||||
{
|
||||
foreach (PersonContainer personContainer in personContainers)
|
||||
{
|
||||
if (personContainer.Key is null || personContainer.Birthdays is null || personContainer.Person is null || !personContainer.Birthdays.Any())
|
||||
continue;
|
||||
if (IPerson.IsDefaultName(mappingDefaultName, personContainer.DisplayDirectoryName) || IPerson.IsDefaultName(mappingDefaultName, personContainer.Person))
|
||||
continue;
|
||||
personBirthday = personContainer.Birthdays[zero];
|
||||
personKey = personBirthday.Value.Ticks;
|
||||
personName = personContainer.Person.GetFullName();
|
||||
personKeyFormatted = IPersonBirthday.GetFormatted(personBirthdayFormat, personBirthday);
|
||||
subjects.Add($"update `subjects` set subj_alias = '{personKeyFormatted}' where subj_name = '{personName}';");
|
||||
_ = stringBuilder.
|
||||
Append("update `markers` set subj_src = 'manual', marker_name = '").
|
||||
Append(personName).
|
||||
Append("' where marker_uid = '").
|
||||
Append(marker.MarkerUid).
|
||||
AppendLine("';");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteMatches(string fPhotoPrismContentDirectory, string mappingDefaultName, string personBirthdayFormat, long ticks, List<Face> distinctFilteredFaces, Shared.Models.Methods.IMapLogic mapLogic)
|
||||
{
|
||||
string file;
|
||||
string text;
|
||||
int? normalizedRectangle;
|
||||
List<string> subjects = new();
|
||||
StringBuilder stringBuilder = new();
|
||||
PersonContainer[]? personContainers;
|
||||
System.Drawing.Rectangle dlibRectangle;
|
||||
@ -158,28 +216,34 @@ public class F_PhotoPrism
|
||||
if (!collection.Any())
|
||||
continue;
|
||||
sortedCollection = collection.OrderByDescending(l => l.Percent).ToArray();
|
||||
foreach ((MappingFromPhotoPrism mappingFromPhotoPrism, Shared.Models.Marker marker, double percent) in sortedCollection)
|
||||
{
|
||||
foreach (PersonContainer personContainer in personContainers)
|
||||
{
|
||||
if (personContainer.Key is null || personContainer.Birthdays is null || !personContainer.Birthdays.Any())
|
||||
continue;
|
||||
personBirthday = personContainer.Birthdays[zero];
|
||||
personKey = personBirthday.Value.Ticks;
|
||||
personDisplayDirectoryName = personContainer.DisplayDirectoryName;
|
||||
personKeyFormatted = IPersonBirthday.GetFormatted(personBirthdayFormat, personBirthday);
|
||||
subjects.Add($"update `subjects` set subj_alias = '{personKeyFormatted}' where subj_name = '{personDisplayDirectoryName}';");
|
||||
_ = stringBuilder.
|
||||
Append("update `markers` set subj_src = 'manual', marker_name = '").
|
||||
Append(personDisplayDirectoryName).
|
||||
Append("' where marker_uid = '").
|
||||
Append(marker.MarkerUid).
|
||||
AppendLine("';");
|
||||
}
|
||||
}
|
||||
PopulateSubjects(mappingDefaultName, personBirthdayFormat, subjects, stringBuilder, personContainers, sortedCollection);
|
||||
}
|
||||
File.WriteAllLines(Path.Combine(fPhotoPrismContentDirectory, $"{ticks}-subject_alias_update.sql"), subjects.Distinct());
|
||||
File.WriteAllText(Path.Combine(fPhotoPrismContentDirectory, $"{ticks}-marker_name_update.sql"), stringBuilder.ToString());
|
||||
if (subjects.Any())
|
||||
{
|
||||
file = Path.Combine(fPhotoPrismContentDirectory, $"{ticks}-subject_alias_update.sql");
|
||||
text = string.Join(Environment.NewLine, subjects.Distinct());
|
||||
_ = IPath.WriteAllText(file, text, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
|
||||
file = Path.Combine(fPhotoPrismContentDirectory, $"{ticks}-marker_name_update.sql");
|
||||
text = stringBuilder.ToString();
|
||||
_ = IPath.WriteAllText(file, text, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
|
||||
}
|
||||
}
|
||||
|
||||
internal static string HexStringToString(string text)
|
||||
{
|
||||
string result;
|
||||
if (text.Length < 2 || text[0] != '0' || text[1] != 'x')
|
||||
result = text;
|
||||
else
|
||||
{
|
||||
string after = text[2..];
|
||||
byte[] bytes = Enumerable.Range(0, after.Length)
|
||||
.Where(x => x % 2 == 0)
|
||||
.Select(x => Convert.ToByte(after.Substring(x, 2), 16))
|
||||
.ToArray();
|
||||
result = Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user