changeset 215:c07170f3f4f7

refactoring of access to images in REST
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Nov 2012 11:25:29 +0100
parents 03817919169b
children e5d5d4a9a326
files Core/RestApi/RestApi.cpp Core/RestApi/RestApi.h Core/RestApi/RestApiOutput.cpp Core/RestApi/RestApiOutput.h OrthancServer/OrthancRestApi.cpp OrthancServer/OrthancRestApi2.cpp
diffstat 6 files changed, 65 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/Core/RestApi/RestApi.cpp	Thu Nov 29 11:11:53 2012 +0100
+++ b/Core/RestApi/RestApi.cpp	Thu Nov 29 11:25:29 2012 +0100
@@ -182,7 +182,8 @@
           call.httpHeaders_ = &headers;
           call.uriComponents_ = &components;
           call.trailing_ = &trailing;
-           
+          call.fullUri_ = &uri;
+          
           call.getArguments_ = &getArguments;
           it->second(call);
         }
@@ -203,6 +204,7 @@
           call.httpHeaders_ = &headers;
           call.uriComponents_ = &components;
           call.trailing_ = &trailing;
+          call.fullUri_ = &uri;
            
           call.data_ = &postData;
           it->second(call);
@@ -224,6 +226,7 @@
           call.httpHeaders_ = &headers;
           call.uriComponents_ = &components;
           call.trailing_ = &trailing;
+          call.fullUri_ = &uri;
            
           call.data_ = &postData;
           it->second(call);
@@ -245,6 +248,7 @@
           call.httpHeaders_ = &headers;
           call.uriComponents_ = &components;
           call.trailing_ = &trailing;
+          call.fullUri_ = &uri;
           it->second(call);
         }
       }
--- a/Core/RestApi/RestApi.h	Thu Nov 29 11:11:53 2012 +0100
+++ b/Core/RestApi/RestApi.h	Thu Nov 29 11:25:29 2012 +0100
@@ -51,6 +51,7 @@
       const HttpHandler::Arguments* httpHeaders_;
       const RestApiPath::Components* uriComponents_;
       const UriComponents* trailing_;
+      const UriComponents* fullUri_;
 
     public:
       RestApiOutput& GetOutput()
@@ -63,9 +64,9 @@
         return *context_;
       }
     
-      const UriComponents& GetTrailing() const
+      const UriComponents& GetFullUri() const
       {
-        return *trailing_;
+        return *fullUri_;
       }
 
       std::string GetUriComponent(const std::string& name,
--- a/Core/RestApi/RestApiOutput.cpp	Thu Nov 29 11:11:53 2012 +0100
+++ b/Core/RestApi/RestApiOutput.cpp	Thu Nov 29 11:25:29 2012 +0100
@@ -82,7 +82,7 @@
     existingResource_ = true;
   }
 
-  void RestApiOutput::Redirect(const char* path)
+  void RestApiOutput::Redirect(const std::string& path)
   {
     CheckStatus();
     output_.Redirect(path);
--- a/Core/RestApi/RestApiOutput.h	Thu Nov 29 11:11:53 2012 +0100
+++ b/Core/RestApi/RestApiOutput.h	Thu Nov 29 11:25:29 2012 +0100
@@ -59,6 +59,6 @@
     void AnswerBuffer(const std::string& buffer,
                       const std::string& contentType);
 
-    void Redirect(const char* path);
+    void Redirect(const std::string& path);
   };
 }
--- a/OrthancServer/OrthancRestApi.cpp	Thu Nov 29 11:11:53 2012 +0100
+++ b/OrthancServer/OrthancRestApi.cpp	Thu Nov 29 11:25:29 2012 +0100
@@ -372,83 +372,6 @@
       }
     }
 
-
-    // Get the DICOM or the JSON file of one instance ---------------------------
- 
-    else if (uri[0] == "instances" &&
-             ((uri.size() == 3 &&
-               (uri[2] == "preview" || 
-                uri[2] == "image-uint8" || 
-                uri[2] == "image-uint16")) ||
-              (uri.size() == 5 &&
-               uri[2] == "frames" &&
-               (uri[4] == "preview" || 
-                uri[4] == "image-uint8" || 
-                uri[4] == "image-uint16"))))
-    {
-      std::string uuid;
-      CompressionType compressionType;
-      existingResource = index_.GetFile(uuid, compressionType, uri[1], AttachedFileType_Dicom);
-
-      std::string action = uri[2];
-
-      unsigned int frame = 0;
-      if (existingResource &&
-          uri.size() == 5)
-      {
-        // Access to multi-frame image
-        action = uri[4];
-        try
-        {
-          frame = boost::lexical_cast<unsigned int>(uri[3]);
-        }
-        catch (boost::bad_lexical_cast)
-        {
-          existingResource = false;
-        }
-      }
-
-      if (existingResource)
-      {
-        std::string dicomContent, png;
-        storage_.ReadFile(dicomContent, uuid);
-        try
-        {
-          if (action == "preview")
-          {
-            FromDcmtkBridge::ExtractPngImage(png, dicomContent, frame, ImageExtractionMode_Preview);
-          }
-          else if (action == "image-uint8")
-          {
-            FromDcmtkBridge::ExtractPngImage(png, dicomContent, frame, ImageExtractionMode_UInt8);
-          }
-          else if (action == "image-uint16")
-          {
-            FromDcmtkBridge::ExtractPngImage(png, dicomContent, frame, ImageExtractionMode_UInt16);
-          }
-          else
-          {
-            throw OrthancException(ErrorCode_InternalError);
-          }
-
-          output.AnswerBufferWithContentType(png, "image/png");
-          return;
-        }
-        catch (OrthancException&)
-        {
-          std::string root = "";
-          for (size_t i = 1; i < uri.size(); i++)
-          {
-            root += "../";
-          }
-
-          output.Redirect(root + "app/images/unsupported.png");
-          return;
-        }
-      }
-    }
-
-
     // DICOM bridge -------------------------------------------------------------
 
     if ((uri.size() == 2 ||
--- a/OrthancServer/OrthancRestApi2.cpp	Thu Nov 29 11:11:53 2012 +0100
+++ b/OrthancServer/OrthancRestApi2.cpp	Thu Nov 29 11:25:29 2012 +0100
@@ -223,6 +223,53 @@
   }
 
 
+  template <enum ImageExtractionMode mode>
+  static void GetImage(RestApi::GetCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+
+    CompressionType compressionType;
+    std::string fileUuid;
+    std::string publicId = call.GetUriComponent("id", "");
+    std::string frameId = call.GetUriComponent("frame", "0");
+
+    unsigned int frame;
+    try
+    {
+      frame = boost::lexical_cast<unsigned int>(frameId);
+    }
+    catch (boost::bad_lexical_cast)
+    {
+      return;
+    }
+
+    if (context.GetIndex().GetFile(fileUuid, compressionType, publicId, AttachedFileType_Dicom))
+    {
+      assert(compressionType == CompressionType_None);
+
+      std::string dicomContent, png;
+      context.GetFileStorage().ReadFile(dicomContent, fileUuid);
+
+      try
+      {
+        FromDcmtkBridge::ExtractPngImage(png, dicomContent, frame, mode);
+        call.GetOutput().AnswerBuffer(png, "image/png");
+      }
+      catch (OrthancException&)
+      {
+        std::string root = "";
+        for (size_t i = 1; i < call.GetFullUri().size(); i++)
+        {
+          root += "../";
+        }
+
+        call.GetOutput().Redirect(root + "app/images/unsupported.png");
+      }
+    }
+  }
+
+
+
 
   // DICOM bridge -------------------------------------------------------------
 
@@ -277,6 +324,13 @@
     Register("/instances/{id}/simplified-tags", GetInstanceTags<true>);
     Register("/instances/{id}/frames", ListFrames);
 
-    // TODO : "content", "frames"
+    Register("/instances/{id}/frames/{frame}/preview", GetImage<ImageExtractionMode_Preview>);
+    Register("/instances/{id}/frames/{frame}/image-uint8", GetImage<ImageExtractionMode_UInt8>);
+    Register("/instances/{id}/frames/{frame}/image-uint16", GetImage<ImageExtractionMode_UInt16>);
+    Register("/instances/{id}/preview", GetImage<ImageExtractionMode_Preview>);
+    Register("/instances/{id}/image-uint8", GetImage<ImageExtractionMode_UInt8>);
+    Register("/instances/{id}/image-uint16", GetImage<ImageExtractionMode_UInt16>);
+
+    // TODO : "content"
   }
 }