comparison Applications/Samples/RtViewerPlugin/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 OrthancStone/Samples/RtViewerPlugin/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/web-viewer", false))
39 {
40 throw Orthanc::OrthancException(
41 Orthanc::ErrorCode_InternalError,
42 "The Stone MPR RT viewer requires the Web Viewer plugin to be installed");
43 }
44 }
45 }
46 catch (Orthanc::OrthancException& e)
47 {
48 LOG(ERROR) << "Exception: " << e.What();
49 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
50 }
51
52 return OrthancPluginErrorCode_Success;
53 }
54
55 template <enum Orthanc::EmbeddedResources::DirectoryResourceId folder>
56 void ServeEmbeddedFolder(OrthancPluginRestOutput* output,
57 const char* url,
58 const OrthancPluginHttpRequest* request)
59 {
60 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext();
61
62 if (request->method != OrthancPluginHttpMethod_Get)
63 {
64 OrthancPluginSendMethodNotAllowed(context, output, "GET");
65 }
66 else
67 {
68 std::string path = "/" + std::string(request->groups[0]);
69 const char* mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path));
70
71 std::string s;
72 Orthanc::EmbeddedResources::GetDirectoryResource(s, folder, path.c_str());
73
74 const char* resource = s.size() ? s.c_str() : NULL;
75 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime);
76 }
77 }
78
79
80 template <enum Orthanc::EmbeddedResources::FileResourceId file>
81 void ServeEmbeddedFile(OrthancPluginRestOutput* output,
82 const char* url,
83 const OrthancPluginHttpRequest* request)
84 {
85 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext();
86
87 if (request->method != OrthancPluginHttpMethod_Get)
88 {
89 OrthancPluginSendMethodNotAllowed(context, output, "GET");
90 }
91 else
92 {
93 const char* mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(url));
94
95 std::string s;
96 Orthanc::EmbeddedResources::GetFileResource(s, file);
97
98 const char* resource = s.size() ? s.c_str() : NULL;
99 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime);
100 }
101 }
102
103
104 extern "C"
105 {
106 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
107 {
108 OrthancPlugins::SetGlobalContext(context);
109
110 #if ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 7, 2)
111 Orthanc::Logging::InitializePluginContext(context);
112 #else
113 Orthanc::Logging::Initialize(context);
114 #endif
115
116 /* Check the version of the Orthanc core */
117 if (OrthancPluginCheckVersion(context) == 0)
118 {
119 char info[1024];
120 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
121 context->orthancVersion,
122 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
123 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
124 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
125 OrthancPluginLogError(context, info);
126 return -1;
127 }
128
129 try
130 {
131 std::string explorer;
132 Orthanc::EmbeddedResources::GetFileResource(
133 explorer, Orthanc::EmbeddedResources::ORTHANC_EXPLORER);
134 OrthancPluginExtendOrthancExplorer(OrthancPlugins::GetGlobalContext(), explorer.c_str());
135
136 // RtViewer files below.
137 // ---------------------
138 // we do not serve the whole directory at once (with ServeEmbeddedFolder)
139 // because it contains uppercase characters that are forbidden by the
140 // resource embedding system
141
142 OrthancPlugins::RegisterRestCallback
143 <ServeEmbeddedFile<Orthanc::EmbeddedResources::RT_VIEWER_WASM_JS> >
144 ("/stone-rtviewer/RtViewerWasm.js", true);
145
146 OrthancPlugins::RegisterRestCallback
147 <ServeEmbeddedFile<Orthanc::EmbeddedResources::RT_VIEWER_WASM> >
148 ("/stone-rtviewer/RtViewerWasm.wasm", true);
149
150 OrthancPlugins::RegisterRestCallback
151 <ServeEmbeddedFile<Orthanc::EmbeddedResources::RT_VIEWER_WASM_APP_JS> >
152 ("/stone-rtviewer/RtViewerWasmApp.js", true);
153
154 OrthancPlugins::RegisterRestCallback
155 <ServeEmbeddedFile<Orthanc::EmbeddedResources::RT_VIEWER_INDEX_HTML> >
156 ("/stone-rtviewer/index.html", true);
157
158 OrthancPluginRegisterOnChangeCallback(context, OnChangeCallback);
159 }
160 catch (...)
161 {
162 OrthancPlugins::LogError("Exception while initializing the Stone Web viewer plugin");
163 return -1;
164 }
165
166 return 0;
167 }
168
169
170 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
171 {
172 }
173
174
175 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
176 {
177 return PLUGIN_NAME;
178 }
179
180
181 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
182 {
183 return PLUGIN_VERSION;
184 }
185 }