person-key-to-immich-import birthday json (Day-Helper-2024-05-18)
csharp_prefer_braces = true
This commit is contained in:
@ -18,7 +18,6 @@ internal static partial class Helper20250519 {
|
||||
string result = JsonSerializer.Serialize(this, Helper20250519RelativePath.Default.RelativePath);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
@ -38,7 +37,6 @@ internal static partial class Helper20250519 {
|
||||
string result = JsonSerializer.Serialize(this, Helper20250519Review.Default.Review);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
@ -342,22 +340,6 @@ internal static partial class Helper20250519 {
|
||||
}
|
||||
}
|
||||
|
||||
private static void LiveSync(ILogger<Worker> logger, string page, RelativePath relativePath, HttpClient httpClient, string? directory, ReadOnlyCollection<Record> records, HttpMethod? httpMethod, bool delete) {
|
||||
long sum;
|
||||
try { sum = records.Sum(l => l.Size); } catch (Exception) { sum = 0; }
|
||||
string size = GetSizeWithSuffix(sum);
|
||||
if (delete) {
|
||||
logger.LogInformation("Starting to delete {count} file(s) [{sum}]", records.Count, size);
|
||||
PreformDeletes(logger, relativePath.RightDirectory, records);
|
||||
logger.LogInformation("Deleted {count} file(s) [{sum}]", records.Count, size);
|
||||
}
|
||||
if (httpMethod is not null) {
|
||||
logger.LogInformation("Starting to {httpMethod} {count} file(s) [{sum}]", httpMethod.ToString().ToLower(), records.Count, size);
|
||||
Preform(logger, page, directory, records, httpClient, httpMethod);
|
||||
logger.LogInformation("{httpMethod}'ed {count} file(s) [{sum}]", httpMethod.ToString(), records.Count, size);
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetSizeWithSuffix(long value) {
|
||||
string result;
|
||||
int i = 0;
|
||||
@ -374,6 +356,68 @@ internal static partial class Helper20250519 {
|
||||
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 ReadOnlyCollection<Verb> GetVerbCollection(string? directory, ReadOnlyCollection<Record> records) {
|
||||
List<Verb> results = [];
|
||||
Verb verb;
|
||||
string checkFile;
|
||||
string checkFileName;
|
||||
string? checkDirectory;
|
||||
List<Verb> collection = [];
|
||||
foreach (Record record in records) {
|
||||
if (string.IsNullOrEmpty(directory)) {
|
||||
continue;
|
||||
}
|
||||
checkFile = Path.Combine(directory, record.RelativePath);
|
||||
checkFileName = Path.GetFileName(checkFile);
|
||||
checkDirectory = Path.GetDirectoryName(checkFile);
|
||||
if (string.IsNullOrEmpty(checkDirectory)) {
|
||||
continue;
|
||||
}
|
||||
if (!Directory.Exists(checkDirectory)) {
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
}
|
||||
if (File.Exists(checkFile) && new FileInfo(checkFile).Length == 0) {
|
||||
File.Delete(checkFile);
|
||||
}
|
||||
verb = new(Directory: checkDirectory,
|
||||
Display: $"{checkFileName}{Environment.NewLine}{checkDirectory}",
|
||||
File: checkFile,
|
||||
Multipart: $"RelativePath:{record.RelativePath}|Size:{record.Size}|Ticks:{record.Ticks};",
|
||||
RelativePath: record.RelativePath,
|
||||
Size: record.Size,
|
||||
Ticks: record.Ticks,
|
||||
UrlEncodedFile: HttpUtility.UrlEncode(checkFile));
|
||||
collection.Add(verb);
|
||||
}
|
||||
Verb[] 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++) {
|
||||
results.Add(sorted[i]);
|
||||
}
|
||||
for (int i = sorted.Length - 1; i > stop - 1; i--) {
|
||||
results.Add(sorted[i]);
|
||||
}
|
||||
if (collection.Count != results.Count) {
|
||||
throw new Exception();
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
private static void PreformDeletes(ILogger<Worker> logger, string? directory, ReadOnlyCollection<Record> records) {
|
||||
string size;
|
||||
Record? record;
|
||||
@ -435,8 +479,9 @@ internal static partial class Helper20250519 {
|
||||
{ new StringContent(verb.Directory), "path", iValue }
|
||||
};
|
||||
httpRequestMessage.Content = multipartFormDataContent;
|
||||
} else
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
httpResponseMessage = httpClient.SendAsync(httpRequestMessage);
|
||||
httpResponseMessage.Wait(-1);
|
||||
if (!httpResponseMessage.Result.IsSuccessStatusCode) {
|
||||
@ -470,66 +515,20 @@ internal static partial class Helper20250519 {
|
||||
#endif
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<Verb> GetVerbCollection(string? directory, ReadOnlyCollection<Record> records) {
|
||||
List<Verb> results = [];
|
||||
Verb verb;
|
||||
string checkFile;
|
||||
string checkFileName;
|
||||
string? checkDirectory;
|
||||
List<Verb> collection = [];
|
||||
foreach (Record record in records) {
|
||||
if (string.IsNullOrEmpty(directory)) {
|
||||
continue;
|
||||
}
|
||||
checkFile = Path.Combine(directory, record.RelativePath);
|
||||
checkFileName = Path.GetFileName(checkFile);
|
||||
checkDirectory = Path.GetDirectoryName(checkFile);
|
||||
if (string.IsNullOrEmpty(checkDirectory)) {
|
||||
continue;
|
||||
}
|
||||
if (!Directory.Exists(checkDirectory)) {
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
}
|
||||
if (File.Exists(checkFile) && new FileInfo(checkFile).Length == 0) {
|
||||
File.Delete(checkFile);
|
||||
}
|
||||
verb = new(Directory: checkDirectory,
|
||||
Display: $"{checkFileName}{Environment.NewLine}{checkDirectory}",
|
||||
File: checkFile,
|
||||
Multipart: $"RelativePath:{record.RelativePath}|Size:{record.Size}|Ticks:{record.Ticks};",
|
||||
RelativePath: record.RelativePath,
|
||||
Size: record.Size,
|
||||
Ticks: record.Ticks,
|
||||
UrlEncodedFile: HttpUtility.UrlEncode(checkFile));
|
||||
collection.Add(verb);
|
||||
private static void LiveSync(ILogger<Worker> logger, string page, RelativePath relativePath, HttpClient httpClient, string? directory, ReadOnlyCollection<Record> records, HttpMethod? httpMethod, bool delete) {
|
||||
long sum;
|
||||
try { sum = records.Sum(l => l.Size); } catch (Exception) { sum = 0; }
|
||||
string size = GetSizeWithSuffix(sum);
|
||||
if (delete) {
|
||||
logger.LogInformation("Starting to delete {count} file(s) [{sum}]", records.Count, size);
|
||||
PreformDeletes(logger, relativePath.RightDirectory, records);
|
||||
logger.LogInformation("Deleted {count} file(s) [{sum}]", records.Count, size);
|
||||
}
|
||||
Verb[] 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++) {
|
||||
results.Add(sorted[i]);
|
||||
if (httpMethod is not null) {
|
||||
logger.LogInformation("Starting to {httpMethod} {count} file(s) [{sum}]", httpMethod.ToString().ToLower(), records.Count, size);
|
||||
Preform(logger, page, directory, records, httpClient, httpMethod);
|
||||
logger.LogInformation("{httpMethod}'ed {count} file(s) [{sum}]", httpMethod.ToString(), records.Count, size);
|
||||
}
|
||||
for (int i = sorted.Length - 1; i > stop - 1; i--) {
|
||||
results.Add(sorted[i]);
|
||||
}
|
||||
if (collection.Count != results.Count) {
|
||||
throw new Exception();
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user