Handle export from phpMyAdmin
This commit is contained in:
@ -52,7 +52,7 @@ public record DatabaseFile(
|
||||
[property: JsonPropertyName("updated_in")] string UpdatedIn,
|
||||
[property: JsonPropertyName("published_at")] object PublishedAt,
|
||||
[property: JsonPropertyName("deleted_at")] object DeletedAt)
|
||||
{
|
||||
{
|
||||
|
||||
internal static Shared.Models.DatabaseFile Map(DatabaseFile databaseFile)
|
||||
{
|
||||
@ -61,51 +61,51 @@ public record DatabaseFile(
|
||||
(int fileWidth, int fileHeight) = (int.Parse(databaseFile.FileWidth), int.Parse(databaseFile.FileHeight));
|
||||
result = new(id,
|
||||
photoId,
|
||||
databaseFile.PhotoUid,
|
||||
databaseFile.PhotoTakenAt,
|
||||
databaseFile.TimeIndex,
|
||||
databaseFile.MediaId,
|
||||
databaseFile.MediaUtc,
|
||||
databaseFile.InstanceId,
|
||||
databaseFile.FileUid,
|
||||
databaseFile.FileName,
|
||||
databaseFile.FileRoot,
|
||||
databaseFile.OriginalName,
|
||||
databaseFile.FileHash,
|
||||
databaseFile.FileSize,
|
||||
databaseFile.FileCodec,
|
||||
databaseFile.FileType,
|
||||
databaseFile.MediaType,
|
||||
databaseFile.FileMime,
|
||||
databaseFile.FilePrimary,
|
||||
databaseFile.FileSidecar,
|
||||
databaseFile.FileMissing,
|
||||
databaseFile.FilePortrait,
|
||||
databaseFile.FileVideo,
|
||||
databaseFile.FileDuration,
|
||||
databaseFile.FileFps,
|
||||
databaseFile.FileFrames,
|
||||
F_PhotoPrism.HexStringToString(databaseFile.PhotoUid),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.PhotoTakenAt),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.TimeIndex),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.MediaId),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.MediaUtc),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.InstanceId),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileUid),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileName),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileRoot),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.OriginalName),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileHash),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileSize),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileCodec),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileType),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.MediaType),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileMime),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FilePrimary),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileSidecar),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileMissing),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FilePortrait),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileVideo),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileDuration),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileFps),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileFrames),
|
||||
fileWidth,
|
||||
fileHeight,
|
||||
databaseFile.FileOrientation,
|
||||
databaseFile.FileOrientationSrc,
|
||||
databaseFile.FileProjection,
|
||||
databaseFile.FileAspectRatio,
|
||||
databaseFile.FileHdr,
|
||||
databaseFile.FileWatermark,
|
||||
databaseFile.FileColorProfile,
|
||||
databaseFile.FileMainColor,
|
||||
databaseFile.FileColors,
|
||||
databaseFile.FileLuminance,
|
||||
databaseFile.FileDiff,
|
||||
databaseFile.FileChroma,
|
||||
databaseFile.FileSoftware,
|
||||
databaseFile.FileError,
|
||||
databaseFile.ModTime,
|
||||
databaseFile.CreatedAt,
|
||||
databaseFile.CreatedIn,
|
||||
databaseFile.UpdatedAt,
|
||||
databaseFile.UpdatedIn,
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileOrientation),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileOrientationSrc),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileProjection),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileAspectRatio),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileHdr),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileWatermark),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileColorProfile),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileMainColor),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileColors),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileLuminance),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileDiff),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileChroma),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileSoftware),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.FileError),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.ModTime),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.CreatedAt),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.CreatedIn),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.UpdatedAt),
|
||||
F_PhotoPrism.HexStringToString(databaseFile.UpdatedIn),
|
||||
databaseFile.PublishedAt,
|
||||
databaseFile.DeletedAt);
|
||||
return result;
|
||||
|
@ -32,28 +32,28 @@ public record Marker(
|
||||
Shared.Models.Marker result;
|
||||
(double x, double y, double w, double h, double score) = (double.Parse(marker.X), double.Parse(marker.Y), double.Parse(marker.W), double.Parse(marker.H), double.Parse(marker.Score));
|
||||
result = new(
|
||||
marker.MarkerUid,
|
||||
marker.FileUid,
|
||||
marker.MarkerType,
|
||||
marker.MarkerSrc,
|
||||
marker.MarkerName,
|
||||
marker.MarkerReview,
|
||||
marker.MarkerInvalid,
|
||||
marker.SubjUid,
|
||||
marker.SubjSrc,
|
||||
marker.FaceId,
|
||||
marker.FaceDist,
|
||||
F_PhotoPrism.HexStringToString(marker.MarkerUid),
|
||||
F_PhotoPrism.HexStringToString(marker.FileUid),
|
||||
F_PhotoPrism.HexStringToString(marker.MarkerType),
|
||||
F_PhotoPrism.HexStringToString(marker.MarkerSrc),
|
||||
F_PhotoPrism.HexStringToString(marker.MarkerName),
|
||||
F_PhotoPrism.HexStringToString(marker.MarkerReview),
|
||||
F_PhotoPrism.HexStringToString(marker.MarkerInvalid),
|
||||
F_PhotoPrism.HexStringToString(marker.SubjUid),
|
||||
F_PhotoPrism.HexStringToString(marker.SubjSrc),
|
||||
F_PhotoPrism.HexStringToString(marker.FaceId),
|
||||
F_PhotoPrism.HexStringToString(marker.FaceDist),
|
||||
x,
|
||||
y,
|
||||
w,
|
||||
h,
|
||||
marker.Q,
|
||||
marker.Size,
|
||||
F_PhotoPrism.HexStringToString(marker.Q),
|
||||
F_PhotoPrism.HexStringToString(marker.Size),
|
||||
score,
|
||||
marker.Thumb,
|
||||
marker.MatchedAt,
|
||||
marker.CreatedAt,
|
||||
marker.UpdatedAt);
|
||||
F_PhotoPrism.HexStringToString(marker.Thumb),
|
||||
F_PhotoPrism.HexStringToString(marker.MatchedAt),
|
||||
F_PhotoPrism.HexStringToString(marker.CreatedAt),
|
||||
F_PhotoPrism.HexStringToString(marker.UpdatedAt));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -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