diff Plugins/Samples/ServeFolders/Plugin.cpp @ 1434:f9cd40166269

refactoring of OrthancPlugins, improvement in ServeFolders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Jun 2015 16:04:05 +0200
parents 97268448bdfc
children b5bc87a7212d
line wrap: on
line diff
--- a/Plugins/Samples/ServeFolders/Plugin.cpp	Tue Jun 30 15:09:34 2015 +0200
+++ b/Plugins/Samples/ServeFolders/Plugin.cpp	Tue Jun 30 16:04:05 2015 +0200
@@ -76,6 +76,10 @@
   {
     return "image/jpeg";
   }
+  else if (extension == ".woff")
+  {
+    return "application/x-font-woff";
+  }
   else
   {
     std::string s = "Unknown MIME type for extension: " + extension;
@@ -154,7 +158,6 @@
   Json::Reader reader;
   if (reader.parse(s, configuration))
   {
-    std::cout << configuration.toStyledString();
     return true;
   }
   else
@@ -217,15 +220,25 @@
     return 0;
   }
 
-  std::string s = "<html><body><h1>Additional folders served by Orthanc</h1><ul>\n";
+  std::string s = "<html><body><h1>Additional folders served by Orthanc</h1>\n";
 
-  for (std::map<std::string, std::string>::const_iterator
-         it = folders_.begin(); it != folders_.end(); ++it)
+  if (folders_.empty())
+  {
+    s += "<p>Empty section <tt>ServeFolders</tt> in your configuration file: No additional folder is served.</p>\n";
+  }
+  else
   {
-    s += "<li><a href=\"" + it->first + "/index.html\">" + it->first + "</li>\n";
+    s += "<ul>\n";
+    for (std::map<std::string, std::string>::const_iterator
+           it = folders_.begin(); it != folders_.end(); ++it)
+    {
+      s += "<li><a href=\"" + it->first + "/index.html\">" + it->first + "</li>\n";
+    }
+    
+    s += "</ul>\n";
   }
 
-  s += "</ul></body></html>";
+  s += "</body></html>\n";
 
   OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html");
 
@@ -261,40 +274,47 @@
       return -1;
     }
 
-    if (configuration.isMember("ServeFolders") &&
-        configuration["ServeFolders"].type() == Json::objectValue)
+    if (configuration.isMember("ServeFolders"))
     {
-      Json::Value::Members members = configuration["ServeFolders"].getMemberNames();
-
-      // Register the callback for each base URI
-      for (Json::Value::Members::const_iterator 
-             it = members.begin(); it != members.end(); ++it)
+      if (configuration["ServeFolders"].type() != Json::objectValue)
       {
-        const std::string& baseUri = *it;
-        const std::string path = configuration["ServeFolders"][*it].asString();
-        const std::string regex = "(" + baseUri + ")/(.*)";
-
-        if (baseUri.empty() ||
-            *baseUri.rbegin() == '/')
-        {
-          std::string message = "The URI of a folder to be server cannot be empty or end with a '/': " + *it;
-          OrthancPluginLogWarning(context_, message.c_str());
-          return -1;
-        }
-
-        OrthancPluginRegisterRestCallback(context, regex.c_str(), FolderCallback);
-        folders_[baseUri] = path;
+        OrthancPluginLogError(context_, "The \"ServeFolders\" configuration section is badly formatted (must be a JSON object)");
+        return -1;
       }
-
-      OrthancPluginRegisterRestCallback(context, INDEX_URI, IndexCallback);
-      OrthancPluginSetRootUri(context, INDEX_URI);
     }
     else
     {
       OrthancPluginLogWarning(context_, "No section \"ServeFolders\" in your configuration file: "
                               "No additional folder will be served!");
+      configuration["ServeFolders"] = Json::objectValue;
     }
 
+
+    Json::Value::Members members = configuration["ServeFolders"].getMemberNames();
+
+    // Register the callback for each base URI
+    for (Json::Value::Members::const_iterator 
+           it = members.begin(); it != members.end(); ++it)
+    {
+      const std::string& baseUri = *it;
+      const std::string path = configuration["ServeFolders"][*it].asString();
+      const std::string regex = "(" + baseUri + ")/(.*)";
+
+      if (baseUri.empty() ||
+          *baseUri.rbegin() == '/')
+      {
+        std::string message = "The URI of a folder to be server cannot be empty or end with a '/': " + *it;
+        OrthancPluginLogWarning(context_, message.c_str());
+        return -1;
+      }
+
+      OrthancPluginRegisterRestCallback(context, regex.c_str(), FolderCallback);
+      folders_[baseUri] = path;
+    }
+
+    OrthancPluginRegisterRestCallback(context, INDEX_URI, IndexCallback);
+    OrthancPluginSetRootUri(context, INDEX_URI);
+
     return 0;
   }