changeset 483:8c3573d28868

export dicom instances to the filesystem
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 16 Jul 2013 12:01:39 +0200
parents b05eb8708aee
children b8ace6fc1d1f
files Core/Toolbox.cpp Core/Toolbox.h OrthancServer/OrthancRestApi.cpp UnitTests/main.cpp
diffstat 4 files changed, 70 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Tue Jul 16 11:30:24 2013 +0200
+++ b/Core/Toolbox.cpp	Tue Jul 16 12:01:39 2013 +0200
@@ -208,10 +208,10 @@
                          const std::string& path) 
   {
     boost::filesystem::ifstream f;
-    f.open(path, std::ifstream::in | std::ios::binary);
+    f.open(path, std::ifstream::in | std::ifstream::binary);
     if (!f.good())
     {
-      throw OrthancException("Unable to open a file");
+      throw OrthancException(ErrorCode_InexistentFile);
     }
 
     // http://www.cplusplus.com/reference/iostream/istream/tellg/
@@ -229,6 +229,26 @@
   }
 
 
+  void Toolbox::WriteFile(const std::string& content,
+                          const std::string& path)
+  {
+    boost::filesystem::ofstream f;
+    f.open(path, std::ofstream::binary);
+    if (!f.good())
+    {
+      throw OrthancException(ErrorCode_CannotWriteFile);
+    }
+
+    if (content.size() != 0)
+    {
+      f.write(content.c_str(), content.size());
+    }
+
+    f.close();
+  }
+
+
+
   void Toolbox::RemoveFile(const std::string& path)
   {
     if (boost::filesystem::exists(path))
--- a/Core/Toolbox.h	Tue Jul 16 11:30:24 2013 +0200
+++ b/Core/Toolbox.h	Tue Jul 16 12:01:39 2013 +0200
@@ -57,6 +57,9 @@
     void ReadFile(std::string& content,
                   const std::string& path);
 
+    void WriteFile(const std::string& content,
+                   const std::string& path);
+
     void Sleep(uint32_t seconds);
 
     void USleep(uint64_t microSeconds);
--- a/OrthancServer/OrthancRestApi.cpp	Tue Jul 16 11:30:24 2013 +0200
+++ b/OrthancServer/OrthancRestApi.cpp	Tue Jul 16 12:01:39 2013 +0200
@@ -735,6 +735,21 @@
   }
 
 
+  static void ExportInstanceFile(RestApi::PostCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+
+    std::string publicId = call.GetUriComponent("id", "");
+
+    std::string dicom;
+    context.ReadFile(dicom, publicId, FileContentType_Dicom);
+
+    Toolbox::WriteFile(dicom, call.GetPostBody());
+
+    call.GetOutput().AnswerBuffer("{}", "application/json");
+  }
+
+
   template <bool simplify>
   static void GetInstanceTags(RestApi::GetCall& call)
   {
@@ -1654,6 +1669,7 @@
     Register("/patients/{id}/protected", IsProtectedPatient);
     Register("/patients/{id}/protected", SetPatientProtection);
     Register("/instances/{id}/file", GetInstanceFile);
+    Register("/instances/{id}/export", ExportInstanceFile);
     Register("/instances/{id}/tags", GetInstanceTags<false>);
     Register("/instances/{id}/simplified-tags", GetInstanceTags<true>);
     Register("/instances/{id}/frames", ListFrames);
--- a/UnitTests/main.cpp	Tue Jul 16 11:30:24 2013 +0200
+++ b/UnitTests/main.cpp	Tue Jul 16 12:01:39 2013 +0200
@@ -451,6 +451,35 @@
 }
 
 
+TEST(Toolbox, WriteFile)
+{
+  std::string path;
+
+  {
+    Toolbox::TemporaryFile tmp;
+    path = tmp.GetPath();
+
+    std::string s;
+    s.append("Hello");
+    s.push_back('\0');
+    s.append("World");
+    ASSERT_EQ(11u, s.size());
+
+    Toolbox::WriteFile(s, path.c_str());
+
+    std::string t;
+    Toolbox::ReadFile(t, path.c_str());
+
+    ASSERT_EQ(11u, t.size());
+    ASSERT_EQ(0, t[5]);
+    ASSERT_EQ(0, memcmp(s.c_str(), t.c_str(), s.size()));
+  }
+
+  std::string u;
+  ASSERT_THROW(Toolbox::ReadFile(u, path.c_str()), OrthancException);
+}
+
+
 int main(int argc, char **argv)
 {
   // Initialize Google's logging library.