comparison StoneWebViewer/Plugin/Plugin.cpp @ 1495:fb74ed5d8c22

initial commit of the Stone Web viewer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 25 Jun 2020 16:51:10 +0200
parents
children 6e832ff72a5e
comparison
equal deleted inserted replaced
1494:5a3ef478deb6 1495:fb74ed5d8c22
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 <OrthancPluginCppWrapper.h>
23 #include <EmbeddedResources.h>
24
25 #include <SystemToolbox.h>
26 #include <Toolbox.h>
27
28 OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType,
29 OrthancPluginResourceType resourceType,
30 const char* resourceId)
31 {
32 try
33 {
34 if (changeType == OrthancPluginChangeType_OrthancStarted)
35 {
36 Json::Value info;
37 if (!OrthancPlugins::RestApiGet(info, "/plugins/dicom-web", false))
38 {
39 throw Orthanc::OrthancException(
40 Orthanc::ErrorCode_InternalError,
41 "The Stone Web viewer requires the DICOMweb plugin to be installed");
42 }
43
44 if (info.type() != Json::objectValue ||
45 !info.isMember("ID") ||
46 !info.isMember("Version") ||
47 info["ID"].type() != Json::stringValue ||
48 info["Version"].type() != Json::stringValue ||
49 info["ID"].asString() != "dicom-web")
50 {
51 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
52 "The DICOMweb plugin is not properly installed");
53 }
54
55 std::string version = info["Version"].asString();
56 if (version != "mainline")
57 {
58 std::vector<std::string> tokens;
59 Orthanc::Toolbox::TokenizeString(tokens, version, '.');
60 if (tokens.size() != 2)
61 {
62 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
63 "Bad version of the DICOMweb plugin: " + version);
64 }
65
66 int major, minor;
67
68 try
69 {
70 major = boost::lexical_cast<int>(tokens[0]);
71 minor = boost::lexical_cast<int>(tokens[1]);
72 }
73 catch (boost::bad_lexical_cast&)
74 {
75 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
76 "Bad version of the DICOMweb plugin: " + version);
77 }
78
79 if (major <= 0 ||
80 (major == 1 && minor <= 1))
81 {
82 throw Orthanc::OrthancException(
83 Orthanc::ErrorCode_InternalError,
84 "The Stone Web viewer requires DICOMweb plugin with version >= 1.2, found: " + version);
85 }
86
87 if (major <= 0 ||
88 (major == 1 && minor == 2))
89 {
90 /**
91 * DICOMweb 1.3 is better than 1.2 for 2 reasons: (1)
92 * MONOCHROME1 images are not properly rendered in DICOMweb
93 * 1.2, and (2) DICOMweb 1.2 cannot transcode images (this
94 * causes issues on JPEG2k images).
95 **/
96 LOG(WARNING) << "The Stone Web viewer has some incompatibilities "
97 << "with DICOMweb plugin 1.2, consider upgrading the DICOMweb plugin";
98 }
99 }
100 }
101 }
102 catch (Orthanc::OrthancException& e)
103 {
104 LOG(ERROR) << "Exception: " << e.What();
105 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
106 }
107
108 return OrthancPluginErrorCode_Success;
109 }
110
111
112 template <enum Orthanc::EmbeddedResources::DirectoryResourceId folder>
113 void ServeEmbeddedFolder(OrthancPluginRestOutput* output,
114 const char* url,
115 const OrthancPluginHttpRequest* request)
116 {
117 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext();
118
119 if (request->method != OrthancPluginHttpMethod_Get)
120 {
121 OrthancPluginSendMethodNotAllowed(context, output, "GET");
122 }
123 else
124 {
125 std::string path = "/" + std::string(request->groups[0]);
126 const char* mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path));
127
128 std::string s;
129 Orthanc::EmbeddedResources::GetDirectoryResource(s, folder, path.c_str());
130
131 const char* resource = s.size() ? s.c_str() : NULL;
132 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime);
133 }
134 }
135
136
137 template <enum Orthanc::EmbeddedResources::FileResourceId file>
138 void ServeEmbeddedFile(OrthancPluginRestOutput* output,
139 const char* url,
140 const OrthancPluginHttpRequest* request)
141 {
142 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext();
143
144 if (request->method != OrthancPluginHttpMethod_Get)
145 {
146 OrthancPluginSendMethodNotAllowed(context, output, "GET");
147 }
148 else
149 {
150 const char* mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(url));
151
152 std::string s;
153 Orthanc::EmbeddedResources::GetFileResource(s, file);
154
155 const char* resource = s.size() ? s.c_str() : NULL;
156 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime);
157 }
158 }
159
160
161 extern "C"
162 {
163 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
164 {
165 OrthancPlugins::SetGlobalContext(context);
166 Orthanc::Logging::InitializePluginContext(context);
167
168 /* Check the version of the Orthanc core */
169 if (OrthancPluginCheckVersion(context) == 0)
170 {
171 char info[1024];
172 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
173 context->orthancVersion,
174 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
175 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
176 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
177 OrthancPluginLogError(context, info);
178 return -1;
179 }
180
181 try
182 {
183 std::string explorer;
184 Orthanc::EmbeddedResources::GetFileResource(
185 explorer, Orthanc::EmbeddedResources::ORTHANC_EXPLORER);
186 OrthancPluginExtendOrthancExplorer(OrthancPlugins::GetGlobalContext(), explorer.c_str());
187
188 OrthancPlugins::RegisterRestCallback
189 <ServeEmbeddedFile<Orthanc::EmbeddedResources::STONE_WEB_VIEWER_WASM> >
190 ("/stone-webviewer/StoneWebViewer.wasm", true);
191
192 OrthancPlugins::RegisterRestCallback
193 <ServeEmbeddedFile<Orthanc::EmbeddedResources::STONE_WEB_VIEWER_JS> >
194 ("/stone-webviewer/StoneWebViewer.js", true);
195
196 OrthancPlugins::RegisterRestCallback
197 <ServeEmbeddedFile<Orthanc::EmbeddedResources::STONE_WRAPPER> >
198 ("/stone-webviewer/stone.js", true);
199
200 OrthancPlugins::RegisterRestCallback
201 <ServeEmbeddedFolder<Orthanc::EmbeddedResources::IMAGES> >
202 ("/stone-webviewer/img/(.*)", true);
203
204 OrthancPlugins::RegisterRestCallback
205 <ServeEmbeddedFolder<Orthanc::EmbeddedResources::WEB_APPLICATION> >
206 ("/stone-webviewer/(.*)", true);
207
208 OrthancPluginRegisterOnChangeCallback(context, OnChangeCallback);
209 }
210 catch (...)
211 {
212 OrthancPlugins::LogError("Exception while initializing the Stone Web viewer plugin");
213 return -1;
214 }
215
216 return 0;
217 }
218
219
220 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
221 {
222 }
223
224
225 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
226 {
227 return PLUGIN_NAME;
228 }
229
230
231 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
232 {
233 return PLUGIN_VERSION;
234 }
235 }