annotate Plugins/Samples/ServeFolders/Plugin.cpp @ 1949:d90f737f2dde

warn about the use of privileged ports
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 05 Apr 2016 10:30:17 +0200
parents 7a05144cb919
children 8856f15b3e02
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
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1628
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1322
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
1628
77c4cc4def0f OrthancPluginErrorCode in REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1597
diff changeset
159 static OrthancPluginErrorCode FolderCallback(OrthancPluginRestOutput* output,
77c4cc4def0f OrthancPluginErrorCode in REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1597
diff changeset
160 const char* url,
77c4cc4def0f OrthancPluginErrorCode in REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1597
diff changeset
161 const OrthancPluginHttpRequest* request)
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
162 {
1597
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
163 namespace fs = boost::filesystem;
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
164
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
165 if (request->method != OrthancPluginHttpMethod_Get)
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
166 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
167 OrthancPluginSendMethodNotAllowed(context_, output, "GET");
1628
77c4cc4def0f OrthancPluginErrorCode in REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1597
diff changeset
168 return OrthancPluginErrorCode_Success;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
169 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
170
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
171 std::string folder;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
172
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
173 if (LookupFolder(folder, output, request))
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
174 {
1597
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
175 const fs::path item(request->groups[1]);
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
176 const fs::path parent((fs::path(folder) / item).parent_path());
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
177
1597
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
178 if (item.filename().string() == "index.html" &&
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
179 fs::is_directory(parent) &&
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
180 !fs::is_regular_file(fs::path(folder) / item))
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
181 {
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
182 // On-the-fly generation of an "index.html"
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
183 std::string s;
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
184 s += "<html>\n";
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
185 s += " <body>\n";
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
186 s += " <ul>\n";
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
187
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
188 fs::directory_iterator end;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
189
1597
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
190 for (fs::directory_iterator it(parent) ; it != end; ++it)
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
191 {
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
192 if (fs::is_directory(it->status()))
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
193 {
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
194 std::string f = it->path().filename().string();
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
195 s += " <li><a href=\"" + f + "/index.html\">" + f + "/</a></li>\n";
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
196 }
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
197 }
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
198
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
199 for (fs::directory_iterator it(parent) ; it != end; ++it)
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
200 {
1911
7a05144cb919 fix issue 11
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
201 fs::file_type type = it->status().type();
7a05144cb919 fix issue 11
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
202
7a05144cb919 fix issue 11
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
203 if (type == fs::regular_file ||
7a05144cb919 fix issue 11
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
204 type == fs::reparse_file) // cf. BitBucket issue #11
1597
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
205 {
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
206 std::string f = it->path().filename().string();
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
207 s += " <li><a href=\"" + f + "\">" + f + "</a></li>\n";
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
208 }
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
209 }
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
210
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
211 s += " </ul>\n";
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
212 s += " </body>\n";
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
213 s += "</html>\n";
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
214
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
215 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html");
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
216 }
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
217 else
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
218 {
1597
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
219 std::string path = folder + "/" + item.string();
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
220 const char* mime = GetMimeType(path);
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
221
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
222 std::string s;
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
223 if (ReadFile(s, path))
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
224 {
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
225 const char* resource = s.size() ? s.c_str() : NULL;
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
226 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime);
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
227 }
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
228 else
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
229 {
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
230 std::string s = "Inexistent file in served folder: " + path;
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
231 OrthancPluginLogError(context_, s.c_str());
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
232 OrthancPluginSendHttpStatusCode(context_, output, 404);
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
233 }
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
234 }
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
235 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
236
1628
77c4cc4def0f OrthancPluginErrorCode in REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1597
diff changeset
237 return OrthancPluginErrorCode_Success;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
238 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
239
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
240
1628
77c4cc4def0f OrthancPluginErrorCode in REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1597
diff changeset
241 static OrthancPluginErrorCode ListServedFolders(OrthancPluginRestOutput* output,
77c4cc4def0f OrthancPluginErrorCode in REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1597
diff changeset
242 const char* url,
77c4cc4def0f OrthancPluginErrorCode in REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1597
diff changeset
243 const OrthancPluginHttpRequest* request)
1323
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 if (request->method != OrthancPluginHttpMethod_Get)
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
246 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
247 OrthancPluginSendMethodNotAllowed(context_, output, "GET");
1628
77c4cc4def0f OrthancPluginErrorCode in REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1597
diff changeset
248 return OrthancPluginErrorCode_Success;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
249 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
250
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
251 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
252
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
253 if (folders_.empty())
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
254 {
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
255 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
256 }
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
257 else
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 s += "<ul>\n";
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
260 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
261 it = folders_.begin(); it != folders_.end(); ++it)
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
262 {
1597
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
263 // The URI is relative to INDEX_URI ("/app/plugin-serve-folders.html")
415dfd1d1c61 Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
264 s += "<li><a href=\"../" + it->first + "/index.html\">" + it->first + "</li>\n";
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
265 }
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
266
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
267 s += "</ul>\n";
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
268 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
269
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
270 s += "</body></html>\n";
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
271
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
272 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
273
1628
77c4cc4def0f OrthancPluginErrorCode in REST callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1597
diff changeset
274 return OrthancPluginErrorCode_Success;
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 }
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 extern "C"
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 {
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
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 context_ = context;
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 /* Check the version of the Orthanc core */
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 if (OrthancPluginCheckVersion(context_) == 0)
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 char info[1024];
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 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
289 context_->orthancVersion,
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
293 OrthancPluginLogError(context_, info);
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 return -1;
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 }
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 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
298
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
299 Json::Value configuration;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
300 if (!ReadConfiguration(configuration, context_))
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
301 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
302 return -1;
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
303 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
304
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
305 if (configuration.isMember("ServeFolders"))
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
306 {
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
307 if (configuration["ServeFolders"].type() != Json::objectValue)
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
308 {
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
309 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
310 return -1;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
311 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
312 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
313 else
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
314 {
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
315 OrthancPluginLogWarning(context_, "No section \"ServeFolders\" in your configuration file: "
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
316 "No additional folder will be served!");
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
317 configuration["ServeFolders"] = Json::objectValue;
1323
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
318 }
5a92665dee23 Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1322
diff changeset
319
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
320
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
321 Json::Value::Members members = configuration["ServeFolders"].getMemberNames();
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
322
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
323 // Register the callback for each base URI
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
324 for (Json::Value::Members::const_iterator
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
325 it = members.begin(); it != members.end(); ++it)
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
326 {
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
327 std::string baseUri = *it;
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
328
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
329 // 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
330 while (!baseUri.empty() &&
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
331 *baseUri.begin() == '/')
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
332 {
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
333 baseUri = baseUri.substr(1);
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
334 }
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
335
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
336 while (!baseUri.empty() &&
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
337 *baseUri.rbegin() == '/')
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
338 {
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
339 baseUri.resize(baseUri.size() - 1);
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
340 }
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
341
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
342 if (baseUri.empty())
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
343 {
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
344 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
345 return -1;
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
346 }
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
347
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
348 // 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
349 const std::string folder = configuration["ServeFolders"][*it].asString();
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
350 if (!boost::filesystem::is_directory(folder))
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
351 {
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
352 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
353 OrthancPluginLogError(context_, msg.c_str());
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
354 return -1;
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
355 }
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 folders_[baseUri] = folder;
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
358
1589
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
359 // Register the callback to serve the folder
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 const std::string regex = "/(" + baseUri + ")/(.*)";
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
362 OrthancPluginRegisterRestCallback(context, regex.c_str(), FolderCallback);
334d3a92ed83 improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1588
diff changeset
363 }
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
364 }
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
365
1588
b5bc87a7212d OrthancPluginReadFile, OrthancPluginWriteFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1434
diff changeset
366 OrthancPluginRegisterRestCallback(context, INDEX_URI, ListServedFolders);
1434
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
367 OrthancPluginSetRootUri(context, INDEX_URI);
f9cd40166269 refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
368
1322
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 return 0;
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 }
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 {
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 }
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 {
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 return "serve-folders";
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 const char* OrthancPluginGetVersion()
f497a72d9f71 fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 {
1391
b3119c32c920 version in ServerFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1384
diff changeset
386 return SERVE_FOLDERS_VERSION;
1322
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 }