diff OrthancServer/ServerContext.cpp @ 226:8a26a8e85edf

refactoring to read files
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 30 Nov 2012 09:45:29 +0100
parents 4eb0c7ce86c9
children 209ca3f6db62
line wrap: on
line diff
--- a/OrthancServer/ServerContext.cpp	Thu Nov 29 22:28:05 2012 +0100
+++ b/OrthancServer/ServerContext.cpp	Fri Nov 30 09:45:29 2012 +0100
@@ -32,13 +32,15 @@
 
 #include "ServerContext.h"
 
+#include "../Core/HttpServer/FilesystemHttpSender.h"
+
 #include <glog/logging.h>
 
 namespace Orthanc
 {
   ServerContext::ServerContext(const boost::filesystem::path& path) :
     storage_(path.string()),
-    index_(storage_, path.string())
+    index_(*this, path.string())
   {
   }
 
@@ -75,4 +77,47 @@
 
     return status;
   }
+
+  
+  void ServerContext::AnswerFile(RestApiOutput& output,
+                                 const std::string& instancePublicId,
+                                 AttachedFileType content)
+  {
+    CompressionType compressionType;
+    std::string fileUuid;
+
+    if (index_.GetFile(fileUuid, compressionType, 
+                       instancePublicId, AttachedFileType_Dicom))
+    {
+      assert(compressionType == CompressionType_None);
+
+      FilesystemHttpSender sender(storage_, fileUuid);
+      sender.SetDownloadFilename(fileUuid + ".dcm");
+      sender.SetContentType("application/dicom");
+      output.AnswerFile(sender);
+    }
+  }
+
+
+  void ServerContext::ReadJson(Json::Value& result,
+                               const std::string& instancePublicId)
+  {
+    CompressionType compressionType;
+    std::string fileUuid;
+    if (!index_.GetFile(fileUuid, compressionType, instancePublicId, AttachedFileType_Json))
+    {
+      throw OrthancException(ErrorCode_InternalError);
+    }
+
+    assert(compressionType == CompressionType_None);
+
+    std::string s;
+    storage_.ReadFile(s, fileUuid);
+
+    Json::Reader reader;
+    if (!reader.parse(s, result))
+    {
+      throw OrthancException("Corrupted JSON file");
+    }
+  }
 }