diff OrthancServer/ServerContext.cpp @ 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 eb8fbcf008b5
children 4aaaecae5803
line wrap: on
line diff
--- 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)
   {