Mercurial > hg > orthanc-ohif
comparison Sources/Plugin.cpp @ 0:39585ba26f20
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 15 Jun 2023 09:48:46 +0200 |
parents | |
children | cbc4be362700 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:39585ba26f20 |
---|---|
1 /** | |
2 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium | |
3 * SPDX-License-Identifier: GPL-3.0-or-later | |
4 */ | |
5 | |
6 /** | |
7 * OHIF plugin for Orthanc | |
8 * Copyright (C) 2023 Sebastien Jodogne, UCLouvain, Belgium | |
9 * | |
10 * This program is free software: you can redistribute it and/or | |
11 * modify it under the terms of the GNU General Public License as | |
12 * published by the Free Software Foundation, either version 3 of the | |
13 * License, or (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, but | |
16 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 * General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 **/ | |
23 | |
24 | |
25 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" | |
26 | |
27 #include <Logging.h> | |
28 #include <SystemToolbox.h> | |
29 | |
30 #include <EmbeddedResources.h> | |
31 | |
32 #include <boost/thread/shared_mutex.hpp> | |
33 | |
34 // Forward declaration | |
35 void ReadStaticAsset(std::string& target, | |
36 const std::string& path); | |
37 | |
38 | |
39 /** | |
40 * As the OHIF static assets are gzipped by the "EmbedStaticAssets.py" | |
41 * script, we use a cache to maintain the uncompressed assets in order | |
42 * to avoid multiple gzip decodings. | |
43 **/ | |
44 class ResourcesCache : public boost::noncopyable | |
45 { | |
46 private: | |
47 typedef std::map<std::string, std::string*> Content; | |
48 | |
49 boost::shared_mutex mutex_; | |
50 Content content_; | |
51 | |
52 public: | |
53 ~ResourcesCache() | |
54 { | |
55 for (Content::iterator it = content_.begin(); it != content_.end(); ++it) | |
56 { | |
57 assert(it->second != NULL); | |
58 delete it->second; | |
59 } | |
60 } | |
61 | |
62 void Answer(OrthancPluginContext* context, | |
63 OrthancPluginRestOutput* output, | |
64 const std::string& path) | |
65 { | |
66 const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path)); | |
67 | |
68 { | |
69 // Check whether the cache already contains the resource | |
70 boost::shared_lock<boost::shared_mutex> lock(mutex_); | |
71 | |
72 Content::const_iterator found = content_.find(path); | |
73 | |
74 if (found != content_.end()) | |
75 { | |
76 assert(found->second != NULL); | |
77 OrthancPluginAnswerBuffer(context, output, found->second->c_str(), found->second->size(), mime.c_str()); | |
78 return; | |
79 } | |
80 } | |
81 | |
82 // This resource has not been cached yet | |
83 | |
84 std::unique_ptr<std::string> item(new std::string); | |
85 ReadStaticAsset(*item, path); | |
86 OrthancPluginAnswerBuffer(context, output, item->c_str(), item->size(), mime.c_str()); | |
87 | |
88 { | |
89 // Store the resource into the cache | |
90 boost::unique_lock<boost::shared_mutex> lock(mutex_); | |
91 | |
92 if (content_.find(path) == content_.end()) | |
93 { | |
94 content_[path] = item.release(); | |
95 } | |
96 } | |
97 } | |
98 }; | |
99 | |
100 | |
101 static ResourcesCache cache_; | |
102 | |
103 void ServeFile(OrthancPluginRestOutput* output, | |
104 const char* url, | |
105 const OrthancPluginHttpRequest* request) | |
106 { | |
107 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext(); | |
108 | |
109 // The next 3 HTTP headers are required to enable SharedArrayBuffer | |
110 // (https://web.dev/coop-coep/) | |
111 OrthancPluginSetHttpHeader(context, output, "Cross-Origin-Embedder-Policy", "require-corp"); | |
112 OrthancPluginSetHttpHeader(context, output, "Cross-Origin-Opener-Policy", "same-origin"); | |
113 OrthancPluginSetHttpHeader(context, output, "Cross-Origin-Resource-Policy", "same-origin"); | |
114 | |
115 std::string uri = request->groups[0]; | |
116 | |
117 if (uri == "app-config.js") | |
118 { | |
119 std::string system, user; | |
120 Orthanc::EmbeddedResources::GetFileResource(system, Orthanc::EmbeddedResources::APP_CONFIG_SYSTEM); | |
121 Orthanc::EmbeddedResources::GetFileResource(user, Orthanc::EmbeddedResources::APP_CONFIG_USER); | |
122 | |
123 std::string s = (user + "\n" + system); | |
124 OrthancPluginAnswerBuffer(context, output, s.c_str(), s.size(), "application/json"); | |
125 } | |
126 else if (uri == "viewer") | |
127 { | |
128 cache_.Answer(context, output, "index.html"); | |
129 } | |
130 else | |
131 { | |
132 cache_.Answer(context, output, uri); | |
133 } | |
134 } | |
135 | |
136 | |
137 OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType, | |
138 OrthancPluginResourceType resourceType, | |
139 const char* resourceId) | |
140 { | |
141 try | |
142 { | |
143 if (changeType == OrthancPluginChangeType_OrthancStarted) | |
144 { | |
145 Json::Value info; | |
146 if (!OrthancPlugins::RestApiGet(info, "/plugins/dicom-web", false)) | |
147 { | |
148 throw Orthanc::OrthancException( | |
149 Orthanc::ErrorCode_InternalError, | |
150 "The OHIF plugin requires the DICOMweb plugin to be installed"); | |
151 } | |
152 | |
153 if (info.type() != Json::objectValue || | |
154 !info.isMember("ID") || | |
155 !info.isMember("Version") || | |
156 info["ID"].type() != Json::stringValue || | |
157 info["Version"].type() != Json::stringValue || | |
158 info["ID"].asString() != "dicom-web") | |
159 { | |
160 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, | |
161 "The DICOMweb plugin is not properly installed"); | |
162 } | |
163 } | |
164 } | |
165 catch (Orthanc::OrthancException& e) | |
166 { | |
167 LOG(ERROR) << "Exception: " << e.What(); | |
168 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
169 } | |
170 | |
171 return OrthancPluginErrorCode_Success; | |
172 } | |
173 | |
174 | |
175 extern "C" | |
176 { | |
177 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) | |
178 { | |
179 OrthancPlugins::SetGlobalContext(context); | |
180 | |
181 /* Check the version of the Orthanc core */ | |
182 if (OrthancPluginCheckVersion(context) == 0) | |
183 { | |
184 char info[1024]; | |
185 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", | |
186 context->orthancVersion, | |
187 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, | |
188 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, | |
189 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); | |
190 OrthancPluginLogError(context, info); | |
191 return -1; | |
192 } | |
193 | |
194 #if ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 7, 2) | |
195 Orthanc::Logging::InitializePluginContext(context); | |
196 #else | |
197 Orthanc::Logging::Initialize(context); | |
198 #endif | |
199 | |
200 OrthancPluginSetDescription(context, "OHIF plugin for Orthanc."); | |
201 | |
202 OrthancPlugins::RegisterRestCallback<ServeFile>("/ohif/(.*)", true); | |
203 | |
204 OrthancPluginRegisterOnChangeCallback(context, OnChangeCallback); | |
205 | |
206 // Extend the default Orthanc Explorer with custom JavaScript for OHIF | |
207 std::string explorer; | |
208 Orthanc::EmbeddedResources::GetFileResource(explorer, Orthanc::EmbeddedResources::ORTHANC_EXPLORER); | |
209 OrthancPluginExtendOrthancExplorer(context, explorer.c_str()); | |
210 | |
211 return 0; | |
212 } | |
213 | |
214 | |
215 ORTHANC_PLUGINS_API void OrthancPluginFinalize() | |
216 { | |
217 } | |
218 | |
219 | |
220 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() | |
221 { | |
222 return "ohif"; | |
223 } | |
224 | |
225 | |
226 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion() | |
227 { | |
228 return ORTHANC_OHIF_VERSION; | |
229 } | |
230 } |