# HG changeset patch # User Sebastien Jodogne # Date 1440762443 -7200 # Node ID 415dfd1d1c61625e3328de13f564d37ae0d8fc3f # Parent f2e3d030ea59b61c2af50b46edd987775d0810bd Improvements to the sample "ServeFolders" plugin diff -r f2e3d030ea59 -r 415dfd1d1c61 NEWS --- 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 ----------- diff -r f2e3d030ea59 -r 415dfd1d1c61 OrthancServer/OrthancRestApi/OrthancRestSystem.cpp --- 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); diff -r f2e3d030ea59 -r 415dfd1d1c61 Plugins/Samples/ServeFolders/Plugin.cpp --- 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 += "\n"; - s += " \n"; - s += " \n"; - s += " \n"; - s += "\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 += "\n"; + s += " \n"; + s += " \n"; + s += " \n"; + s += "\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::const_iterator it = folders_.begin(); it != folders_.end(); ++it) { - s += "
  • first + "\">" + it->first + "
  • \n"; + // The URI is relative to INDEX_URI ("/app/plugin-serve-folders.html") + s += "
  • first + "/index.html\">" + it->first + "
  • \n"; } s += "\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 + ")/(.*)";