Mercurial > hg > orthanc
diff Core/FileStorage/StorageAccessor.cpp @ 1549:e5e975e9b738
refactoring and simplification of StorageAccessor
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 17 Aug 2015 10:47:04 +0200 |
parents | 6e7e5ed91c2d |
children | 2c7d5eb588e6 |
line wrap: on
line diff
--- a/Core/FileStorage/StorageAccessor.cpp Fri Aug 14 11:52:03 2015 +0200 +++ b/Core/FileStorage/StorageAccessor.cpp Mon Aug 17 10:47:04 2015 +0200 @@ -33,31 +33,126 @@ #include "../PrecompiledHeaders.h" #include "StorageAccessor.h" +#include "../Compression/ZlibCompressor.h" +#include "../OrthancException.h" +#include "../HttpServer/HttpStreamTranscoder.h" + namespace Orthanc { - FileInfo StorageAccessor::Write(const std::vector<uint8_t>& content, - FileContentType type) + FileInfo StorageAccessor::Write(const void* data, + size_t size, + FileContentType type, + CompressionType compression, + bool storeMd5) { - if (content.size() == 0) + std::string uuid = Toolbox::GenerateUuid(); + + std::string md5; + + if (storeMd5) + { + Toolbox::ComputeMD5(md5, data, size); + } + + switch (compression) { - return WriteInternal(NULL, 0, type); - } - else - { - return WriteInternal(&content[0], content.size(), type); + case CompressionType_None: + { + area_.Create(uuid, data, size, type); + return FileInfo(uuid, type, size, md5); + } + + case CompressionType_ZlibWithSize: + { + ZlibCompressor zlib; + + std::string compressed; + zlib.Compress(compressed, data, size); + + std::string compressedMD5; + + if (storeMd5) + { + Toolbox::ComputeMD5(compressedMD5, compressed); + } + + if (compressed.size() > 0) + { + area_.Create(uuid, &compressed[0], compressed.size(), type); + } + else + { + area_.Create(uuid, NULL, 0, type); + } + + return FileInfo(uuid, type, size, md5, + CompressionType_ZlibWithSize, compressed.size(), compressedMD5); + } + + default: + throw OrthancException(ErrorCode_NotImplemented); } } - FileInfo StorageAccessor::Write(const std::string& content, - FileContentType type) + + void StorageAccessor::Read(std::string& content, + const FileInfo& info) { - if (content.size() == 0) + switch (info.GetCompressionType()) { - return WriteInternal(NULL, 0, type); + case CompressionType_None: + { + area_.Read(content, info.GetUuid(), info.GetContentType()); + break; + } + + case CompressionType_ZlibWithSize: + { + ZlibCompressor zlib; + + std::string compressed; + area_.Read(compressed, info.GetUuid(), info.GetContentType()); + IBufferCompressor::Uncompress(content, zlib, compressed); + break; + } + + default: + { + throw OrthancException(ErrorCode_NotImplemented); + } } - else - { - return WriteInternal(&content[0], content.size(), type); - } + + // TODO Check the validity of the uncompressed MD5? + } + + + void StorageAccessor::SetupSender(BufferHttpSender& sender, + const FileInfo& info) + { + Read(sender.GetBuffer(), info); + sender.SetContentType(GetMimeType(info.GetContentType())); + sender.SetContentFilename(info.GetUuid() + std::string(GetFileExtension(info.GetContentType()))); + } + + + void StorageAccessor::AnswerFile(HttpOutput& output, + const FileInfo& info) + { + BufferHttpSender sender; + SetupSender(sender, info); + + HttpStreamTranscoder transcoder(sender, info.GetCompressionType()); + output.Answer(transcoder); + } + + + void StorageAccessor::AnswerFile(RestApiOutput& output, + const FileInfo& info) + { + BufferHttpSender sender; + SetupSender(sender, info); + + HttpStreamTranscoder transcoder(sender, info.GetCompressionType()); + output.AnswerStream(transcoder); } }