changeset 6131:24bf08f02e55 attach-custom-data

merge
author Alain Mazy <am@orthanc.team>
date Wed, 21 May 2025 21:31:14 +0200
parents 31d41a52ac2f (diff) 08745ddaee12 (current diff)
children 38c388f7eaed
files
diffstat 8 files changed, 123 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/SystemToolbox.cpp	Wed May 21 17:24:19 2025 +0200
+++ b/OrthancFramework/Sources/SystemToolbox.cpp	Wed May 21 21:31:14 2025 +0200
@@ -454,6 +454,31 @@
     }
   }
 
+#if ORTHANC_ENABLE_MD5 == 1
+  void SystemToolbox::ComputeFileMD5(std::string& result,
+                                     const std::string& path)
+  {
+    std::ifstream fileStream(path, std::ifstream::binary);
+    Toolbox::ComputeMD5(result, fileStream);
+  }
+
+  bool SystemToolbox::CompareFilesMD5(const std::string& path1,
+                                      const std::string& path2)
+  {
+    if (SystemToolbox::GetFileSize(path1) != SystemToolbox::GetFileSize(path2))
+    {
+      return false;
+    }
+
+    std::string path1md5, path2md5;
+    
+    SystemToolbox::ComputeFileMD5(path1md5, path1);
+    SystemToolbox::ComputeFileMD5(path2md5, path2);
+
+    return path1md5 == path2md5;
+  }
+#endif
+
 
   void SystemToolbox::MakeDirectory(const std::string& path)
   {
--- a/OrthancFramework/Sources/SystemToolbox.h	Wed May 21 17:24:19 2025 +0200
+++ b/OrthancFramework/Sources/SystemToolbox.h	Wed May 21 21:31:14 2025 +0200
@@ -83,6 +83,15 @@
 
     static uint64_t GetFileSize(const std::string& path);
 
+#if ORTHANC_ENABLE_MD5 == 1
+    static void ComputeFileMD5(std::string& result,
+                               const std::string& path);
+
+    // returns true if file have the same MD5
+    static bool CompareFilesMD5(const std::string& path1,
+                                const std::string& path2); 
+#endif
+
     static void MakeDirectory(const std::string& path);
 
     static bool IsExistingFile(const std::string& path);
--- a/OrthancFramework/Sources/Toolbox.cpp	Wed May 21 17:24:19 2025 +0200
+++ b/OrthancFramework/Sources/Toolbox.cpp	Wed May 21 21:31:14 2025 +0200
@@ -499,6 +499,40 @@
   }
 
   void Toolbox::ComputeMD5(std::string& result,
+                           std::istream& inputStream)
+  {
+    md5_state_s state;
+    md5_init(&state);
+
+    const size_t bufferSize = 1024;
+    char buffer[bufferSize];
+    
+    while (inputStream.good())
+    {
+      inputStream.read(buffer, bufferSize);
+      std::streamsize bytesRead = inputStream.gcount();
+
+      if (bytesRead > 0)
+      {
+        md5_append(&state, 
+                   reinterpret_cast<const md5_byte_t*>(buffer), 
+                 static_cast<int>(bytesRead));
+      }
+    }
+
+    md5_byte_t actualHash[16];
+    md5_finish(&state, actualHash);
+
+    result.resize(32);
+    for (unsigned int i = 0; i < 16; i++)
+    {
+      result[2 * i] = GetHexadecimalCharacter(static_cast<uint8_t>(actualHash[i] / 16));
+      result[2 * i + 1] = GetHexadecimalCharacter(static_cast<uint8_t>(actualHash[i] % 16));
+    }
+  }
+
+
+  void Toolbox::ComputeMD5(std::string& result,
                            const std::set<std::string>& data)
   {
     std::string s;
--- a/OrthancFramework/Sources/Toolbox.h	Wed May 21 17:24:19 2025 +0200
+++ b/OrthancFramework/Sources/Toolbox.h	Wed May 21 21:31:14 2025 +0200
@@ -134,6 +134,9 @@
 
     static void ComputeMD5(std::string& result,
                            const std::set<std::string>& data);
+
+    static void ComputeMD5(std::string& result,
+                           std::istream& stream);
 #endif
 
     static void ComputeSHA1(std::string& result,
--- a/OrthancFramework/UnitTestsSources/FrameworkTests.cpp	Wed May 21 17:24:19 2025 +0200
+++ b/OrthancFramework/UnitTestsSources/FrameworkTests.cpp	Wed May 21 21:31:14 2025 +0200
@@ -397,6 +397,10 @@
 
   Toolbox::ComputeMD5(s, set);
   ASSERT_EQ("d1aaf4767a3c10a473407a4e47b02da6", s); // set md5 same as string with the values sorted
+
+  std::istringstream iss(std::string("aaabbbccc"));
+  Toolbox::ComputeMD5(s, iss);
+  ASSERT_EQ("d1aaf4767a3c10a473407a4e47b02da6", s);
 }
 
 TEST(Toolbox, ComputeSHA1)
@@ -1591,6 +1595,50 @@
 #endif
 
 
+#if ORTHANC_SANDBOXED != 1 && ORTHANC_ENABLE_MD5 == 1
+TEST(Toolbox, FileMD5)
+{
+  std::string path;
+
+  {
+    TemporaryFile tmp1, tmp2;
+    std::string s = "aaabbbccc";
+
+    SystemToolbox::WriteFile(s, tmp1.GetPath());
+    SystemToolbox::WriteFile(s, tmp2.GetPath());
+
+    std::string md5;
+    SystemToolbox::ComputeFileMD5(md5, tmp1.GetPath());
+
+    ASSERT_EQ("d1aaf4767a3c10a473407a4e47b02da6", md5);
+    ASSERT_TRUE(SystemToolbox::CompareFilesMD5(tmp1.GetPath(), tmp2.GetPath()));
+  }
+
+  { // different sizes
+    TemporaryFile tmp1, tmp2;
+    std::string s1 = "aaabbbccc";
+    std::string s2 = "aaabbbcccd";
+
+    SystemToolbox::WriteFile(s1, tmp1.GetPath());
+    SystemToolbox::WriteFile(s2, tmp2.GetPath());
+
+    ASSERT_FALSE(SystemToolbox::CompareFilesMD5(tmp1.GetPath(), tmp2.GetPath()));
+  }
+
+  { // same sizes, different contents
+    TemporaryFile tmp1, tmp2;
+    std::string s1 = "aaabbbccc";
+    std::string s2 = "aaabbbccd";
+
+    SystemToolbox::WriteFile(s1, tmp1.GetPath());
+    SystemToolbox::WriteFile(s2, tmp2.GetPath());
+
+    ASSERT_FALSE(SystemToolbox::CompareFilesMD5(tmp1.GetPath(), tmp2.GetPath()));
+  }
+
+}
+#endif
+
 #if ORTHANC_SANDBOXED != 1
 TEST(Toolbox, GetMacAddressess)
 {
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Wed May 21 17:24:19 2025 +0200
+++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Wed May 21 21:31:14 2025 +0200
@@ -2128,6 +2128,7 @@
       dbCapabilities_.SetHasFindSupport(systemInfo.supports_find());
       dbCapabilities_.SetKeyValueStoresSupport(systemInfo.supports_key_value_stores());
       dbCapabilities_.SetQueuesSupport(systemInfo.supports_queues());
+      dbCapabilities_.SetAttachmentCustomDataSupport(systemInfo.has_attachment_custom_data());
     }
 
     open_ = true;
--- a/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto	Wed May 21 17:24:19 2025 +0200
+++ b/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto	Wed May 21 21:31:14 2025 +0200
@@ -174,6 +174,7 @@
     bool has_extended_changes = 9;  // New in Orthanc 1.12.5
     bool supports_key_value_stores = 10;  // New in Orthanc 1.12.99
     bool supports_queues = 11;            // New in Orthanc 1.12.99
+    bool has_attachment_custom_data = 12; // New in Orthanc 1.12.99
   }
 }
 
--- a/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp	Wed May 21 17:24:19 2025 +0200
+++ b/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp	Wed May 21 21:31:14 2025 +0200
@@ -2516,6 +2516,7 @@
     dbCapabilities_.SetHasFindSupport(HasIntegratedFind());
     dbCapabilities_.SetKeyValueStoresSupport(true);
     dbCapabilities_.SetQueuesSupport(true);
+    dbCapabilities_.SetAttachmentCustomDataSupport(true);
     db_.Open(path);
   }
 
@@ -2532,6 +2533,7 @@
     dbCapabilities_.SetHasFindSupport(HasIntegratedFind());
     dbCapabilities_.SetKeyValueStoresSupport(true);
     dbCapabilities_.SetQueuesSupport(true);
+    dbCapabilities_.SetAttachmentCustomDataSupport(true);
     db_.OpenInMemory();
   }