# HG changeset patch # User Sebastien Jodogne # Date 1779188189 -7200 # Node ID b60fb4f30c4b96b051d9afe0fcfff430c429a3c5 # Parent 6df5cb04dd7f43ad189306be291d298a5a8ab6f3 added support for StorageRange in StorageAreaDataSource diff -r 6df5cb04dd7f -r b60fb4f30c4b OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp --- 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 #include - namespace Orthanc { class StorageAreaDataSource::Value : public IDynamicObject @@ -169,6 +168,13 @@ } + StorageAreaDataSource::Range* StorageAreaDataSource::Range::CreateFromSwap(std::string& content) + { + boost::shared_ptr 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(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 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()); + } + } + } } diff -r 6df5cb04dd7f -r b60fb4f30c4b OrthancFramework/Sources/DataSource/StorageAreaDataSource.h --- 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 @@ -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); }; } diff -r 6df5cb04dd7f -r b60fb4f30c4b OrthancFramework/Sources/FileStorage/StorageRange.cpp --- 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 +#include + namespace Orthanc { void StorageRange::SanityCheck() const @@ -131,39 +133,55 @@ return s + "/" + boost::lexical_cast(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(data) + start_, end_ - start_ + 1); } else if (hasStart_) { - target = source.substr(start_, source.size() - start_); + target.assign(reinterpret_cast(data) + start_, size - start_); } else if (hasEnd_) { - target = source.substr(0, end_ + 1); + target.assign(reinterpret_cast(data), end_ + 1); } else { - target = source; + target.assign(reinterpret_cast(data), size); } } + uint64_t StorageRange::GetContentLength(uint64_t fullSize) const { SanityCheck(); diff -r 6df5cb04dd7f -r b60fb4f30c4b OrthancFramework/Sources/FileStorage/StorageRange.h --- 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);