# HG changeset patch # User Sebastien Jodogne # Date 1444404026 -7200 # Node ID f5ddbd9239dd1911dbb2e7f1244ce39a01febfcb # Parent 8ca0e89798b2a41dfb53fb1b27a38fe2529a2d8e New URIs for attachments: ".../compress", ".../uncompress" and ".../is-compressed" diff -r 8ca0e89798b2 -r f5ddbd9239dd NEWS --- a/NEWS Fri Oct 09 13:31:22 2015 +0200 +++ b/NEWS Fri Oct 09 17:20:26 2015 +0200 @@ -5,6 +5,7 @@ * "/tools/create-dicom": Support of binary tags encoded using data URI scheme * "/tools/create-dicom": Support of hierarchical structures (creation of sequences) * "/modify" can insert/modify sequences +* New URIs for attachments: ".../compress", ".../uncompress" and ".../is-compressed" Plugins ------- diff -r 8ca0e89798b2 -r f5ddbd9239dd OrthancServer/OrthancRestApi/OrthancRestResources.cpp --- a/OrthancServer/OrthancRestApi/OrthancRestResources.cpp Fri Oct 09 13:31:22 2015 +0200 +++ b/OrthancServer/OrthancRestApi/OrthancRestResources.cpp Fri Oct 09 17:20:26 2015 +0200 @@ -479,6 +479,7 @@ { Json::Value operations = Json::arrayValue; + operations.append("compress"); operations.append("compressed-data"); if (info.GetCompressedMD5() != "") @@ -488,6 +489,7 @@ operations.append("compressed-size"); operations.append("data"); + operations.append("is-compressed"); if (info.GetUncompressedMD5() != "") { @@ -495,6 +497,7 @@ } operations.append("size"); + operations.append("uncompress"); if (info.GetCompressedMD5() != "" && info.GetUncompressedMD5() != "") @@ -663,6 +666,31 @@ } + template + static void ChangeAttachmentCompression(RestApiPostCall& call) + { + CheckValidResourceType(call); + + std::string publicId = call.GetUriComponent("id", ""); + std::string name = call.GetUriComponent("name", ""); + FileContentType contentType = StringToContentType(name); + + OrthancRestApi::GetContext(call).ChangeAttachmentCompression(publicId, contentType, compression); + call.GetOutput().AnswerBuffer("{}", "application/json"); + } + + + static void IsAttachmentCompressed(RestApiGetCall& call) + { + FileInfo info; + if (GetAttachmentInfo(info, call)) + { + std::string answer = (info.GetCompressionType() == CompressionType_None) ? "0" : "1"; + call.GetOutput().AnswerBuffer(answer, "text/plain"); + } + } + + // Raw access to the DICOM tags of an instance ------------------------------ static void GetRawContent(RestApiGetCall& call) @@ -1125,14 +1153,17 @@ Register("/{resourceType}/{id}/attachments", ListAttachments); Register("/{resourceType}/{id}/attachments/{name}", DeleteAttachment); Register("/{resourceType}/{id}/attachments/{name}", GetAttachmentOperations); + Register("/{resourceType}/{id}/attachments/{name}", UploadAttachment); + Register("/{resourceType}/{id}/attachments/{name}/compress", ChangeAttachmentCompression); Register("/{resourceType}/{id}/attachments/{name}/compressed-data", GetAttachmentData<0>); Register("/{resourceType}/{id}/attachments/{name}/compressed-md5", GetAttachmentCompressedMD5); Register("/{resourceType}/{id}/attachments/{name}/compressed-size", GetAttachmentCompressedSize); Register("/{resourceType}/{id}/attachments/{name}/data", GetAttachmentData<1>); + Register("/{resourceType}/{id}/attachments/{name}/is-compressed", IsAttachmentCompressed); Register("/{resourceType}/{id}/attachments/{name}/md5", GetAttachmentMD5); Register("/{resourceType}/{id}/attachments/{name}/size", GetAttachmentSize); + Register("/{resourceType}/{id}/attachments/{name}/uncompress", ChangeAttachmentCompression); Register("/{resourceType}/{id}/attachments/{name}/verify-md5", VerifyAttachment); - Register("/{resourceType}/{id}/attachments/{name}", UploadAttachment); Register("/tools/lookup", Lookup); Register("/tools/find", Find); diff -r 8ca0e89798b2 -r f5ddbd9239dd OrthancServer/ServerContext.cpp --- a/OrthancServer/ServerContext.cpp Fri Oct 09 13:31:22 2015 +0200 +++ b/OrthancServer/ServerContext.cpp Fri Oct 09 17:20:26 2015 +0200 @@ -307,15 +307,14 @@ } - void ServerContext::AnswerAttachment(RestApiOutput& output, - const std::string& instancePublicId, + const std::string& resourceId, FileContentType content) { FileInfo attachment; - if (!index_.LookupAttachment(attachment, instancePublicId, content)) + if (!index_.LookupAttachment(attachment, resourceId, content)) { - throw OrthancException(ErrorCode_InternalError); + throw OrthancException(ErrorCode_UnknownResource); } StorageAccessor accessor(area_); @@ -323,6 +322,52 @@ } + void ServerContext::ChangeAttachmentCompression(const std::string& resourceId, + FileContentType attachmentType, + CompressionType compression) + { + LOG(INFO) << "Changing compression type for attachment " + << EnumerationToString(attachmentType) + << " of resource " << resourceId << " to " + << compression; + + FileInfo attachment; + if (!index_.LookupAttachment(attachment, resourceId, attachmentType)) + { + throw OrthancException(ErrorCode_UnknownResource); + } + + if (attachment.GetCompressionType() == compression) + { + // Nothing to do + return; + } + + std::string content; + + StorageAccessor accessor(area_); + accessor.Read(content, attachment); + + FileInfo modified = accessor.Write(content.empty() ? NULL : content.c_str(), + content.size(), attachmentType, compression, storeMD5_); + + try + { + StoreStatus status = index_.AddAttachment(modified, resourceId); + if (status != StoreStatus_Success) + { + accessor.Remove(modified); + throw OrthancException(ErrorCode_Database); + } + } + catch (OrthancException&) + { + accessor.Remove(modified); + throw; + } + } + + void ServerContext::ReadJson(Json::Value& result, const std::string& instancePublicId) { diff -r 8ca0e89798b2 -r f5ddbd9239dd OrthancServer/ServerContext.h --- a/OrthancServer/ServerContext.h Fri Oct 09 13:31:22 2015 +0200 +++ b/OrthancServer/ServerContext.h Fri Oct 09 17:20:26 2015 +0200 @@ -184,9 +184,13 @@ DicomInstanceToStore& dicom); void AnswerAttachment(RestApiOutput& output, - const std::string& instancePublicId, + const std::string& resourceId, FileContentType content); + void ChangeAttachmentCompression(const std::string& resourceId, + FileContentType attachmentType, + CompressionType compression); + void ReadJson(Json::Value& result, const std::string& instancePublicId);