comparison Applications/StoneWebViewer/Plugin/Plugin.cpp @ 1538:d1806b4e4839

moving OrthancStone/Samples/ as Applications/Samples/
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Aug 2020 13:24:38 +0200
parents StoneWebViewer/Plugin/Plugin.cpp@3eca4f9c2827
children 8ddf77198ed7
comparison
equal deleted inserted replaced
1537:de8cf5859e84 1538:d1806b4e4839
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
23
24 #include <EmbeddedResources.h>
25
26 #include <SystemToolbox.h>
27 #include <Toolbox.h>
28
29 OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType,
30 OrthancPluginResourceType resourceType,
31 const char* resourceId)
32 {
33 try
34 {
35 if (changeType == OrthancPluginChangeType_OrthancStarted)
36 {
37 Json::Value info;
38 if (!OrthancPlugins::RestApiGet(info, "/plugins/dicom-web", false))
39 {
40 throw Orthanc::OrthancException(
41 Orthanc::ErrorCode_InternalError,
42 "The Stone Web viewer requires the DICOMweb plugin to be installed");
43 }
44
45 if (info.type() != Json::objectValue ||
46 !info.isMember("ID") ||
47 !info.isMember("Version") ||
48 info["ID"].type() != Json::stringValue ||
49 info["Version"].type() != Json::stringValue ||
50 info["ID"].asString() != "dicom-web")
51 {
52 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
53 "The DICOMweb plugin is not properly installed");
54 }
55
56 std::string version = info["Version"].asString();
57 if (version != "mainline")
58 {
59 std::vector<std::string> tokens;
60 Orthanc::Toolbox::TokenizeString(tokens, version, '.');
61 if (tokens.size() != 2)
62 {
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
64 "Bad version of the DICOMweb plugin: " + version);
65 }
66
67 int major, minor;
68
69 try
70 {
71 major = boost::lexical_cast<int>(tokens[0]);
72 minor = boost::lexical_cast<int>(tokens[1]);
73 }
74 catch (boost::bad_lexical_cast&)
75 {
76 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
77 "Bad version of the DICOMweb plugin: " + version);
78 }
79
80 if (major <= 0 ||
81 (major == 1 && minor <= 1))
82 {
83 throw Orthanc::OrthancException(
84 Orthanc::ErrorCode_InternalError,
85 "The Stone Web viewer requires DICOMweb plugin with version >= 1.2, found: " + version);
86 }
87
88 if (major <= 0 ||
89 (major == 1 && minor == 2))
90 {
91 /**
92 * DICOMweb 1.3 is better than 1.2 for 2 reasons: (1)
93 * MONOCHROME1 images are not properly rendered in DICOMweb
94 * 1.2, and (2) DICOMweb 1.2 cannot transcode images (this
95 * causes issues on JPEG2k images).
96 **/
97 LOG(WARNING) << "The Stone Web viewer has some incompatibilities "
98 << "with DICOMweb plugin 1.2, consider upgrading the DICOMweb plugin";
99 }
100 }
101 }
102 }
103 catch (Orthanc::OrthancException& e)
104 {
105 LOG(ERROR) << "Exception: " << e.What();
106 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
107 }
108
109 return OrthancPluginErrorCode_Success;
110 }
111
112
113 template <enum Orthanc::EmbeddedResources::DirectoryResourceId folder>
114 void ServeEmbeddedFolder(OrthancPluginRestOutput* output,
115 const char* url,
116 const OrthancPluginHttpRequest* request)
117 {
118 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext();
119
120 if (request->method != OrthancPluginHttpMethod_Get)
121 {
122 OrthancPluginSendMethodNotAllowed(context, output, "GET");
123 }
124 else
125 {
126 std::string path = "/" + std::string(request->groups[0]);
127 const char* mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path));
128
129 std::string s;
130 Orthanc::EmbeddedResources::GetDirectoryResource(s, folder, path.c_str());
131
132 const char* resource = s.size() ? s.c_str() : NULL;
133 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime);
134 }
135 }
136
137
138 template <enum Orthanc::EmbeddedResources::FileResourceId file>
139 void ServeEmbeddedFile(OrthancPluginRestOutput* output,
140 const char* url,
141 const OrthancPluginHttpRequest* request)
142 {
143 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext();
144
145 if (request->method != OrthancPluginHttpMethod_Get)
146 {
147 OrthancPluginSendMethodNotAllowed(context, output, "GET");
148 }
149 else
150 {
151 const char* mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(url));
152
153 std::string s;
154 Orthanc::EmbeddedResources::GetFileResource(s, file);
155
156 const char* resource = s.size() ? s.c_str() : NULL;
157 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime);
158 }
159 }
160
161
162 extern "C"
163 {
164 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
165 {
166 OrthancPlugins::SetGlobalContext(context);
167
168 #if ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 7, 2)
169 Orthanc::Logging::InitializePluginContext(context);
170 #else
171 Orthanc::Logging::Initialize(context);
172 #endif
173
174 /* Check the version of the Orthanc core */
175 if (OrthancPluginCheckVersion(context) == 0)
176 {
177 char info[1024];
178 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
179 context->orthancVersion,
180 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
181 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
182 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
183 OrthancPluginLogError(context, info);
184 return -1;
185 }
186
187 try
188 {
189 std::string explorer;
190 Orthanc::EmbeddedResources::GetFileResource(
191 explorer, Orthanc::EmbeddedResources::ORTHANC_EXPLORER);
192 OrthancPluginExtendOrthancExplorer(OrthancPlugins::GetGlobalContext(), explorer.c_str());
193
194 OrthancPlugins::RegisterRestCallback
195 <ServeEmbeddedFile<Orthanc::EmbeddedResources::STONE_WEB_VIEWER_WASM> >
196 ("/stone-webviewer/StoneWebViewer.wasm", true);
197
198 OrthancPlugins::RegisterRestCallback
199 <ServeEmbeddedFile<Orthanc::EmbeddedResources::STONE_WEB_VIEWER_JS> >
200 ("/stone-webviewer/StoneWebViewer.js", true);
201
202 OrthancPlugins::RegisterRestCallback
203 <ServeEmbeddedFile<Orthanc::EmbeddedResources::STONE_WRAPPER> >
204 ("/stone-webviewer/stone.js", true);
205
206 OrthancPlugins::RegisterRestCallback
207 <ServeEmbeddedFolder<Orthanc::EmbeddedResources::IMAGES> >
208 ("/stone-webviewer/img/(.*)", true);
209
210 OrthancPlugins::RegisterRestCallback
211 <ServeEmbeddedFolder<Orthanc::EmbeddedResources::WEB_APPLICATION> >
212 ("/stone-webviewer/(.*)", true);
213
214 OrthancPluginRegisterOnChangeCallback(context, OnChangeCallback);
215 }
216 catch (...)
217 {
218 OrthancPlugins::LogError("Exception while initializing the Stone Web viewer plugin");
219 return -1;
220 }
221
222 return 0;
223 }
224
225
226 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
227 {
228 }
229
230
231 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
232 {
233 return PLUGIN_NAME;
234 }
235
236
237 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
238 {
239 return PLUGIN_VERSION;
240 }
241 }