changeset 248:2442033911d6

creation of zip files
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 04 Dec 2012 17:49:37 +0100
parents c9b3ba0fd140
children 5694365ecb96
files Core/Compression/HierarchicalZipWriter.cpp OrthancServer/OrthancRestApi.cpp
diffstat 2 files changed, 61 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Compression/HierarchicalZipWriter.cpp	Tue Dec 04 17:27:23 2012 +0100
+++ b/Core/Compression/HierarchicalZipWriter.cpp	Tue Dec 04 17:49:37 2012 +0100
@@ -59,7 +59,9 @@
             result.push_back(' ');
           }
         }
-        else if (isalnum(source[i]))
+        else if (isalnum(source[i]) || 
+                 source[i] == '.' || 
+                 source[i] == '_')
         {
           result.push_back(source[i]);
           lastSpace = false;
--- a/OrthancServer/OrthancRestApi.cpp	Tue Dec 04 17:27:23 2012 +0100
+++ b/OrthancServer/OrthancRestApi.cpp	Tue Dec 04 17:49:37 2012 +0100
@@ -34,6 +34,7 @@
 
 #include "../Core/HttpServer/FilesystemHttpSender.h"
 #include "../Core/Uuid.h"
+#include "../Core/Compression/HierarchicalZipWriter.h"
 #include "DicomProtocol/DicomUserConnection.h"
 #include "FromDcmtkBridge.h"
 #include "OrthancInitialization.h"
@@ -350,6 +351,62 @@
   }
 
 
+  // Download of ZIP files ----------------------------------------------------
+ 
+  static void GetPatientArchive(RestApi::GetCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+
+    Json::Value patient;
+    if (!context.GetIndex().LookupResource(patient, call.GetUriComponent("id", ""), ResourceType_Patient))
+    {
+      return;
+    }
+
+    Toolbox::TemporaryFile tmp;
+
+    {
+      HierarchicalZipWriter writer(tmp.GetPath().c_str());
+      
+      for (size_t i = 0; i < patient["Studies"].size(); i++)
+      {
+        Json::Value study;
+        if (context.GetIndex().LookupResource(study, patient["Studies"][i].asString(), ResourceType_Study))
+        {
+          writer.CreateDirectory(study["MainDicomTags"]["StudyDescription"].asString().c_str());
+
+          for (size_t i = 0; i < study["Series"].size(); i++)
+          {
+            Json::Value series;
+            if (context.GetIndex().LookupResource(series, study["Series"][i].asString(), ResourceType_Series))
+            {
+              std::string m = series["MainDicomTags"]["Modality"].asString();
+              std::string s = series["MainDicomTags"]["SeriesDescription"].asString();
+              writer.CreateDirectory((m + " " + s).c_str());
+
+              for (size_t i = 0; i < series["Instances"].size(); i++)
+              {
+                Json::Value instance;
+                if (context.GetIndex().LookupResource(instance, series["Instances"][i].asString(), ResourceType_Instance))
+                {
+                  writer.CreateFile(instance["MainDicomTags"]["SOPInstanceUID"].asString().c_str());
+                }
+              }
+
+              writer.CloseDirectory();
+            }
+          }
+
+          writer.CloseDirectory();
+        }
+      }
+    }
+    
+    FilesystemHttpSender sender(tmp.GetPath().c_str());
+    call.GetOutput().AnswerFile(sender);
+  }
+
+
   // Changes API --------------------------------------------------------------
  
   static void GetSinceAndLimit(int64_t& since,
@@ -653,6 +710,7 @@
     Register("/series/{id}", GetSingleResource<ResourceType_Series>);
     Register("/studies/{id}", DeleteSingleResource<ResourceType_Study>);
     Register("/studies/{id}", GetSingleResource<ResourceType_Study>);
+    Register("/patients/{id}/archive", GetPatientArchive);
 
     Register("/instances/{id}/file", GetInstanceFile);
     Register("/instances/{id}/tags", GetInstanceTags<false>);