changeset 6808:b60fb4f30c4b

added support for StorageRange in StorageAreaDataSource
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 19 May 2026 12:56:29 +0200
parents 6df5cb04dd7f
children 3f5309b92016 65060f296783
files OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp OrthancFramework/Sources/DataSource/StorageAreaDataSource.h OrthancFramework/Sources/FileStorage/StorageRange.cpp OrthancFramework/Sources/FileStorage/StorageRange.h
diffstat 4 files changed, 88 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Tue May 19 12:11:01 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Tue May 19 12:56:29 2026 +0200
@@ -37,7 +37,6 @@
 #include <boost/lexical_cast.hpp>
 #include <cassert>
 
-
 namespace Orthanc
 {
   class StorageAreaDataSource::Value : public IDynamicObject
@@ -169,6 +168,13 @@
   }
 
 
+  StorageAreaDataSource::Range* StorageAreaDataSource::Range::CreateFromSwap(std::string& content)
+  {
+    boost::shared_ptr<Value> value(new Value(StringMemoryBuffer::CreateFromSwap(content)));
+    return new Range(value);
+  }
+
+
   StorageAreaDataSource::Range* StorageAreaDataSource::ReadRange(DataSourceReader& reader,
                                                                  const std::string& uuid,
                                                                  FileContentType type,
@@ -239,8 +245,7 @@
             }
           }
 
-          boost::shared_ptr<Value> value(new Value(StringMemoryBuffer::CreateFromSwap(content)));
-          return new Range(value);
+          return Range::CreateFromSwap(content);
 #else
           throw OrthancException(ErrorCode_InternalError, "Support for zlib is disabled, cannot uncompress attachment");
 #endif
@@ -280,4 +285,45 @@
     return ReadRange(reader, attachment.GetUuid(), attachment.GetContentType(),
                      0, untilPosition, attachment.GetCustomData());
   }
+
+
+  StorageAreaDataSource::Range* StorageAreaDataSource::ReadRange(DataSourceReader& reader,
+                                                                 const FileInfo& attachment,
+                                                                 const StorageRange& range,
+                                                                 bool uncompress,
+                                                                 bool checkMD5)
+  {
+    // This mimics "StorageAccessor::ReadRange()"
+
+    if (uncompress &&
+        attachment.GetCompressionType() != CompressionType_None)
+    {
+      // An uncompression is needed in this case
+      std::unique_ptr<StorageAreaDataSource::Range> uncompressed(
+        ReadAttachment(reader, attachment, true /* uncompress */, checkMD5));
+
+      std::string content;
+      range.Extract(content, uncompressed->GetData(), uncompressed->GetSize());
+
+      return Range::CreateFromSwap(content);
+    }
+    else
+    {
+      // Access to the raw attachment is sufficient in this case, but
+      // no MD5 checking can be done in general
+      const uint64_t start = range.HasStart() ? range.GetStartInclusive() : 0;
+      const uint64_t endExclusive = range.HasEnd() ? range.GetEndInclusive() + 1 : attachment.GetCompressedSize();
+
+      if (start >= attachment.GetCompressedSize() ||
+          endExclusive > attachment.GetCompressedSize())
+      {
+        throw OrthancException(ErrorCode_BadRange);
+      }
+      else
+      {
+        return ReadRange(reader, attachment.GetUuid(), attachment.GetContentType(),
+                         start, endExclusive, attachment.GetCustomData());
+      }
+    }
+  }
 }
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h	Tue May 19 12:11:01 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h	Tue May 19 12:56:29 2026 +0200
@@ -31,6 +31,7 @@
 #include "../Compatibility.h"
 #include "../FileStorage/FileInfo.h"
 #include "../FileStorage/IStorageArea.h"
+#include "../FileStorage/StorageRange.h"
 #include "IDataSource.h"
 
 #include <boost/shared_ptr.hpp>
@@ -74,6 +75,8 @@
       size_t GetSize() const;
 
       void Copy(std::string& to) const;
+
+      static Range* CreateFromSwap(std::string& content);
     };
 
     static Range* ReadRange(DataSourceReader& reader,
@@ -90,6 +93,12 @@
 
     static Range* ReadBeginning(DataSourceReader& reader,
                                 const FileInfo& attachment,
-                                uint64_t untilPosition);
+                                uint64_t untilPosition /* exclusive */);
+
+    static Range* ReadRange(DataSourceReader& reader,
+                            const FileInfo& attachment,
+                            const StorageRange& range,
+                            bool uncompress,
+                            bool checkMD5);
   };
 }
--- a/OrthancFramework/Sources/FileStorage/StorageRange.cpp	Tue May 19 12:11:01 2026 +0200
+++ b/OrthancFramework/Sources/FileStorage/StorageRange.cpp	Tue May 19 12:56:29 2026 +0200
@@ -33,6 +33,8 @@
 #include <boost/lexical_cast.hpp>
 
 
+#include <signal.h>
+
 namespace Orthanc
 {
   void StorageRange::SanityCheck() const
@@ -131,39 +133,55 @@
     return s + "/" + boost::lexical_cast<std::string>(fullSize);
   }
 
+
   void StorageRange::Extract(std::string &target,
-                                       const std::string &source) const
+                             const std::string &source) const
+  {
+    Extract(target, source.empty() ? NULL : source.c_str(), source.size());
+  }
+
+
+  void StorageRange::Extract(std::string& target,
+                             const void* data,
+                             size_t size) const
   {
     SanityCheck();
 
-    if (hasStart_ && start_ >= source.size())
+    if (size != 0 &&
+        data == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+
+    if (hasStart_ && start_ >= size)
     {
       throw OrthancException(ErrorCode_BadRange);
     }
 
-    if (hasEnd_ && end_ >= source.size())
+    if (hasEnd_ && end_ >= size)
     {
       throw OrthancException(ErrorCode_BadRange);
     }
 
     if (hasStart_ && hasEnd_)
     {
-      target = source.substr(start_, end_ - start_ + 1);
+      target.assign(reinterpret_cast<const char*>(data) + start_, end_ - start_ + 1);
     }
     else if (hasStart_)
     {
-      target = source.substr(start_, source.size() - start_);
+      target.assign(reinterpret_cast<const char*>(data) + start_, size - start_);
     }
     else if (hasEnd_)
     {
-      target = source.substr(0, end_ + 1);
+      target.assign(reinterpret_cast<const char*>(data), end_ + 1);
     }
     else
     {
-      target = source;
+      target.assign(reinterpret_cast<const char*>(data), size);
     }
   }
 
+
   uint64_t StorageRange::GetContentLength(uint64_t fullSize) const
   {
     SanityCheck();
--- a/OrthancFramework/Sources/FileStorage/StorageRange.h	Tue May 19 12:11:01 2026 +0200
+++ b/OrthancFramework/Sources/FileStorage/StorageRange.h	Tue May 19 12:56:29 2026 +0200
@@ -69,6 +69,10 @@
     void Extract(std::string& target,
                  const std::string& source) const;
 
+    void Extract(std::string& target,
+                 const void* data,
+                 size_t size) const;
+
     uint64_t GetContentLength(uint64_t fullSize) const;
 
     static StorageRange ParseHttpRange(const std::string& s);