Mercurial > hg > orthanc
annotate Plugins/Samples/ServeFolders/Plugin.cpp @ 4026:05a363186da6
ORTHANC_BUILDING_FRAMEWORK_LIBRARY, Orthanc::InitializeFramework()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 09 Jun 2020 16:26:57 +0200 |
parents | 94f4a18a79cc |
children |
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 | 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 |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3531
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 * |
1375
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1323
diff
changeset
|
7 * 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
|
8 * 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
|
9 * 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
|
10 * License, or (at your option) any later version. |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1323
diff
changeset
|
11 * |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1323
diff
changeset
|
12 * 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
|
13 * 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
|
14 * 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
|
15 * General Public License for more details. |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 * |
1375
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1323
diff
changeset
|
17 * 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
|
18 * 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
|
19 **/ |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 |
2062
40ffd0e8676a
generation of etag in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2061
diff
changeset
|
22 #include "../Common/OrthancPluginCppWrapper.h" |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
24 #include <json/reader.h> |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
25 #include <json/value.h> |
1589
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
26 #include <boost/filesystem.hpp> |
2062
40ffd0e8676a
generation of etag in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2061
diff
changeset
|
27 #include <boost/date_time/posix_time/posix_time.hpp> |
1589
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
28 |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 |
2176
fead5549aaa7
introduction of HAS_ORTHANC_EXCEPTION to avoid PluginException if not necessary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2169
diff
changeset
|
30 #if HAS_ORTHANC_EXCEPTION == 1 |
fead5549aaa7
introduction of HAS_ORTHANC_EXCEPTION to avoid PluginException if not necessary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2169
diff
changeset
|
31 # error The macro HAS_ORTHANC_EXCEPTION must be set to 0 to compile this plugin |
fead5549aaa7
introduction of HAS_ORTHANC_EXCEPTION to avoid PluginException if not necessary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2169
diff
changeset
|
32 #endif |
fead5549aaa7
introduction of HAS_ORTHANC_EXCEPTION to avoid PluginException if not necessary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2169
diff
changeset
|
33 |
fead5549aaa7
introduction of HAS_ORTHANC_EXCEPTION to avoid PluginException if not necessary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2169
diff
changeset
|
34 |
fead5549aaa7
introduction of HAS_ORTHANC_EXCEPTION to avoid PluginException if not necessary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2169
diff
changeset
|
35 |
2166
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
36 static std::map<std::string, std::string> extensions_; |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
37 static std::map<std::string, std::string> folders_; |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
38 static const char* INDEX_URI = "/app/plugin-serve-folders.html"; |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
39 static bool allowCache_ = false; |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
40 static bool generateETag_ = true; |
2061
8856f15b3e02
disabling cache in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
41 |
8856f15b3e02
disabling cache in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
42 |
8856f15b3e02
disabling cache in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
43 static void SetHttpHeaders(OrthancPluginRestOutput* output) |
8856f15b3e02
disabling cache in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
44 { |
8856f15b3e02
disabling cache in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
45 if (!allowCache_) |
8856f15b3e02
disabling cache in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
46 { |
8856f15b3e02
disabling cache in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
47 // http://stackoverflow.com/a/2068407/881731 |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
48 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext(); |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
49 OrthancPluginSetHttpHeader(context, output, "Cache-Control", "no-cache, no-store, must-revalidate"); |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
50 OrthancPluginSetHttpHeader(context, output, "Pragma", "no-cache"); |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
51 OrthancPluginSetHttpHeader(context, output, "Expires", "0"); |
2061
8856f15b3e02
disabling cache in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
52 } |
8856f15b3e02
disabling cache in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
53 } |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
54 |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
55 |
2166
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
56 static void RegisterDefaultExtensions() |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
57 { |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
58 extensions_["css"] = "text/css"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
59 extensions_["gif"] = "image/gif"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
60 extensions_["html"] = "text/html"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
61 extensions_["jpeg"] = "image/jpeg"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
62 extensions_["jpg"] = "image/jpeg"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
63 extensions_["js"] = "application/javascript"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
64 extensions_["json"] = "application/json"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
65 extensions_["nexe"] = "application/x-nacl"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
66 extensions_["nmf"] = "application/json"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
67 extensions_["pexe"] = "application/x-pnacl"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
68 extensions_["png"] = "image/png"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
69 extensions_["svg"] = "image/svg+xml"; |
2433
b382879072a5
added application/wasm MIME type for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2268
diff
changeset
|
70 extensions_["wasm"] = "application/wasm"; |
2166
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
71 extensions_["woff"] = "application/x-font-woff"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
72 extensions_["xml"] = "application/xml"; |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
73 } |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
74 |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
75 |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
76 static std::string GetMimeType(const std::string& path) |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
77 { |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
78 size_t dot = path.find_last_of('.'); |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
79 |
2166
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
80 std::string extension = (dot == std::string::npos) ? "" : path.substr(dot + 1); |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
81 std::transform(extension.begin(), extension.end(), extension.begin(), tolower); |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
82 |
2166
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
83 std::map<std::string, std::string>::const_iterator found = extensions_.find(extension); |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
84 |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
85 if (found != extensions_.end() && |
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
86 !found->second.empty()) |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
87 { |
2166
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
88 return found->second; |
2061
8856f15b3e02
disabling cache in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
89 } |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
90 else |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
91 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
92 OrthancPlugins::LogWarning("ServeFolders: Unknown MIME type for extension \"" + extension + "\""); |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
93 return "application/octet-stream"; |
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 } |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
96 |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
97 |
1589
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
98 static bool LookupFolder(std::string& folder, |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
99 OrthancPluginRestOutput* output, |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
100 const OrthancPluginHttpRequest* request) |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
101 { |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
102 const std::string uri = request->groups[0]; |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
103 |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
104 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
|
105 if (found == folders_.end()) |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
106 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
107 OrthancPlugins::LogError("Unknown URI in plugin server-folders: " + uri); |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
108 OrthancPluginSendHttpStatusCode(OrthancPlugins::GetGlobalContext(), output, 404); |
1589
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
109 return false; |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
110 } |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
111 else |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
112 { |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
113 folder = found->second; |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
114 return true; |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
115 } |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
116 } |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
117 |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
118 |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
119 static void Answer(OrthancPluginRestOutput* output, |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
120 const char* content, |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
121 size_t size, |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
122 const std::string& mime) |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
123 { |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
124 if (generateETag_) |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
125 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
126 OrthancPlugins::OrthancString md5; |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
127 md5.Assign(OrthancPluginComputeMd5(OrthancPlugins::GetGlobalContext(), content, size)); |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2177
diff
changeset
|
128 |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
129 std::string etag = "\"" + std::string(md5.GetContent()) + "\""; |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
130 OrthancPluginSetHttpHeader(OrthancPlugins::GetGlobalContext(), output, "ETag", etag.c_str()); |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
131 } |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
132 |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
133 SetHttpHeaders(output); |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
134 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, content, size, mime.c_str()); |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
135 } |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
136 |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
137 |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
138 void ServeFolder(OrthancPluginRestOutput* output, |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
139 const char* url, |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
140 const OrthancPluginHttpRequest* request) |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
141 { |
1597
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
142 namespace fs = boost::filesystem; |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
143 |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
144 if (request->method != OrthancPluginHttpMethod_Get) |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
145 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
146 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET"); |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
147 return; |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
148 } |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
149 |
1589
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
150 std::string folder; |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
151 |
1589
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
152 if (LookupFolder(folder, output, request)) |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
153 { |
1597
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
154 const fs::path item(request->groups[1]); |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
155 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
|
156 |
1597
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
157 if (item.filename().string() == "index.html" && |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
158 fs::is_directory(parent) && |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
159 !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
|
160 { |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
161 // 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
|
162 std::string s; |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
163 s += "<html>\n"; |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
164 s += " <body>\n"; |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
165 s += " <ul>\n"; |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
166 |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
167 fs::directory_iterator end; |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
168 |
1597
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
169 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
|
170 { |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
171 if (fs::is_directory(it->status())) |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
172 { |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
173 std::string f = it->path().filename().string(); |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
174 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
|
175 } |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
176 } |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
177 |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
178 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
|
179 { |
1911 | 180 fs::file_type type = it->status().type(); |
181 | |
182 if (type == fs::regular_file || | |
183 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
|
184 { |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
185 std::string f = it->path().filename().string(); |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
186 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
|
187 } |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
188 } |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
189 |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
190 s += " </ul>\n"; |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
191 s += " </body>\n"; |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
192 s += "</html>\n"; |
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
193 |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
194 Answer(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
|
195 } |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
196 else |
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
197 { |
1597
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
198 std::string path = folder + "/" + item.string(); |
2166
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
199 std::string mime = GetMimeType(path); |
1597
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
200 |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
201 OrthancPlugins::MemoryBuffer content; |
2062
40ffd0e8676a
generation of etag in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2061
diff
changeset
|
202 |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
203 try |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
204 { |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
205 content.ReadFile(path); |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
206 } |
2176
fead5549aaa7
introduction of HAS_ORTHANC_EXCEPTION to avoid PluginException if not necessary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2169
diff
changeset
|
207 catch (...) |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
208 { |
2234
a78d15509a1c
cleaner separation of PluginException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
209 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile); |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
210 } |
2062
40ffd0e8676a
generation of etag in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2061
diff
changeset
|
211 |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
212 boost::posix_time::ptime lastModification = |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
213 boost::posix_time::from_time_t(fs::last_write_time(path)); |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
214 std::string t = boost::posix_time::to_iso_string(lastModification); |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
215 OrthancPluginSetHttpHeader(OrthancPlugins::GetGlobalContext(), |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
216 output, "Last-Modified", t.c_str()); |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
217 |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
218 Answer(output, content.GetData(), content.GetSize(), mime); |
1589
334d3a92ed83
improvements to the ServeFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1588
diff
changeset
|
219 } |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
220 } |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
221 } |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
222 |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
223 |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
224 void ListServedFolders(OrthancPluginRestOutput* output, |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
225 const char* url, |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
226 const OrthancPluginHttpRequest* request) |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
227 { |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
228 if (request->method != OrthancPluginHttpMethod_Get) |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
229 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
230 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET"); |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
231 return; |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
232 } |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
233 |
1434
f9cd40166269
refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1425
diff
changeset
|
234 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
|
235 |
1434
f9cd40166269
refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1425
diff
changeset
|
236 if (folders_.empty()) |
f9cd40166269
refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1425
diff
changeset
|
237 { |
f9cd40166269
refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1425
diff
changeset
|
238 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
|
239 } |
f9cd40166269
refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1425
diff
changeset
|
240 else |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
241 { |
1434
f9cd40166269
refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1425
diff
changeset
|
242 s += "<ul>\n"; |
f9cd40166269
refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1425
diff
changeset
|
243 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
|
244 it = folders_.begin(); it != folders_.end(); ++it) |
f9cd40166269
refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1425
diff
changeset
|
245 { |
1597
415dfd1d1c61
Improvements to the sample "ServeFolders" plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
246 // 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
|
247 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
|
248 } |
f9cd40166269
refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1425
diff
changeset
|
249 |
f9cd40166269
refactoring of OrthancPlugins, improvement in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1425
diff
changeset
|
250 s += "</ul>\n"; |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
251 } |
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 s += "</body></html>\n"; |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
254 |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
255 Answer(output, s.c_str(), s.size(), "text/html"); |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
256 } |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
257 |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
258 |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
259 static void ConfigureFolders(const Json::Value& folders) |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
260 { |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
261 if (folders.type() != Json::objectValue) |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
262 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
263 OrthancPlugins::LogError("The list of folders to be served is badly formatted (must be a JSON object)"); |
2234
a78d15509a1c
cleaner separation of PluginException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
264 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
265 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
266 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
267 Json::Value::Members members = folders.getMemberNames(); |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
268 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
269 // Register the callback for each base URI |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
270 for (Json::Value::Members::const_iterator |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
271 it = members.begin(); it != members.end(); ++it) |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
272 { |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
273 if (folders[*it].type() != Json::stringValue) |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
274 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
275 OrthancPlugins::LogError("The folder to be served \"" + *it + |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
276 "\" must be associated with a string value (its mapped URI)"); |
2234
a78d15509a1c
cleaner separation of PluginException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
277 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
278 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
279 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
280 std::string baseUri = *it; |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
281 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
282 // Remove the heading and trailing slashes in the root URI, if any |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
283 while (!baseUri.empty() && |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
284 *baseUri.begin() == '/') |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
285 { |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
286 baseUri = baseUri.substr(1); |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
287 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
288 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
289 while (!baseUri.empty() && |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
290 *baseUri.rbegin() == '/') |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
291 { |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
292 baseUri.resize(baseUri.size() - 1); |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
293 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
294 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
295 if (baseUri.empty()) |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
296 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
297 OrthancPlugins::LogError("The URI of a folder to be served cannot be empty"); |
2234
a78d15509a1c
cleaner separation of PluginException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
298 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
299 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
300 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
301 // Check whether the source folder exists and is indeed a directory |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
302 const std::string folder = folders[*it].asString(); |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
303 if (!boost::filesystem::is_directory(folder)) |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
304 { |
3531 | 305 OrthancPlugins::LogError("Trying to serve an inexistent folder: " + folder); |
2234
a78d15509a1c
cleaner separation of PluginException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
306 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile); |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
307 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
308 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
309 folders_[baseUri] = folder; |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
310 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
311 // Register the callback to serve the folder |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
312 { |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
313 const std::string regex = "/(" + baseUri + ")/(.*)"; |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
314 OrthancPlugins::RegisterRestCallback<ServeFolder>(regex.c_str(), true); |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
315 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
316 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
317 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
318 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
319 |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
320 static void ConfigureExtensions(const Json::Value& extensions) |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
321 { |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
322 if (extensions.type() != Json::objectValue) |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
323 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
324 OrthancPlugins::LogError("The list of extensions is badly formatted (must be a JSON object)"); |
2234
a78d15509a1c
cleaner separation of PluginException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
325 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
326 } |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
327 |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
328 Json::Value::Members members = extensions.getMemberNames(); |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
329 |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
330 for (Json::Value::Members::const_iterator |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
331 it = members.begin(); it != members.end(); ++it) |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
332 { |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
333 if (extensions[*it].type() != Json::stringValue) |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
334 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
335 OrthancPlugins::LogError("The file extension \"" + *it + |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
336 "\" must be associated with a string value (its MIME type)"); |
2234
a78d15509a1c
cleaner separation of PluginException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2205
diff
changeset
|
337 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
338 } |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
339 |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
340 const std::string& mime = extensions[*it].asString(); |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
341 |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
342 std::string name = *it; |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
343 |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
344 if (!name.empty() && |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
345 name[0] == '.') |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
346 { |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
347 name = name.substr(1); // Remove the leading dot "." |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
348 } |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
349 |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
350 extensions_[name] = mime; |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
351 |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
352 if (mime.empty()) |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
353 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
354 OrthancPlugins::LogWarning("ServeFolders: Removing MIME type for file extension \"." + |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
355 name + "\""); |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
356 } |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
357 else |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
358 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
359 OrthancPlugins::LogWarning("ServeFolders: Associating file extension \"." + name + |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
360 "\" with MIME type \"" + mime + "\""); |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
361 } |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
362 } |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
363 } |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
364 |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
365 |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
366 static void ReadConfiguration() |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
367 { |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
368 OrthancPlugins::OrthancConfiguration configuration; |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
369 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
370 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
371 OrthancPlugins::OrthancConfiguration globalConfiguration; |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
372 globalConfiguration.GetSection(configuration, "ServeFolders"); |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
373 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
374 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
375 if (!configuration.IsSection("Folders")) |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
376 { |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
377 // This is a basic configuration |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
378 ConfigureFolders(configuration.GetJson()); |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
379 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
380 else |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
381 { |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
382 // This is an advanced configuration |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
383 ConfigureFolders(configuration.GetJson()["Folders"]); |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
384 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
385 bool tmp; |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
386 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
387 if (configuration.LookupBooleanValue(tmp, "AllowCache")) |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
388 { |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
389 allowCache_ = tmp; |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
390 OrthancPlugins::LogWarning("ServeFolders: Requesting the HTTP client to " + |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
391 std::string(tmp ? "enable" : "disable") + |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
392 " its caching mechanism"); |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
393 } |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
394 |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
395 if (configuration.LookupBooleanValue(tmp, "GenerateETag")) |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
396 { |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
397 generateETag_ = tmp; |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
398 OrthancPlugins::LogWarning("ServeFolders: The computation of an ETag for the " |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
399 "served resources is " + |
2168
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
400 std::string(tmp ? "enabled" : "disabled")); |
84033563f7f0
ServeFolders: Better protection against exceptions in the callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2167
diff
changeset
|
401 } |
2169
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
402 |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
403 OrthancPlugins::OrthancConfiguration extensions; |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
404 configuration.GetSection(extensions, "Extensions"); |
d15de5685ad8
ServeFolders: "Extensions" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2168
diff
changeset
|
405 ConfigureExtensions(extensions.GetJson()); |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
406 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
407 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
408 if (folders_.empty()) |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
409 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
410 OrthancPlugins::LogWarning("ServeFolders: Empty configuration file: " |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
411 "No additional folder will be served!"); |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
412 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
413 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
414 |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
415 |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
416 extern "C" |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
417 { |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
418 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
419 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
420 OrthancPlugins::SetGlobalContext(context); |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
421 |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
422 /* Check the version of the Orthanc core */ |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
423 if (OrthancPluginCheckVersion(context) == 0) |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
424 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
425 OrthancPlugins::ReportMinimalOrthancVersion(ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, |
2268
ce5c13b95dac
New function: OrthancPluginRegisterIncomingHttpRequestFilter2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
426 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, |
ce5c13b95dac
New function: OrthancPluginRegisterIncomingHttpRequestFilter2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
427 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
428 return -1; |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
429 } |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
430 |
2166
700dbb3baac9
refactoring in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2062
diff
changeset
|
431 RegisterDefaultExtensions(); |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
432 OrthancPluginSetDescription(context, "Serve additional folders with the HTTP server of Orthanc."); |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
433 OrthancPluginSetRootUri(context, INDEX_URI); |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
434 OrthancPlugins::RegisterRestCallback<ListServedFolders>(INDEX_URI, true); |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
435 |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
436 try |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
437 { |
2167
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
438 ReadConfiguration(); |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
439 } |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
440 catch (OrthancPlugins::PluginException& e) |
aa2915963531
"Folders" sub-configuration in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2166
diff
changeset
|
441 { |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
442 OrthancPlugins::LogError("Error while initializing the ServeFolders plugin: " + |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
443 std::string(e.What(context))); |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
444 } |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1322
diff
changeset
|
445 |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
446 return 0; |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
447 } |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
448 |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
449 |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
450 ORTHANC_PLUGINS_API void OrthancPluginFinalize() |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
451 { |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
452 } |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
453 |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
454 |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
455 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
456 { |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
457 return "serve-folders"; |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
458 } |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
459 |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
460 |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
461 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion() |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
462 { |
1391
b3119c32c920
version in ServerFolders plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1384
diff
changeset
|
463 return SERVE_FOLDERS_VERSION; |
1322
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
464 } |
f497a72d9f71
fixes to the sample plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
465 } |