changeset 1597:415dfd1d1c61

Improvements to the sample "ServeFolders" plugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 28 Aug 2015 13:47:23 +0200
parents f2e3d030ea59
children c6b50b803387
files NEWS OrthancServer/OrthancRestApi/OrthancRestSystem.cpp Plugins/Samples/ServeFolders/Plugin.cpp
diffstat 3 files changed, 67 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Thu Aug 27 15:36:43 2015 +0200
+++ b/NEWS	Fri Aug 28 13:47:23 2015 +0200
@@ -27,6 +27,7 @@
 * New function "OrthancPluginSendHttpStatus()" to send HTTP status with a body
 * New function "OrthancPluginRegisterRestCallbackNoLock()" for high-performance plugins
 * Plugins have access to explicit error codes 
+* Improvements to the sample "ServeFolders" plugin
 
 Maintenance
 -----------
--- a/OrthancServer/OrthancRestApi/OrthancRestSystem.cpp	Thu Aug 27 15:36:43 2015 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestSystem.cpp	Fri Aug 28 13:47:23 2015 +0200
@@ -167,7 +167,17 @@
       const char *c = plugins.GetProperty(id.c_str(), _OrthancPluginProperty_RootUri);
       if (c != NULL)
       {
-        v["RootUri"] = c;
+        std::string root = c;
+        if (!root.empty())
+        {
+          // Turn the root URI into a URI relative to "/app/explorer.js"
+          if (root[0] == '/')
+          {
+            root = ".." + root;
+          }
+
+          v["RootUri"] = root;
+        }
       }
 
       c = plugins.GetProperty(id.c_str(), _OrthancPluginProperty_Description);
--- a/Plugins/Samples/ServeFolders/Plugin.cpp	Thu Aug 27 15:36:43 2015 +0200
+++ b/Plugins/Samples/ServeFolders/Plugin.cpp	Fri Aug 28 13:47:23 2015 +0200
@@ -156,61 +156,12 @@
 }
 
 
-static int32_t IndexCallback(OrthancPluginRestOutput* output,
-                             const char* url,
-                             const OrthancPluginHttpRequest* request)
-{
-  namespace fs = boost::filesystem;
-
-  if (request->method != OrthancPluginHttpMethod_Get)
-  {
-    OrthancPluginSendMethodNotAllowed(context_, output, "GET");
-    return 0;
-  }
-
-  std::string folder;
-  if (LookupFolder(folder, output, request))
-  {
-    std::string baseUri = "/" + std::string(request->groups[0]);
-
-    if (fs::is_regular_file(fs::path(folder) / "index.html"))
-    {
-      std::string s = baseUri + "/index.html";
-      OrthancPluginRedirect(context_, output, s.c_str());
-    }
-    else
-    {
-      std::string s;
-      s += "<html>\n";
-      s += "  <body>\n";
-      s += "    <ul>\n";
-
-      fs::directory_iterator end;
-      for (fs::directory_iterator it(folder) ; it != end; ++it)
-      {
-        if (fs::is_regular_file(it->status()))
-        {
-          std::string f = it->path().filename().string();
-          s += "      <li><a href=\"" + baseUri + "/" + f + "\">" + f + "</a></li>";
-        }
-      }
-
-      s += "    </ul>\n";
-      s += "  </body>\n";
-      s += "</html>\n";
-
-      OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html");
-    }
-  }
-
-  return 0;
-}
-
-
 static int32_t FolderCallback(OrthancPluginRestOutput* output,
                               const char* url,
                               const OrthancPluginHttpRequest* request)
 {
+  namespace fs = boost::filesystem;  
+
   if (request->method != OrthancPluginHttpMethod_Get)
   {
     OrthancPluginSendMethodNotAllowed(context_, output, "GET");
@@ -221,22 +172,62 @@
 
   if (LookupFolder(folder, output, request))
   {
-    const std::string item = request->groups[1];
+    const fs::path item(request->groups[1]);
+    const fs::path parent((fs::path(folder) / item).parent_path());
 
-    std::string path = folder + "/" + item;
-    const char* mime = GetMimeType(path);
+    if (item.filename().string() == "index.html" &&
+        fs::is_directory(parent) &&
+        !fs::is_regular_file(fs::path(folder) / item))
+    {
+      // On-the-fly generation of an "index.html" 
+      std::string s;
+      s += "<html>\n";
+      s += "  <body>\n";
+      s += "    <ul>\n";
+
+      fs::directory_iterator end;
 
-    std::string s;
-    if (ReadFile(s, path))
-    {
-      const char* resource = s.size() ? s.c_str() : NULL;
-      OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime);
+      for (fs::directory_iterator it(parent) ; it != end; ++it)
+      {
+        if (fs::is_directory(it->status()))
+        {
+          std::string f = it->path().filename().string();
+          s += "      <li><a href=\"" + f + "/index.html\">" + f + "/</a></li>\n";
+        }
+      }
+
+      for (fs::directory_iterator it(parent) ; it != end; ++it)
+      {
+        if (fs::is_regular_file(it->status()))
+        {
+          std::string f = it->path().filename().string();
+          s += "      <li><a href=\"" + f + "\">" + f + "</a></li>\n";
+        }
+      }
+
+      s += "    </ul>\n";
+      s += "  </body>\n";
+      s += "</html>\n";
+
+      OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html");
     }
     else
     {
-      std::string s = "Inexistent file in served folder: " + path;
-      OrthancPluginLogError(context_, s.c_str());
-      OrthancPluginSendHttpStatusCode(context_, output, 404);
+      std::string path = folder + "/" + item.string();
+      const char* mime = GetMimeType(path);
+
+      std::string s;
+      if (ReadFile(s, path))
+      {
+        const char* resource = s.size() ? s.c_str() : NULL;
+        OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime);
+      }
+      else
+      {
+        std::string s = "Inexistent file in served folder: " + path;
+        OrthancPluginLogError(context_, s.c_str());
+        OrthancPluginSendHttpStatusCode(context_, output, 404);
+      }
     }
   }
 
@@ -266,7 +257,8 @@
     for (std::map<std::string, std::string>::const_iterator
            it = folders_.begin(); it != folders_.end(); ++it)
     {
-      s += "<li><a href=\"/" + it->first + "\">" + it->first + "</li>\n";
+      // The URI is relative to INDEX_URI ("/app/plugin-serve-folders.html")
+      s += "<li><a href=\"../" + it->first + "/index.html\">" + it->first + "</li>\n";
     }
     
     s += "</ul>\n";
@@ -361,12 +353,6 @@
 
       folders_[baseUri] = folder;
 
-      // Register the callback to serve the index of the folder
-      {
-        const std::string regex = "/(" + baseUri + ")";
-        OrthancPluginRegisterRestCallback(context, regex.c_str(), IndexCallback);
-      }
-
       // Register the callback to serve the folder
       {
         const std::string regex = "/(" + baseUri + ")/(.*)";