From f458af776a0b9732a46043173c5e1fd4c8f83379 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Sat, 31 Aug 2024 08:17:11 -0700 Subject: [PATCH] Body Changes Only --- Drag-Drop-Explorer/DragDropExplorer.cs | 8 +-- Drag-Drop-Search/DragDropSearch.cs | 10 ++-- FaceRecognitionDotNet/DisposableObject.cs | 9 ++- Metadata/Models/Stateless/Exif.cs | 8 ++- Metadata/Models/Stateless/Face.cs | 11 ++++ .../OffsetDateTimeOriginal.cs | 2 +- Resize/Models/_C_Resize.cs | 2 + .../Models/Stateless/Methods/ImageHelper.cs | 2 + Shared/Phares/Shared/RijndaelEncryption.cs | 32 +++++++---- Tests/UnitTestHardCoded.cs | 55 +++++++++++++------ ThumbHash/Models/ThumbHash.cs | 6 +- 11 files changed, 97 insertions(+), 48 deletions(-) diff --git a/Drag-Drop-Explorer/DragDropExplorer.cs b/Drag-Drop-Explorer/DragDropExplorer.cs index 7fcb7f2..6f73869 100644 --- a/Drag-Drop-Explorer/DragDropExplorer.cs +++ b/Drag-Drop-Explorer/DragDropExplorer.cs @@ -59,7 +59,7 @@ public partial class DragDropExplorer : Form Controls.Add(_FirstTextBox); } - void Form1_Load(object? sender, EventArgs e) + private void Form1_Load(object? sender, EventArgs e) { try { @@ -81,7 +81,7 @@ public partial class DragDropExplorer : Form return result; } - void TextBox_LostFocus(object? sender, EventArgs e) + private void TextBox_LostFocus(object? sender, EventArgs e) { try { @@ -99,7 +99,7 @@ public partial class DragDropExplorer : Form } } - void Form1_DragEnter(object? sender, DragEventArgs e) + private void Form1_DragEnter(object? sender, DragEventArgs e) { try { @@ -112,7 +112,7 @@ public partial class DragDropExplorer : Form } } - void Form1_DragDrop(object? sender, DragEventArgs e) + private void Form1_DragDrop(object? sender, DragEventArgs e) { try { diff --git a/Drag-Drop-Search/DragDropSearch.cs b/Drag-Drop-Search/DragDropSearch.cs index 4c386d9..8bfd8d4 100644 --- a/Drag-Drop-Search/DragDropSearch.cs +++ b/Drag-Drop-Search/DragDropSearch.cs @@ -64,7 +64,7 @@ public partial class DragDropSearch : Form Controls.Add(_TextBox); } - void Form1_Load(object? sender, EventArgs e) + private void Form1_Load(object? sender, EventArgs e) { try { @@ -79,7 +79,7 @@ public partial class DragDropSearch : Form } } - void TextBox_LostFocus(object? sender, EventArgs e) + private void TextBox_LostFocus(object? sender, EventArgs e) { try { @@ -92,7 +92,7 @@ public partial class DragDropSearch : Form } } - void Form1_DragEnter(object? sender, DragEventArgs e) + private void Form1_DragEnter(object? sender, DragEventArgs e) { try { @@ -105,7 +105,7 @@ public partial class DragDropSearch : Form } } - void LoadData() + private void LoadData() { Container[] containers; string aPropertySingletonDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(_Configuration.PropertyConfiguration, nameof(A_Property), "{}"); @@ -168,7 +168,7 @@ public partial class DragDropSearch : Form } } - void Form1_DragDrop(object? sender, DragEventArgs e) + private void Form1_DragDrop(object? sender, DragEventArgs e) { try { diff --git a/FaceRecognitionDotNet/DisposableObject.cs b/FaceRecognitionDotNet/DisposableObject.cs index ac2a3c0..9a37cbc 100644 --- a/FaceRecognitionDotNet/DisposableObject.cs +++ b/FaceRecognitionDotNet/DisposableObject.cs @@ -26,16 +26,15 @@ public abstract class DisposableObject : IDisposable /// /// If this object is disposed, then is thrown. /// - public void ThrowIfDisposed() - { - if (IsDisposed) - throw new ObjectDisposedException(GetType().FullName); - } + public void ThrowIfDisposed() => + ObjectDisposedException.ThrowIf(IsDisposed, this); internal void ThrowIfDisposed(string objectName) { +#pragma warning disable CA1513 if (IsDisposed) throw new ObjectDisposedException(objectName); +#pragma warning restore CA1513 } #region Overrides diff --git a/Metadata/Models/Stateless/Exif.cs b/Metadata/Models/Stateless/Exif.cs index 0ce16f3..468cbaf 100644 --- a/Metadata/Models/Stateless/Exif.cs +++ b/Metadata/Models/Stateless/Exif.cs @@ -519,8 +519,12 @@ internal abstract class Exif internal static Shared.Models.ExifDirectory GetExifDirectory(Shared.Models.FilePath filePath) { - Shared.Models.ExifDirectory? result; - System.Drawing.Size? size = Dimensions.GetDimensions(filePath.FullName); + Shared.Models.ExifDirectory result; + System.Drawing.Size? size; + try + { size = Dimensions.GetDimensions(filePath.FullName); } + catch (Exception) + { size = null; } IReadOnlyList directories = ImageMetadataReader.ReadMetadata(filePath.FullName); result = Covert(filePath, size, directories); return result; diff --git a/Metadata/Models/Stateless/Face.cs b/Metadata/Models/Stateless/Face.cs index 30c77ee..8cb08b1 100644 --- a/Metadata/Models/Stateless/Face.cs +++ b/Metadata/Models/Stateless/Face.cs @@ -35,6 +35,17 @@ internal static class Face result = pngDirectory.TextualData[artist.Length..]; break; } + if (result is null) + { + const string author = "Author:"; + foreach (PngDirectory pngDirectory in pngDirectories) + { + if (pngDirectory.TextualData is null || !pngDirectory.TextualData.StartsWith(author)) + continue; + result = pngDirectory.TextualData[author.Length..]; + break; + } + } } return result; } diff --git a/Offset-Date-Time-Original/OffsetDateTimeOriginal.cs b/Offset-Date-Time-Original/OffsetDateTimeOriginal.cs index 0b9c7dd..0181417 100644 --- a/Offset-Date-Time-Original/OffsetDateTimeOriginal.cs +++ b/Offset-Date-Time-Original/OffsetDateTimeOriginal.cs @@ -112,7 +112,7 @@ public class OffsetDateTimeOriginal string checkFile; PropertyItem? propertyItem; string? ticksDirectory = null; - int dateTimeOriginal = MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagDateTimeOriginal; + const int dateTimeOriginal = MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagDateTimeOriginal; // 36867 for (int i = 0; i < int.MaxValue; i++) { ticksDirectory = Path.Combine(sourceDirectory, ticks.ToString()); diff --git a/Resize/Models/_C_Resize.cs b/Resize/Models/_C_Resize.cs index 34594f0..a5f879b 100644 --- a/Resize/Models/_C_Resize.cs +++ b/Resize/Models/_C_Resize.cs @@ -312,10 +312,12 @@ public class C_Resize { if (mappingFromItem.ResizedFileHolder is null) throw new NullReferenceException(nameof(mappingFromItem.ResizedFileHolder)); +#pragma warning disable CA1854 if (!outputResolutionToResize.ContainsKey(_Original)) throw new Exception(); if (!outputResolutionToResize.ContainsKey(outputResolution)) throw new Exception(); +#pragma warning restore CA1854 FileInfo fileInfo = new(mappingFromItem.ResizedFileHolder.FullName); bool check = false; int[] resize = outputResolutionToResize[outputResolution]; diff --git a/Shared/Models/Stateless/Methods/ImageHelper.cs b/Shared/Models/Stateless/Methods/ImageHelper.cs index 352248f..e4493f2 100644 --- a/Shared/Models/Stateless/Methods/ImageHelper.cs +++ b/Shared/Models/Stateless/Methods/ImageHelper.cs @@ -81,6 +81,7 @@ internal abstract class ImageHelper internal static Size GetDimensions(BinaryReader binaryReader, int? faceRight, int? faceBottom) { Size? result = null; +#pragma warning disable IDE0230 Dictionary> _ImageFormatDecoders = new() { { new byte[] { 0x42, 0x4D }, DecodeBitmap }, @@ -89,6 +90,7 @@ internal abstract class ImageHelper { new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng }, { new byte[] { 0xff, 0xd8 }, DecodeJfif }, }; +#pragma warning restore IDE0230 int maxMagicBytesLength = _ImageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length; byte[] magicBytes = new byte[maxMagicBytesLength]; for (int i = 0; i < maxMagicBytesLength; i += 1) diff --git a/Shared/Phares/Shared/RijndaelEncryption.cs b/Shared/Phares/Shared/RijndaelEncryption.cs index a8b1ee7..0a000c7 100644 --- a/Shared/Phares/Shared/RijndaelEncryption.cs +++ b/Shared/Phares/Shared/RijndaelEncryption.cs @@ -9,25 +9,28 @@ namespace Phares.Shared; public static class RijndaelEncryption { + /// - /// Change the Inputkey GUID when you use this code in your own program. - /// Keep this inputkey very safe and prevent someone from decoding it some way!! + /// Change the input key GUID when you use this code in your own program. + /// Keep this input key very safe and prevent someone from decoding it some way!! /// Generated 2021-08-10 /// - internal const string _Inputkey = "970CCEF6-4307-4F6A-9AC8-377DADB889BD"; + internal const string _InputKey = "970CCEF6-4307-4F6A-9AC8-377DADB889BD"; /// /// Encrypt the given text and give the byte array back as a BASE64 string /// /// The text to encrypt - /// The pasword salt + /// The password salt /// The encrypted text public static string Encrypt(string text, string salt) { string result; if (string.IsNullOrEmpty(text)) - throw new NullReferenceException(nameof(text)); + throw new ArgumentNullException(nameof(text)); +#pragma warning disable RijndaelManaged aesAlg = NewRijndaelManaged(salt); +#pragma warning restore ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); MemoryStream msEncrypt = new(); using (CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write)) @@ -46,7 +49,9 @@ public static class RijndaelEncryption { bool result; base64String = base64String.Trim(); +#pragma warning disable result = (base64String.Length % 4 == 0) && Regex.IsMatch(base64String, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None); +#pragma warning restore return result; } @@ -54,16 +59,18 @@ public static class RijndaelEncryption /// Decrypts the given text /// /// The encrypted BASE64 text - /// The pasword salt + /// The password salt /// De gedecrypte text public static string Decrypt(string cipherText, string salt) { if (string.IsNullOrEmpty(cipherText)) - throw new NullReferenceException(nameof(cipherText)); + throw new ArgumentNullException(nameof(cipherText)); if (!IsBase64String(cipherText)) throw new Exception("The cipherText input parameter is not base64 encoded"); string text; +#pragma warning disable RijndaelManaged aesAlg = NewRijndaelManaged(salt); +#pragma warning restore ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); byte[] cipher = Convert.FromBase64String(cipherText); using (MemoryStream msDecrypt = new(cipher)) @@ -76,19 +83,22 @@ public static class RijndaelEncryption } /// - /// GetPersonName a new RijndaelManaged class and initialize it + /// Create a new RijndaelManaged class and initialize it /// - /// The pasword salt + /// The password salt /// +#pragma warning disable private static RijndaelManaged NewRijndaelManaged(string salt) { if (salt == null) - throw new NullReferenceException(nameof(salt)); + throw new ArgumentNullException(nameof(salt)); byte[] saltBytes = Encoding.ASCII.GetBytes(salt); - Rfc2898DeriveBytes key = new(_Inputkey, saltBytes); + Rfc2898DeriveBytes key = new(_InputKey, saltBytes); RijndaelManaged aesAlg = new(); +#pragma warning restore aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8); return aesAlg; } + } \ No newline at end of file diff --git a/Tests/UnitTestHardCoded.cs b/Tests/UnitTestHardCoded.cs index cdc4c6b..7bbdee0 100644 --- a/Tests/UnitTestHardCoded.cs +++ b/Tests/UnitTestHardCoded.cs @@ -4,6 +4,7 @@ using Phares.Shared; using System.Diagnostics; using System.Globalization; using System.Reflection; +using System.Text.Json; using View_by_Distance.Shared.Models; using View_by_Distance.Shared.Models.Stateless.Methods; using View_by_Distance.Tests.Models; @@ -14,6 +15,7 @@ namespace View_by_Distance.Tests; public partial class UnitTestHardCoded { + private readonly string _Git; private readonly AppSettings _AppSettings; private readonly string _WorkingDirectory; private readonly Configuration _Configuration; @@ -43,6 +45,7 @@ public partial class UnitTestHardCoded Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory); propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot); configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration); + _Git = "c9dbce3b"; _AppSettings = appSettings; _Configuration = configuration; _IsEnvironment = isEnvironment; @@ -86,7 +89,7 @@ public partial class UnitTestHardCoded [TestMethod] public void TestMethodGetApproximateYears() { - string personDisplayDirectory = "D:/1-Images-A/Images-7007a9df-Results/A2)People/7007a9df/{}/^/Sydney Dupray^9"; + string personDisplayDirectory = string.Concat($"D:/1-Images-A/Images-{_Git}-Results/A2)People/{_Git}", "/{}/^/Sydney Dupray^9"); if (Directory.Exists(Directory.GetDirectoryRoot(personDisplayDirectory)) && Directory.Exists(personDisplayDirectory)) { char numberSign = '#'; @@ -189,7 +192,7 @@ public partial class UnitTestHardCoded [TestMethod] public void TestMethodRenameAbandoned() { - string directory = "D:/1-Images-A/Images-7007a9df-Results/A2)People/7007a9df/{}/!/Abandoned"; + string directory = string.Concat($"D:/1-Images-A/Images-{_Git}-Results/A2)People/{_Git}", "/{}/!/Abandoned"); if (Directory.Exists(Path.GetPathRoot(directory)) && Directory.Exists(directory)) { string checkFile; @@ -209,7 +212,7 @@ public partial class UnitTestHardCoded [TestMethod] public void TestMethodRenameDelete() { - string directory = "D:/1-Images-A/Images-7007a9df-Results/A)Property/7007a9df/{}"; + string directory = string.Concat($"D:/1-Images-A/Images-{_Git}-Results/A)Property/{_Git}", "/{}"); if (Directory.Exists(Path.GetPathRoot(directory)) && Directory.Exists(directory)) { string checkFile; @@ -229,7 +232,7 @@ public partial class UnitTestHardCoded [TestMethod] public void TestMethodRenameOld() { - string directory = "D:/1-Images-A/Images-7007a9df-Results/E)Distance/7007a9df/()"; + string directory = $"D:/1-Images-A/Images-{_Git}-Results/E)Distance/{_Git}/()"; if (Directory.Exists(Path.GetPathRoot(directory)) && Directory.Exists(directory)) { string checkFile; @@ -250,7 +253,7 @@ public partial class UnitTestHardCoded public void TestMethodRenameDup() { string directory; - directory = "D:/1-Images-A/Images-7007a9df-Results/E)Distance/7007a9df/()"; + directory = $"D:/1-Images-A/Images-{_Git}-Results/E)Distance/{_Git}/()"; if (Directory.Exists(Path.GetPathRoot(directory)) && Directory.Exists(directory)) { string checkFile; @@ -264,7 +267,7 @@ public partial class UnitTestHardCoded } Assert.IsTrue(true); } - directory = "D:/1-Images-A/Images-7007a9df-Results/A2)People/7007a9df/{}/!"; + directory = string.Concat($"D:/1-Images-A/Images-{_Git}-Results/A2)People/{_Git}", "/{}/!"); if (Directory.Exists(Path.GetPathRoot(directory)) && Directory.Exists(directory)) { string checkFile; @@ -284,9 +287,9 @@ public partial class UnitTestHardCoded [TestMethod] public void TestMethodRename() { - // string directory = "D:/2-Images-B/Not-Copy-Copy-7007a9df"; - string directory = "D:/1-Images-A/Images-7007a9df"; - // string directory = "D:/2-Images-B/Not-Copy-Copy-7007a9df"; + // string $directory = "D:/2-Images-B/Not-Copy-Copy-{_Git}"; + string directory = $"D:/1-Images-A/Images-{_Git}"; + // string $directory = "D:/2-Images-B/Not-Copy-Copy-{_Git}"; if (Directory.Exists(Path.GetPathRoot(directory)) && Directory.Exists(directory)) { string[] directories = Directory.GetDirectories(directory, "*;*", SearchOption.AllDirectories); @@ -301,9 +304,9 @@ public partial class UnitTestHardCoded } [TestMethod] - public void TestMethodRenameForUnkown() + public void TestMethodRenameForUnknown() { - string directory = "D:/1-Images-A/Images-7007a9df-Results/E)Distance/7007a9df/(RectInt-2023-06-19-less-0.99)"; + string directory = $"D:/1-Images-A/Images-{_Git}-Results/E)Distance/{_Git}/(RectInt-2023-06-19-less-0.99)"; if (Directory.Exists(Path.GetPathRoot(directory)) && Directory.Exists(directory)) { string[] files = Directory.GetFiles(directory, "*.unk", SearchOption.AllDirectories); @@ -316,13 +319,13 @@ public partial class UnitTestHardCoded [TestMethod] public void TestMethodRenameForTicks() { - string directory = "D:/1-Images-A/Images-7007a9df-Results/A2)People/7007a9df/([])/ged"; + string directory = $"D:/1-Images-A/Images-{_Git}-Results/A2)People/{_Git}/([])/ged"; if (Directory.Exists(Path.GetPathRoot(directory)) && Directory.Exists(directory)) { string checkName; DateTime dateTime; string weekOfYear; - string checkDirectoy; + string checkDirectory; Calendar calendar = new CultureInfo("en-US").Calendar; string[] files = Directory.GetFiles(directory, "*.ged", SearchOption.TopDirectoryOnly); foreach (string file in files) @@ -331,10 +334,10 @@ public partial class UnitTestHardCoded continue; dateTime = new(ticks); weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00"); - checkDirectoy = Path.Combine(directory, dateTime.Year.ToString(), $"{dateTime.Year}-Week-{weekOfYear}"); - checkName = Path.Combine(checkDirectoy, Path.GetFileName(file)); - if (!Directory.Exists(checkDirectoy)) - _ = Directory.CreateDirectory(checkDirectoy); + checkDirectory = Path.Combine(directory, dateTime.Year.ToString(), $"{dateTime.Year}-Week-{weekOfYear}"); + checkName = Path.Combine(checkDirectory, Path.GetFileName(file)); + if (!Directory.Exists(checkDirectory)) + _ = Directory.CreateDirectory(checkDirectory); if (File.Exists(checkName)) continue; File.Move(file, checkName); @@ -343,4 +346,22 @@ public partial class UnitTestHardCoded NonThrowTryCatch(); } + [TestMethod] + public void TestMethodImmichAsset() + { + if (!string.IsNullOrEmpty(_Configuration.ImmichAssetsFile) && File.Exists(_Configuration.ImmichAssetsFile)) + { + Dictionary keyValuePairs = []; + string json = File.ReadAllText(_Configuration.ImmichAssetsFile); + ImmichAsset[]? immichAssets = JsonSerializer.Deserialize(json, ImmichAssetCollectionSourceGenerationContext.Default.ImmichAssetArray); + if (immichAssets is not null) + { + foreach (ImmichAsset immichAsset in immichAssets) + keyValuePairs.Add(immichAsset.OriginalPath, immichAsset); + } + Assert.IsTrue(keyValuePairs.Count > 0); + } + NonThrowTryCatch(); + } + } \ No newline at end of file diff --git a/ThumbHash/Models/ThumbHash.cs b/ThumbHash/Models/ThumbHash.cs index 371f789..98833a6 100644 --- a/ThumbHash/Models/ThumbHash.cs +++ b/ThumbHash/Models/ThumbHash.cs @@ -11,13 +11,13 @@ public static partial class ThumbHash private const int _MinHash = 5; [DoesNotReturn] - static void ThrowIfLessThan(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) => throw new ArgumentOutOfRangeException(paramName, value, $"'{value}' must be greater than or equal to '{other}'."); + private static void ThrowIfLessThan(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) => throw new ArgumentOutOfRangeException(paramName, value, $"'{value}' must be greater than or equal to '{other}'."); [DoesNotReturn] - static void ThrowIfGreaterThan(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) => throw new ArgumentOutOfRangeException(paramName, value, $"'{paramName}' must be less than or equal to '{other}'."); + private static void ThrowIfGreaterThan(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) => throw new ArgumentOutOfRangeException(paramName, value, $"'{paramName}' must be less than or equal to '{other}'."); [DoesNotReturn] - static void ThrowNotEqual(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null, [CallerArgumentExpression(nameof(other))] string? otherName = null) => throw new ArgumentOutOfRangeException(paramName, value, $"'{paramName}' must be equal to '{other}' ('{otherName}')."); + private static void ThrowNotEqual(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null, [CallerArgumentExpression(nameof(other))] string? otherName = null) => throw new ArgumentOutOfRangeException(paramName, value, $"'{paramName}' must be equal to '{other}' ('{otherName}')."); /// /// Encodes an RGBA image to a ThumbHash.