changeset 1700:f5ddbd9239dd

New URIs for attachments: ".../compress", ".../uncompress" and ".../is-compressed"
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 09 Oct 2015 17:20:26 +0200
parents 8ca0e89798b2
children 4aaaecae5803 77d3e3a13c10
files NEWS OrthancServer/OrthancRestApi/OrthancRestResources.cpp OrthancServer/ServerContext.cpp OrthancServer/ServerContext.h
diffstat 4 files changed, 87 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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
 -------
--- 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 <enum CompressionType compression>
+  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<CompressionType_ZlibWithSize>);
     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<CompressionType_None>);
     Register("/{resourceType}/{id}/attachments/{name}/verify-md5", VerifyAttachment);
-    Register("/{resourceType}/{id}/attachments/{name}", UploadAttachment);
 
     Register("/tools/lookup", Lookup);
     Register("/tools/find", Find);
--- 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)
   {
--- 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);