person-key-to-immich-import birthday json (Day-Helper-2024-05-18)

csharp_prefer_braces = true
This commit is contained in:
2025-09-06 11:16:55 -07:00
parent 8ec89953bc
commit 6102da7266
54 changed files with 2218 additions and 1721 deletions

View File

@ -1,12 +1,10 @@
using File_Folder_Helper.Models;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.Logging;
using System.Collections.ObjectModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using File_Folder_Helper.Models;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.Logging;
#if ShellProgressBar
using ShellProgressBar;
#endif
@ -65,53 +63,53 @@ internal static partial class Helper20250407 {
const int rightSideOnlyIndex = 4;
const int leftSideIsNewerIndex = 1;
const int rightSideIsNewerIndex = 3;
if (string.IsNullOrEmpty(segments[leftSideOnlyIndex]))
if (string.IsNullOrEmpty(segments[leftSideOnlyIndex])) {
leftSideOnly = null;
else if (segments[leftSideOnlyIndex][0] == plus)
} else if (segments[leftSideOnlyIndex][0] == plus) {
leftSideOnly = true;
else if (segments[leftSideOnlyIndex][0] == minus)
} else if (segments[leftSideOnlyIndex][0] == minus) {
leftSideOnly = false;
else {
} else {
check = false;
leftSideOnly = null;
}
if (string.IsNullOrEmpty(segments[leftSideIsNewerIndex]))
if (string.IsNullOrEmpty(segments[leftSideIsNewerIndex])) {
leftSideIsNewer = null;
else if (segments[leftSideIsNewerIndex][0] == greaterThan)
} else if (segments[leftSideIsNewerIndex][0] == greaterThan) {
leftSideIsNewer = true;
else if (segments[leftSideIsNewerIndex][0] == lessThan)
} else if (segments[leftSideIsNewerIndex][0] == lessThan) {
leftSideIsNewer = false;
else {
} else {
check = false;
leftSideIsNewer = null;
}
if (string.IsNullOrEmpty(segments[notEqualButIndex]))
if (string.IsNullOrEmpty(segments[notEqualButIndex])) {
notEqualBut = null;
else if (segments[notEqualButIndex][0] == greaterThan)
} else if (segments[notEqualButIndex][0] == greaterThan) {
notEqualBut = true;
else if (segments[notEqualButIndex][0] == lessThan)
} else if (segments[notEqualButIndex][0] == lessThan) {
notEqualBut = false;
else {
} else {
check = false;
notEqualBut = null;
}
if (string.IsNullOrEmpty(segments[rightSideIsNewerIndex]))
if (string.IsNullOrEmpty(segments[rightSideIsNewerIndex])) {
rightSideIsNewer = null;
else if (segments[rightSideIsNewerIndex][0] == greaterThan)
} else if (segments[rightSideIsNewerIndex][0] == greaterThan) {
rightSideIsNewer = true;
else if (segments[rightSideIsNewerIndex][0] == lessThan)
} else if (segments[rightSideIsNewerIndex][0] == lessThan) {
rightSideIsNewer = false;
else {
} else {
check = false;
rightSideIsNewer = null;
}
if (string.IsNullOrEmpty(segments[rightSideOnlyIndex]))
if (string.IsNullOrEmpty(segments[rightSideOnlyIndex])) {
rightSideOnly = null;
else if (segments[rightSideOnlyIndex][0] == plus)
} else if (segments[rightSideOnlyIndex][0] == plus) {
rightSideOnly = true;
else if (segments[rightSideOnlyIndex][0] == minus)
} else if (segments[rightSideOnlyIndex][0] == minus) {
rightSideOnly = false;
else {
} else {
check = false;
rightSideOnly = null;
}
@ -160,9 +158,9 @@ internal static partial class Helper20250407 {
matcher.AddIncludePatterns(!File.Exists(includePatternsFile) ? ["*"] : File.ReadAllLines(includePatternsFile));
matcher.AddExcludePatterns(!File.Exists(excludePatternsFile) ? ["System Volume Information"] : File.ReadAllLines(excludePatternsFile));
ReadOnlyCollection<Record> rightRecords = GetRecords(rightDirectory, matcher);
if (rightRecords.Count == 0)
if (rightRecords.Count == 0) {
logger.LogInformation("No source records");
else {
} else {
string checkFile = Path.Combine(rightDirectory, fileName);
Review review = new(AreEqual: null,
LeftSideIsNewer: null,
@ -173,9 +171,9 @@ internal static partial class Helper20250407 {
RightSideOnly: null);
string json = JsonSerializer.Serialize(review, ReviewCommonSourceGenerationContext.Default.Review);
WriteAllText(checkFile, json);
if (rootUniformResourceLocators.Length == 0)
if (rootUniformResourceLocators.Length == 0) {
logger.LogInformation("No urls");
else {
} else {
string format = NginxFileSystem.GetFormat();
TimeZoneInfo timeZoneInfo = TimeZoneInfo.Local;
Sync(logger, rightDirectory, fileName, logic, rootUniformResourceLocators, rightRecords, format, timeZoneInfo);
@ -194,11 +192,13 @@ internal static partial class Helper20250407 {
ReadOnlyCollection<ReadOnlyCollection<string>> collection = Helpers.HelperDirectory.GetFilesCollection(rightDirectory, "*", "*");
foreach (ReadOnlyCollection<string> c in collection) {
foreach (string f in c) {
if (!matcher.Match(rightDirectory, f).HasMatches)
if (!matcher.Match(rightDirectory, f).HasMatches) {
continue;
}
fileInfo = new(f);
if (fileInfo.Length == 0)
if (fileInfo.Length == 0) {
continue;
}
relativePath = Path.GetRelativePath(rightDirectory, fileInfo.FullName);
record = new(RelativePath: relativePath,
Size: fileInfo.Length,
@ -211,20 +211,21 @@ internal static partial class Helper20250407 {
private static void WriteAllText(string path, string text) {
string check = !File.Exists(path) ? string.Empty : File.ReadAllText(path);
if (check != text)
if (check != text) {
File.WriteAllText(path, text);
}
}
private static void Sync(ILogger<Worker> logger, string rightDirectory, string fileName, Logic? logic, string[] rootUniformResourceLocators, ReadOnlyCollection<Record> rightRecords, string format, TimeZoneInfo timeZoneInfo) {
Review? review;
foreach (string rootUniformResourceLocator in rootUniformResourceLocators) {
if (!rootUniformResourceLocator.StartsWith("https:"))
if (!rootUniformResourceLocator.StartsWith("https:")) {
logger.LogInformation("Not supported URL <{url}>", rootUniformResourceLocator);
else {
} else {
review = GetJsonResponse(logger, fileName, rootUniformResourceLocator, format, timeZoneInfo);
if (review?.Records is null || review.Records.Length == 0)
if (review?.Records is null || review.Records.Length == 0) {
logger.LogInformation("No response records");
else {
} else {
ReadOnlyCollection<Record> leftRecords = review.Records.AsReadOnly();
Sync(logger, rightDirectory, fileName, logic, rightRecords, rootUniformResourceLocator, leftRecords);
}
@ -285,8 +286,9 @@ internal static partial class Helper20250407 {
match = nginxFileSystem.LastModified.Value;
continue;
}
if (nginxFileSystem.LastModified is null || nginxFileSystem.LastModified <= dateTime)
if (nginxFileSystem.LastModified is null || nginxFileSystem.LastModified <= dateTime) {
continue;
}
dateTime = nginxFileSystem.LastModified.Value;
}
result = match is not null && match.Value > dateTime;
@ -298,8 +300,9 @@ internal static partial class Helper20250407 {
string checkFile;
HttpClient httpClient = new();
checkFile = Path.Combine(rightDirectory, fileName);
if (File.Exists(checkFile))
if (File.Exists(checkFile)) {
File.Delete(checkFile);
}
ReadOnlyCollection<Segment> areEqual = GetAreEqual(rightDirectory, fileName, rightRecords, rootUniformResourceLocators, leftRecords);
ReadOnlyCollection<Segment> notEqualBut = GetNotEqualBut(rightDirectory, fileName, rightRecords, rootUniformResourceLocators, leftRecords);
ReadOnlyCollection<Segment> leftSideOnly = GetLeftSideOnly(rightDirectory, fileName, rightRecords, rootUniformResourceLocators, leftRecords);
@ -316,26 +319,36 @@ internal static partial class Helper20250407 {
json = JsonSerializer.Serialize(review, ReviewCommonSourceGenerationContext.Default.Review);
checkFile = Path.Combine(rightDirectory, fileName);
WriteAllText(checkFile, json);
if (notEqualBut.Count > 0 && l is not null && l.NotEqualBut is not null && l.Raw[l.NotEqualButIndex][0] == l.Minus && !l.NotEqualBut.Value)
if (notEqualBut.Count > 0 && l is not null && l.NotEqualBut is not null && l.Raw[l.NotEqualButIndex][0] == l.Minus && !l.NotEqualBut.Value) {
logger.LogDebug("Doing nothing with {name}", nameof(Logic.NotEqualBut));
if (leftSideOnly.Count > 0 && l is not null && l.LeftSideOnly is not null && l.Raw[l.LeftSideOnlyIndex][0] == l.Minus && !l.LeftSideOnly.Value)
}
if (leftSideOnly.Count > 0 && l is not null && l.LeftSideOnly is not null && l.Raw[l.LeftSideOnlyIndex][0] == l.Minus && !l.LeftSideOnly.Value) {
throw new NotImplementedException("Not possible with https!");
if (leftSideIsNewer.Count > 0 && l is not null && l.LeftSideIsNewer is not null && l.Raw[l.LeftSideIsNewerIndex][0] == l.LessThan && !l.LeftSideIsNewer.Value)
}
if (leftSideIsNewer.Count > 0 && l is not null && l.LeftSideIsNewer is not null && l.Raw[l.LeftSideIsNewerIndex][0] == l.LessThan && !l.LeftSideIsNewer.Value) {
throw new NotImplementedException("Not possible with https!");
if (rightSideIsNewer.Count > 0 && l is not null && l.RightSideIsNewer is not null && l.Raw[l.RightSideIsNewerIndex][0] == l.LessThan && !l.RightSideIsNewer.Value)
}
if (rightSideIsNewer.Count > 0 && l is not null && l.RightSideIsNewer is not null && l.Raw[l.RightSideIsNewerIndex][0] == l.LessThan && !l.RightSideIsNewer.Value) {
throw new NotImplementedException("Not possible with https!");
if (rightSideOnly.Count > 0 && l is not null && l.RightSideOnly is not null && l.Raw[l.RightSideOnlyIndex][0] == l.Plus && l.RightSideOnly.Value)
}
if (rightSideOnly.Count > 0 && l is not null && l.RightSideOnly is not null && l.Raw[l.RightSideOnlyIndex][0] == l.Plus && l.RightSideOnly.Value) {
throw new NotImplementedException("Not possible with https!");
if (rightSideOnly.Count > 0 && l is not null && l.RightSideOnly is not null && l.Raw[l.RightSideOnlyIndex][0] == l.Minus && !l.RightSideOnly.Value)
}
if (rightSideOnly.Count > 0 && l is not null && l.RightSideOnly is not null && l.Raw[l.RightSideOnlyIndex][0] == l.Minus && !l.RightSideOnly.Value) {
DoWork(logger, rightDirectory, httpClient, rightSideOnly, delete: true, download: false);
if (leftSideOnly.Count > 0 && l is not null && l.LeftSideOnly is not null && l.Raw[l.LeftSideOnlyIndex][0] == l.Plus && l.LeftSideOnly.Value)
}
if (leftSideOnly.Count > 0 && l is not null && l.LeftSideOnly is not null && l.Raw[l.LeftSideOnlyIndex][0] == l.Plus && l.LeftSideOnly.Value) {
DoWork(logger, rightDirectory, httpClient, leftSideOnly, delete: false, download: true);
if (leftSideIsNewer.Count > 0 && l is not null && l.LeftSideIsNewer is not null && l.Raw[l.LeftSideIsNewerIndex][0] == l.GreaterThan && l.LeftSideIsNewer.Value)
}
if (leftSideIsNewer.Count > 0 && l is not null && l.LeftSideIsNewer is not null && l.Raw[l.LeftSideIsNewerIndex][0] == l.GreaterThan && l.LeftSideIsNewer.Value) {
DoWork(logger, rightDirectory, httpClient, leftSideIsNewer, delete: true, download: true);
if (notEqualBut.Count > 0 && l is not null && l.NotEqualBut is not null && l.Raw[l.NotEqualButIndex][0] == l.Plus && l.NotEqualBut.Value)
}
if (notEqualBut.Count > 0 && l is not null && l.NotEqualBut is not null && l.Raw[l.NotEqualButIndex][0] == l.Plus && l.NotEqualBut.Value) {
DoWork(logger, rightDirectory, httpClient, notEqualBut, delete: true, download: true);
if (rightSideIsNewer.Count > 0 && l is not null && l.RightSideIsNewer is not null && l.Raw[l.RightSideIsNewerIndex][0] == l.GreaterThan && l.RightSideIsNewer.Value)
}
if (rightSideIsNewer.Count > 0 && l is not null && l.RightSideIsNewer is not null && l.Raw[l.RightSideIsNewerIndex][0] == l.GreaterThan && l.RightSideIsNewer.Value) {
DoWork(logger, rightDirectory, httpClient, rightSideIsNewer, delete: true, download: true);
}
}
private static ReadOnlyCollection<Segment> GetAreEqual(string rightDirectory, string fileName, ReadOnlyCollection<Record> rightRecords, string rootUniformResourceLocators, ReadOnlyCollection<Record> leftRecords) {
@ -350,13 +363,16 @@ internal static partial class Helper20250407 {
checkDirectory = r.RelativePath;
continue;
}
if (r.RelativePath == rightDirectory || r.RelativePath == fileName)
if (r.RelativePath == rightDirectory || r.RelativePath == fileName) {
continue;
if (!keyValuePairs.TryGetValue(r.RelativePath, out record))
}
if (!keyValuePairs.TryGetValue(r.RelativePath, out record)) {
continue;
}
totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds;
if (record.Size != r.Size || totalSeconds is > 2 or < -2)
if (record.Size != r.Size || totalSeconds is > 2 or < -2) {
continue;
}
segment = new(Left: r,
LeftDirectory: checkDirectory,
Right: record,
@ -369,8 +385,9 @@ internal static partial class Helper20250407 {
private static ReadOnlyDictionary<string, Record> GetKeyValuePairs(ReadOnlyCollection<Record> records) {
Dictionary<string, Record> results = [];
foreach (Record record in records)
foreach (Record record in records) {
results.Add(record.RelativePath, record);
}
return new(results);
}
@ -386,15 +403,19 @@ internal static partial class Helper20250407 {
checkDirectory = r.RelativePath;
continue;
}
if (r.RelativePath == rightDirectory || r.RelativePath == fileName)
if (r.RelativePath == rightDirectory || r.RelativePath == fileName) {
continue;
if (!keyValuePairs.TryGetValue(r.RelativePath, out record))
}
if (!keyValuePairs.TryGetValue(r.RelativePath, out record)) {
continue;
if (record.Size == r.Size)
}
if (record.Size == r.Size) {
continue;
}
totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds;
if (totalSeconds is >= 2 or <= -2)
if (totalSeconds is >= 2 or <= -2) {
continue;
}
segment = new(Left: r,
LeftDirectory: checkDirectory,
Right: record,
@ -416,10 +437,12 @@ internal static partial class Helper20250407 {
checkDirectory = r.RelativePath;
continue;
}
if (r.RelativePath == rightDirectory || r.RelativePath == fileName)
if (r.RelativePath == rightDirectory || r.RelativePath == fileName) {
continue;
if (keyValuePairs.TryGetValue(r.RelativePath, out record))
}
if (keyValuePairs.TryGetValue(r.RelativePath, out record)) {
continue;
}
segment = new(Left: r,
LeftDirectory: checkDirectory,
Right: record,
@ -441,10 +464,12 @@ internal static partial class Helper20250407 {
checkDirectory = r.RelativePath;
continue;
}
if (r.RelativePath == rightDirectory || r.RelativePath == fileName)
if (r.RelativePath == rightDirectory || r.RelativePath == fileName) {
continue;
if (keyValuePairs.TryGetValue(r.RelativePath, out record))
}
if (keyValuePairs.TryGetValue(r.RelativePath, out record)) {
continue;
}
segment = new(Left: record,
LeftDirectory: null,
Right: r,
@ -467,13 +492,16 @@ internal static partial class Helper20250407 {
checkDirectory = r.RelativePath;
continue;
}
if (r.RelativePath == rightDirectory || r.RelativePath == fileName)
if (r.RelativePath == rightDirectory || r.RelativePath == fileName) {
continue;
if (!keyValuePairs.TryGetValue(r.RelativePath, out record))
}
if (!keyValuePairs.TryGetValue(r.RelativePath, out record)) {
continue;
}
totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds;
if (totalSeconds is > -2)
if (totalSeconds is > -2) {
continue;
}
segment = new(Left: r,
LeftDirectory: checkDirectory,
Right: record,
@ -496,13 +524,16 @@ internal static partial class Helper20250407 {
checkDirectory = r.RelativePath;
continue;
}
if (r.RelativePath == rightDirectory || r.RelativePath == fileName)
if (r.RelativePath == rightDirectory || r.RelativePath == fileName) {
continue;
if (!keyValuePairs.TryGetValue(r.RelativePath, out record))
}
if (!keyValuePairs.TryGetValue(r.RelativePath, out record)) {
continue;
}
totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds;
if (totalSeconds is > -2)
if (totalSeconds is > -2) {
continue;
}
segment = new(Left: record,
LeftDirectory: null,
Right: r,
@ -546,20 +577,6 @@ internal static partial class Helper20250407 {
return result;
}
private static string GetDurationWithSuffix(long ticks) {
string result;
TimeSpan timeSpan = new(DateTime.Now.Ticks - ticks);
if (timeSpan.TotalMilliseconds < 1000)
result = $"{timeSpan.Milliseconds} ms";
else if (timeSpan.TotalMilliseconds < 60000)
result = $"{Math.Floor(timeSpan.TotalSeconds)} s";
else if (timeSpan.TotalMilliseconds < 3600000)
result = $"{Math.Floor(timeSpan.TotalMinutes)} m";
else
result = $"{Math.Floor(timeSpan.TotalHours)} h";
return result;
}
private static void DoDeletes(ILogger<Worker> logger, string rightDirectory, ReadOnlyCollection<Segment> segments) {
Record? record;
string size;
@ -572,8 +589,9 @@ internal static partial class Helper20250407 {
progressBar.Tick();
#endif
record = segments[i].Right;
if (record is null)
if (record is null) {
continue;
}
size = GetSizeWithSuffix(record.Size);
try {
File.Delete(Path.Combine(rightDirectory, record.RelativePath));
@ -609,9 +627,9 @@ internal static partial class Helper20250407 {
size = GetSizeWithSuffix(download.Size);
httpResponseMessage = httpClient.GetAsync(download.UniformResourceLocator);
httpResponseMessage.Wait(-1);
if (!httpResponseMessage.Result.IsSuccessStatusCode)
if (!httpResponseMessage.Result.IsSuccessStatusCode) {
logger.LogInformation("Failed to download: <{checkURL}> - {size};", download.UniformResourceLocator, size);
else {
} else {
response = httpResponseMessage.Result.Content.ReadAsStringAsync();
response.Wait();
try {
@ -643,19 +661,24 @@ internal static partial class Helper20250407 {
List<Download> collection = [];
string? checkUniformResourceLocator;
foreach (Segment segment in segments) {
if (segment.Left is null)
if (segment.Left is null) {
continue;
}
checkFile = Path.Combine(rightDirectory, segment.Left.RelativePath);
checkDirectory = Path.GetDirectoryName(checkFile);
if (string.IsNullOrEmpty(checkDirectory))
if (string.IsNullOrEmpty(checkDirectory)) {
continue;
if (!Directory.Exists(checkDirectory))
}
if (!Directory.Exists(checkDirectory)) {
_ = Directory.CreateDirectory(checkDirectory);
if (File.Exists(checkFile) && new FileInfo(checkFile).Length == 0)
}
if (File.Exists(checkFile) && new FileInfo(checkFile).Length == 0) {
File.Delete(checkFile);
}
checkUniformResourceLocator = ConvertTo(segment.RootUniformResourceLocator, segment.Left.RelativePath);
if (string.IsNullOrEmpty(checkUniformResourceLocator))
if (string.IsNullOrEmpty(checkUniformResourceLocator)) {
continue;
}
download = new(Directory: checkDirectory,
Display: checkUniformResourceLocator[segment.RootUniformResourceLocator.Length..],
File: checkFile,
@ -666,12 +689,15 @@ internal static partial class Helper20250407 {
}
Download[] sorted = (from l in collection orderby l.Size select l).ToArray();
int stop = sorted.Length < 100 ? sorted.Length : 100;
for (int i = 0; i < stop; i++)
for (int i = 0; i < stop; i++) {
results.Add(sorted[i]);
for (int i = sorted.Length - 1; i > stop - 1; i--)
}
for (int i = sorted.Length - 1; i > stop - 1; i--) {
results.Add(sorted[i]);
if (collection.Count != results.Count)
}
if (collection.Count != results.Count) {
throw new Exception();
}
return results.AsReadOnly();
}
@ -682,12 +708,28 @@ internal static partial class Helper20250407 {
string fileName = Path.GetFileName(windowsMock);
ReadOnlyCollection<string> directoryNames = Helpers.HelperDirectory.GetDirectoryNames(windowsMock);
foreach (string directoryName in directoryNames) {
if (directoryName == windowsRoot || directoryName == fileName)
if (directoryName == windowsRoot || directoryName == fileName) {
continue;
}
result = $"{result}/{directoryName}";
}
result = result == rootURL ? null : $"{result}/{fileName}";
return result;
}
private static string GetDurationWithSuffix(long ticks) {
string result;
TimeSpan timeSpan = new(DateTime.Now.Ticks - ticks);
if (timeSpan.TotalMilliseconds < 1000) {
result = $"{timeSpan.Milliseconds} ms";
} else if (timeSpan.TotalMilliseconds < 60000) {
result = $"{Math.Floor(timeSpan.TotalSeconds)} s";
} else if (timeSpan.TotalMilliseconds < 3600000) {
result = $"{Math.Floor(timeSpan.TotalMinutes)} m";
} else {
result = $"{Math.Floor(timeSpan.TotalHours)} h";
}
return result;
}
}