Mercurial > hg > orthanc
comparison 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 |
comparison
equal
deleted
inserted
replaced
1433:461e7554bff7 | 1434:f9cd40166269 |
---|---|
74 } | 74 } |
75 else if (extension == ".jpg" || extension == ".jpeg") | 75 else if (extension == ".jpg" || extension == ".jpeg") |
76 { | 76 { |
77 return "image/jpeg"; | 77 return "image/jpeg"; |
78 } | 78 } |
79 else if (extension == ".woff") | |
80 { | |
81 return "application/x-font-woff"; | |
82 } | |
79 else | 83 else |
80 { | 84 { |
81 std::string s = "Unknown MIME type for extension: " + extension; | 85 std::string s = "Unknown MIME type for extension: " + extension; |
82 OrthancPluginLogWarning(context_, s.c_str()); | 86 OrthancPluginLogWarning(context_, s.c_str()); |
83 return "application/octet-stream"; | 87 return "application/octet-stream"; |
152 } | 156 } |
153 | 157 |
154 Json::Reader reader; | 158 Json::Reader reader; |
155 if (reader.parse(s, configuration)) | 159 if (reader.parse(s, configuration)) |
156 { | 160 { |
157 std::cout << configuration.toStyledString(); | |
158 return true; | 161 return true; |
159 } | 162 } |
160 else | 163 else |
161 { | 164 { |
162 OrthancPluginLogError(context, "Unable to parse the configuration"); | 165 OrthancPluginLogError(context, "Unable to parse the configuration"); |
215 { | 218 { |
216 OrthancPluginSendMethodNotAllowed(context_, output, "GET"); | 219 OrthancPluginSendMethodNotAllowed(context_, output, "GET"); |
217 return 0; | 220 return 0; |
218 } | 221 } |
219 | 222 |
220 std::string s = "<html><body><h1>Additional folders served by Orthanc</h1><ul>\n"; | 223 std::string s = "<html><body><h1>Additional folders served by Orthanc</h1>\n"; |
221 | 224 |
222 for (std::map<std::string, std::string>::const_iterator | 225 if (folders_.empty()) |
223 it = folders_.begin(); it != folders_.end(); ++it) | 226 { |
224 { | 227 s += "<p>Empty section <tt>ServeFolders</tt> in your configuration file: No additional folder is served.</p>\n"; |
225 s += "<li><a href=\"" + it->first + "/index.html\">" + it->first + "</li>\n"; | 228 } |
226 } | 229 else |
227 | 230 { |
228 s += "</ul></body></html>"; | 231 s += "<ul>\n"; |
232 for (std::map<std::string, std::string>::const_iterator | |
233 it = folders_.begin(); it != folders_.end(); ++it) | |
234 { | |
235 s += "<li><a href=\"" + it->first + "/index.html\">" + it->first + "</li>\n"; | |
236 } | |
237 | |
238 s += "</ul>\n"; | |
239 } | |
240 | |
241 s += "</body></html>\n"; | |
229 | 242 |
230 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html"); | 243 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html"); |
231 | 244 |
232 return 0; | 245 return 0; |
233 } | 246 } |
259 if (!ReadConfiguration(configuration, context_)) | 272 if (!ReadConfiguration(configuration, context_)) |
260 { | 273 { |
261 return -1; | 274 return -1; |
262 } | 275 } |
263 | 276 |
264 if (configuration.isMember("ServeFolders") && | 277 if (configuration.isMember("ServeFolders")) |
265 configuration["ServeFolders"].type() == Json::objectValue) | 278 { |
266 { | 279 if (configuration["ServeFolders"].type() != Json::objectValue) |
267 Json::Value::Members members = configuration["ServeFolders"].getMemberNames(); | |
268 | |
269 // Register the callback for each base URI | |
270 for (Json::Value::Members::const_iterator | |
271 it = members.begin(); it != members.end(); ++it) | |
272 { | 280 { |
273 const std::string& baseUri = *it; | 281 OrthancPluginLogError(context_, "The \"ServeFolders\" configuration section is badly formatted (must be a JSON object)"); |
274 const std::string path = configuration["ServeFolders"][*it].asString(); | 282 return -1; |
275 const std::string regex = "(" + baseUri + ")/(.*)"; | |
276 | |
277 if (baseUri.empty() || | |
278 *baseUri.rbegin() == '/') | |
279 { | |
280 std::string message = "The URI of a folder to be server cannot be empty or end with a '/': " + *it; | |
281 OrthancPluginLogWarning(context_, message.c_str()); | |
282 return -1; | |
283 } | |
284 | |
285 OrthancPluginRegisterRestCallback(context, regex.c_str(), FolderCallback); | |
286 folders_[baseUri] = path; | |
287 } | 283 } |
288 | |
289 OrthancPluginRegisterRestCallback(context, INDEX_URI, IndexCallback); | |
290 OrthancPluginSetRootUri(context, INDEX_URI); | |
291 } | 284 } |
292 else | 285 else |
293 { | 286 { |
294 OrthancPluginLogWarning(context_, "No section \"ServeFolders\" in your configuration file: " | 287 OrthancPluginLogWarning(context_, "No section \"ServeFolders\" in your configuration file: " |
295 "No additional folder will be served!"); | 288 "No additional folder will be served!"); |
296 } | 289 configuration["ServeFolders"] = Json::objectValue; |
290 } | |
291 | |
292 | |
293 Json::Value::Members members = configuration["ServeFolders"].getMemberNames(); | |
294 | |
295 // Register the callback for each base URI | |
296 for (Json::Value::Members::const_iterator | |
297 it = members.begin(); it != members.end(); ++it) | |
298 { | |
299 const std::string& baseUri = *it; | |
300 const std::string path = configuration["ServeFolders"][*it].asString(); | |
301 const std::string regex = "(" + baseUri + ")/(.*)"; | |
302 | |
303 if (baseUri.empty() || | |
304 *baseUri.rbegin() == '/') | |
305 { | |
306 std::string message = "The URI of a folder to be server cannot be empty or end with a '/': " + *it; | |
307 OrthancPluginLogWarning(context_, message.c_str()); | |
308 return -1; | |
309 } | |
310 | |
311 OrthancPluginRegisterRestCallback(context, regex.c_str(), FolderCallback); | |
312 folders_[baseUri] = path; | |
313 } | |
314 | |
315 OrthancPluginRegisterRestCallback(context, INDEX_URI, IndexCallback); | |
316 OrthancPluginSetRootUri(context, INDEX_URI); | |
297 | 317 |
298 return 0; | 318 return 0; |
299 } | 319 } |
300 | 320 |
301 | 321 |