comparison Sources/Plugin.cpp @ 0:4e889a8e8be2

initial commit of the viewer plugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 04 Jul 2023 19:15:27 +0200
parents
children 0f03a8a0bd6f
comparison
equal deleted inserted replaced
-1:000000000000 0:4e889a8e8be2
1 /**
2 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium
3 * SPDX-License-Identifier: GPL-3.0-or-later
4 */
5
6 /**
7 * STL 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 Three.js static assets are gzipped by the
41 * "EmbedStaticAssets.py" script, we use a cache to maintain the
42 * uncompressed assets in order 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(OrthancPluginRestOutput* output,
63 const std::string& path)
64 {
65 const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path));
66
67 {
68 // Check whether the cache already contains the resource
69 boost::shared_lock<boost::shared_mutex> lock(mutex_);
70
71 Content::const_iterator found = content_.find(path);
72
73 if (found != content_.end())
74 {
75 assert(found->second != NULL);
76 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, found->second->c_str(), found->second->size(), mime.c_str());
77 return;
78 }
79 }
80
81 // This resource has not been cached yet
82
83 std::unique_ptr<std::string> item(new std::string);
84 ReadStaticAsset(*item, path);
85 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, item->c_str(), item->size(), mime.c_str());
86
87 {
88 // Store the resource into the cache
89 boost::unique_lock<boost::shared_mutex> lock(mutex_);
90
91 if (content_.find(path) == content_.end())
92 {
93 content_[path] = item.release();
94 }
95 }
96 }
97 };
98
99
100 static ResourcesCache cache_;
101 static bool hasCreateDicomStl_;
102
103 void ServeFile(OrthancPluginRestOutput* output,
104 const char* url,
105 const OrthancPluginHttpRequest* request)
106 {
107 std::string file = request->groups[0];
108
109 if (file == "viewer.html")
110 {
111 std::string s;
112 Orthanc::EmbeddedResources::GetFileResource(s, Orthanc::EmbeddedResources::VIEWER_HTML);
113 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), Orthanc::EnumerationToString(Orthanc::MimeType_Html));
114 }
115 else if (file == "viewer.js")
116 {
117 std::string s;
118 Orthanc::EmbeddedResources::GetFileResource(s, Orthanc::EmbeddedResources::VIEWER_JS);
119 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), Orthanc::EnumerationToString(Orthanc::MimeType_JavaScript));
120 }
121 else
122 {
123 cache_.Answer(output, file);
124 }
125 }
126
127
128 extern "C"
129 {
130 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
131 {
132 OrthancPlugins::SetGlobalContext(context);
133
134 /* Check the version of the Orthanc core */
135 if (OrthancPluginCheckVersion(OrthancPlugins::GetGlobalContext()) == 0)
136 {
137 char info[1024];
138 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
139 OrthancPlugins::GetGlobalContext()->orthancVersion,
140 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
141 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
142 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
143 OrthancPluginLogError(OrthancPlugins::GetGlobalContext(), info);
144 return -1;
145 }
146
147 #if ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 7, 2)
148 Orthanc::Logging::InitializePluginContext(context);
149 #else
150 Orthanc::Logging::Initialize(context);
151 #endif
152
153 hasCreateDicomStl_ = OrthancPlugins::CheckMinimalOrthancVersion(1, 12, 1);
154
155 if (!hasCreateDicomStl_)
156 {
157 LOG(WARNING) << "Your version of Orthanc (" << std::string(context->orthancVersion)
158 << ") is insufficient to create DICOM STL, it should be above 1.12.1";
159 }
160
161 OrthancPluginSetDescription(context, "STL plugin for Orthanc.");
162
163 OrthancPlugins::RegisterRestCallback<ServeFile>("/stl/(.*)", true);
164
165 // Extend the default Orthanc Explorer with custom JavaScript for STL
166 std::string explorer;
167 Orthanc::EmbeddedResources::GetFileResource(explorer, Orthanc::EmbeddedResources::ORTHANC_EXPLORER);
168 OrthancPluginExtendOrthancExplorer(OrthancPlugins::GetGlobalContext(), explorer.c_str());
169
170 return 0;
171 }
172
173
174 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
175 {
176 }
177
178
179 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
180 {
181 return "stl";
182 }
183
184
185 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
186 {
187 return ORTHANC_STL_VERSION;
188 }
189 }