changeset 1146:200fcac0deb4

optimization for access to attachments
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 11 Sep 2014 14:04:48 +0200
parents 0479d02c6778
children ae9a83a6fa47
files Core/Enumerations.cpp Core/Enumerations.h OrthancServer/OrthancRestApi/OrthancRestResources.cpp OrthancServer/ServerContext.cpp OrthancServer/ServerContext.h
diffstat 5 files changed, 42 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Enumerations.cpp	Thu Sep 11 13:06:16 2014 +0200
+++ b/Core/Enumerations.cpp	Thu Sep 11 14:04:48 2014 +0200
@@ -549,4 +549,20 @@
     // The encoding was properly detected
     return true;
   }
+
+
+  const char* GetMimeType(FileContentType type)
+  {
+    switch (type)
+    {
+      case FileContentType_Dicom:
+        return "application/dicom";
+
+      case FileContentType_DicomAsJson:
+        return "application/json";
+
+      default:
+        return "application/octet-stream";
+    }
+  }
 }
--- a/Core/Enumerations.h	Thu Sep 11 13:06:16 2014 +0200
+++ b/Core/Enumerations.h	Thu Sep 11 14:04:48 2014 +0200
@@ -308,4 +308,6 @@
 
   bool GetDicomEncoding(Encoding& encoding,
                         const char* specificCharacterSet);
+
+  const char* GetMimeType(FileContentType type);
 }
--- a/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Thu Sep 11 13:06:16 2014 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Thu Sep 11 14:04:48 2014 +0200
@@ -112,7 +112,7 @@
     ServerContext& context = OrthancRestApi::GetContext(call);
 
     std::string publicId = call.GetUriComponent("id", "");
-    context.AnswerDicomFile(call.GetOutput(), publicId, FileContentType_Dicom);
+    context.AnswerAttachment(call.GetOutput(), publicId, FileContentType_Dicom);
   }
 
 
@@ -138,18 +138,18 @@
 
     std::string publicId = call.GetUriComponent("id", "");
     
-    Json::Value full;
-    context.ReadJson(full, publicId);
-
     if (simplify)
     {
+      Json::Value full;
+      context.ReadJson(full, publicId);
+
       Json::Value simplified;
       SimplifyTags(simplified, full);
       call.GetOutput().AnswerJson(simplified);
     }
     else
     {
-      call.GetOutput().AnswerJson(full);
+      context.AnswerAttachment(call.GetOutput(), publicId, FileContentType_DicomAsJson);
     }
   }
 
@@ -449,13 +449,19 @@
     CheckValidResourceType(call);
  
     std::string publicId = call.GetUriComponent("id", "");
-    std::string name = call.GetUriComponent("name", "");
+    FileContentType type = StringToContentType(call.GetUriComponent("name", ""));
 
-    std::string content;
-    context.ReadFile(content, publicId, StringToContentType(name),
-                     (uncompress == 1));
-
-    call.GetOutput().AnswerBuffer(content, "application/octet-stream");
+    if (uncompress)
+    {
+      context.AnswerAttachment(call.GetOutput(), publicId, type);
+    }
+    else
+    {
+      // Return the raw data (possibly compressed), as stored on the filesystem
+      std::string content;
+      context.ReadFile(content, publicId, type, false);
+      call.GetOutput().AnswerBuffer(content, "application/octet-stream");
+    }
   }
 
 
--- a/OrthancServer/ServerContext.cpp	Thu Sep 11 13:06:16 2014 +0200
+++ b/OrthancServer/ServerContext.cpp	Thu Sep 11 14:04:48 2014 +0200
@@ -391,9 +391,9 @@
 
 
 
-  void ServerContext::AnswerDicomFile(RestApiOutput& output,
-                                      const std::string& instancePublicId,
-                                      FileContentType content)
+  void ServerContext::AnswerAttachment(RestApiOutput& output,
+                                       const std::string& instancePublicId,
+                                       FileContentType content)
   {
     FileInfo attachment;
     if (!index_.LookupAttachment(attachment, instancePublicId, content))
@@ -404,7 +404,7 @@
     accessor_.SetCompressionForNextOperations(attachment.GetCompressionType());
 
     std::auto_ptr<HttpFileSender> sender(accessor_.ConstructHttpFileSender(attachment.GetUuid(), attachment.GetContentType()));
-    sender->SetContentType("application/dicom");
+    sender->SetContentType(GetMimeType(content));
     sender->SetDownloadFilename(instancePublicId + ".dcm");
     output.AnswerFile(*sender);
   }
--- a/OrthancServer/ServerContext.h	Thu Sep 11 13:06:16 2014 +0200
+++ b/OrthancServer/ServerContext.h	Thu Sep 11 14:04:48 2014 +0200
@@ -164,9 +164,9 @@
     StoreStatus Store(std::string& resultPublicId,
                       DicomInstanceToStore& dicom);
 
-    void AnswerDicomFile(RestApiOutput& output,
-                         const std::string& instancePublicId,
-                         FileContentType content);
+    void AnswerAttachment(RestApiOutput& output,
+                          const std::string& instancePublicId,
+                          FileContentType content);
 
     void ReadJson(Json::Value& result,
                   const std::string& instancePublicId);