annotate Plugins/Samples/ServeFolders/Plugin.cpp @ 1589:334d3a92ed83

improvements to the ServeFolders plugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 26 Aug 2015 17:43:00 +0200
parents b5bc87a7212d
children 415dfd1d1c61
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
1375
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1323
diff changeset
6 * This program is free software: you can redistribute it and/or
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1323
diff changeset
7 * modify it under the terms of the GNU General Public License as
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1323
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1323
diff changeset
9 * License, or (at your option) any later version.
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1323
diff changeset
10 *
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1323
diff changeset
11 * This program is distributed in the hope that it will be useful, but
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1323
diff changeset
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1323
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1323
diff changeset
14 * General Public License for more details.
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 *
1375
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1323
diff changeset
16 * You should have received a copy of the GNU General Public License
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1323
diff changeset
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 **/
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
1425
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
21 #include <orthanc/OrthancCPlugin.h>
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
23 #include <json/reader.h>
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
24 #include <json/value.h>
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
25 #include <boost/filesystem.hpp>
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
26
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 static OrthancPluginContext* context_ = NULL;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
29 static std::map<std::string, std::string> folders_;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
30 static const char* INDEX_URI = "/app/plugin-serve-folders.html";
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
31
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
32
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
33 static const char* GetMimeType(const std::string& path)
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
34 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
35 size_t dot = path.find_last_of('.');
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
36
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
37 std::string extension = (dot == std::string::npos) ? "" : path.substr(dot);
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
38 std::transform(extension.begin(), extension.end(), extension.begin(), tolower);
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
39
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
40 if (extension == ".html")
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
41 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
42 return "text/html";
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
43 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
44 else if (extension == ".css")
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
45 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
46 return "text/css";
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
47 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
48 else if (extension == ".js")
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
49 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
50 return "application/javascript";
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
51 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
52 else if (extension == ".gif")
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
53 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
54 return "image/gif";
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
55 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
56 else if (extension == ".svg")
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
57 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
58 return "image/svg+xml";
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
59 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
60 else if (extension == ".json")
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
61 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
62 return "application/json";
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
63 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
64 else if (extension == ".xml")
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
65 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
66 return "application/xml";
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
67 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
68 else if (extension == ".png")
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
69 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
70 return "image/png";
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
71 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
72 else if (extension == ".jpg" || extension == ".jpeg")
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
73 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
74 return "image/jpeg";
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
75 }
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
76 else if (extension == ".woff")
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
77 {
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
78 return "application/x-font-woff";
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
79 }
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
80 else
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
81 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
82 std::string s = "Unknown MIME type for extension: " + extension;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
83 OrthancPluginLogWarning(context_, s.c_str());
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
84 return "application/octet-stream";
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
85 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
86 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
87
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
89 static bool ReadFile(std::string& target,
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
90 const std::string& path)
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
91 {
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
92 OrthancPluginMemoryBuffer buffer;
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
93 if (OrthancPluginReadFile(context_, &buffer, path.c_str()))
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
94 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
95 return false;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
96 }
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
97 else
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
98 {
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
99 target.assign(reinterpret_cast<const char*>(buffer.data), buffer.size);
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
100 OrthancPluginFreeMemoryBuffer(context_, &buffer);
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
101 return true;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
102 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
103 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
104
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
105
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
106 static bool ReadConfiguration(Json::Value& configuration,
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
107 OrthancPluginContext* context)
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
1425
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
109 std::string s;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
110
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
111 {
1425
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
112 char* tmp = OrthancPluginGetConfiguration(context);
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
113 if (tmp == NULL)
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
114 {
1425
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
115 OrthancPluginLogError(context, "Error while retrieving the configuration from Orthanc");
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
116 return false;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
117 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
118
1425
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
119 s.assign(tmp);
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
120 OrthancPluginFreeString(context, tmp);
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
121 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
122
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
123 Json::Reader reader;
1425
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
124 if (reader.parse(s, configuration))
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
125 {
1425
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
126 return true;
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
127 }
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
128 else
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
129 {
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1391
diff changeset
130 OrthancPluginLogError(context, "Unable to parse the configuration");
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
131 return false;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
132 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
133 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
134
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
135
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
136
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
137 static bool LookupFolder(std::string& folder,
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
138 OrthancPluginRestOutput* output,
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
139 const OrthancPluginHttpRequest* request)
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
140 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
141 const std::string uri = request->groups[0];
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
142
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
143 std::map<std::string, std::string>::const_iterator found = folders_.find(uri);
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
144 if (found == folders_.end())
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
145 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
146 std::string s = "Unknown URI in plugin server-folders: " + uri;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
147 OrthancPluginLogError(context_, s.c_str());
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
148 OrthancPluginSendHttpStatusCode(context_, output, 404);
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
149 return false;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
150 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
151 else
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
152 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
153 folder = found->second;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
154 return true;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
155 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
156 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
157
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
158
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
159 static int32_t IndexCallback(OrthancPluginRestOutput* output,
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
160 const char* url,
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
161 const OrthancPluginHttpRequest* request)
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
162 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
163 namespace fs = boost::filesystem;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
164
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
165 if (request->method != OrthancPluginHttpMethod_Get)
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
166 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
167 OrthancPluginSendMethodNotAllowed(context_, output, "GET");
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
168 return 0;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
169 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
170
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
171 std::string folder;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
172 if (LookupFolder(folder, output, request))
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
173 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
174 std::string baseUri = "/" + std::string(request->groups[0]);
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
175
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
176 if (fs::is_regular_file(fs::path(folder) / "index.html"))
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
177 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
178 std::string s = baseUri + "/index.html";
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
179 OrthancPluginRedirect(context_, output, s.c_str());
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
180 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
181 else
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
182 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
183 std::string s;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
184 s += "<html>\n";
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
185 s += " <body>\n";
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
186 s += " <ul>\n";
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
187
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
188 fs::directory_iterator end;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
189 for (fs::directory_iterator it(folder) ; it != end; ++it)
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
190 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
191 if (fs::is_regular_file(it->status()))
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
192 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
193 std::string f = it->path().filename().string();
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
194 s += " <li><a href=\"" + baseUri + "/" + f + "\">" + f + "</a></li>";
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
195 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
196 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
197
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
198 s += " </ul>\n";
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
199 s += " </body>\n";
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
200 s += "</html>\n";
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
201
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
202 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html");
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
203 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
204 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
205
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
206 return 0;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
207 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
208
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
209
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
210 static int32_t FolderCallback(OrthancPluginRestOutput* output,
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
211 const char* url,
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
212 const OrthancPluginHttpRequest* request)
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
213 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
214 if (request->method != OrthancPluginHttpMethod_Get)
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
215 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
216 OrthancPluginSendMethodNotAllowed(context_, output, "GET");
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
217 return 0;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
218 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
219
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
220 std::string folder;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
221
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
222 if (LookupFolder(folder, output, request))
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
223 {
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
224 const std::string item = request->groups[1];
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
225
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
226 std::string path = folder + "/" + item;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
227 const char* mime = GetMimeType(path);
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
228
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
229 std::string s;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
230 if (ReadFile(s, path))
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
231 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
232 const char* resource = s.size() ? s.c_str() : NULL;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
233 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime);
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
234 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
235 else
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
236 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
237 std::string s = "Inexistent file in served folder: " + path;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
238 OrthancPluginLogError(context_, s.c_str());
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
239 OrthancPluginSendHttpStatusCode(context_, output, 404);
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
240 }
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
241 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
242
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
243 return 0;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
244 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
245
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
246
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
247 static int32_t ListServedFolders(OrthancPluginRestOutput* output,
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
248 const char* url,
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
249 const OrthancPluginHttpRequest* request)
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
250 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
251 if (request->method != OrthancPluginHttpMethod_Get)
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
252 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
253 OrthancPluginSendMethodNotAllowed(context_, output, "GET");
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
254 return 0;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
255 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
256
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
257 std::string s = "<html><body><h1>Additional folders served by Orthanc</h1>\n";
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
258
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
259 if (folders_.empty())
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
260 {
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
261 s += "<p>Empty section <tt>ServeFolders</tt> in your configuration file: No additional folder is served.</p>\n";
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
262 }
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
263 else
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
264 {
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
265 s += "<ul>\n";
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
266 for (std::map<std::string, std::string>::const_iterator
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
267 it = folders_.begin(); it != folders_.end(); ++it)
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
268 {
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
269 s += "<li><a href=\"/" + it->first + "\">" + it->first + "</li>\n";
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
270 }
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
271
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
272 s += "</ul>\n";
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
273 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
274
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
275 s += "</body></html>\n";
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
276
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
277 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html");
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
278
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
279 return 0;
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 }
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 extern "C"
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 {
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 {
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 context_ = context;
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 /* Check the version of the Orthanc core */
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 if (OrthancPluginCheckVersion(context_) == 0)
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 {
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 char info[1024];
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 context_->orthancVersion,
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
298 OrthancPluginLogError(context_, info);
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 return -1;
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 }
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 OrthancPluginSetDescription(context_, "Serve additional folders with the HTTP server of Orthanc.");
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
304 Json::Value configuration;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
305 if (!ReadConfiguration(configuration, context_))
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
306 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
307 return -1;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
308 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
309
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
310 if (configuration.isMember("ServeFolders"))
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
311 {
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
312 if (configuration["ServeFolders"].type() != Json::objectValue)
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
313 {
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
314 OrthancPluginLogError(context_, "The \"ServeFolders\" configuration section is badly formatted (must be a JSON object)");
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
315 return -1;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
316 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
317 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
318 else
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
319 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
320 OrthancPluginLogWarning(context_, "No section \"ServeFolders\" in your configuration file: "
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
321 "No additional folder will be served!");
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
322 configuration["ServeFolders"] = Json::objectValue;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
323 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
324
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
325
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
326 Json::Value::Members members = configuration["ServeFolders"].getMemberNames();
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
327
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
328 // Register the callback for each base URI
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
329 for (Json::Value::Members::const_iterator
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
330 it = members.begin(); it != members.end(); ++it)
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
331 {
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
332 std::string baseUri = *it;
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
333
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
334 // Remove the heading and trailing slashes in the root URI, if any
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
335 while (!baseUri.empty() &&
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
336 *baseUri.begin() == '/')
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
337 {
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
338 baseUri = baseUri.substr(1);
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
339 }
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
340
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
341 while (!baseUri.empty() &&
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
342 *baseUri.rbegin() == '/')
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
343 {
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
344 baseUri.resize(baseUri.size() - 1);
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
345 }
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
346
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
347 if (baseUri.empty())
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
348 {
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
349 OrthancPluginLogError(context_, "The URI of a folder to be served cannot be empty");
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
350 return -1;
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
351 }
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
352
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
353 // Check whether the source folder exists and is indeed a directory
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
354 const std::string folder = configuration["ServeFolders"][*it].asString();
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
355 if (!boost::filesystem::is_directory(folder))
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
356 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
357 std::string msg = "Trying and serve an inexistent folder: " + folder;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
358 OrthancPluginLogError(context_, msg.c_str());
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
359 return -1;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
360 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
361
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
362 folders_[baseUri] = folder;
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
363
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
364 // Register the callback to serve the index of the folder
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
365 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
366 const std::string regex = "/(" + baseUri + ")";
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
367 OrthancPluginRegisterRestCallback(context, regex.c_str(), IndexCallback);
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
368 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
369
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
370 // Register the callback to serve the folder
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
371 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
372 const std::string regex = "/(" + baseUri + ")/(.*)";
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
373 OrthancPluginRegisterRestCallback(context, regex.c_str(), FolderCallback);
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
374 }
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
375 }
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
376
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
377 OrthancPluginRegisterRestCallback(context, INDEX_URI, ListServedFolders);
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
378 OrthancPluginSetRootUri(context, INDEX_URI);
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
379
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 return 0;
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 }
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 {
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 }
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 {
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 return "serve-folders";
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 }
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 {
1391
b3119c32c920 version in ServerFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1384
diff changeset
397 return SERVE_FOLDERS_VERSION;
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 }
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399 }