comparison Sources/Plugin.cpp @ 0:3f1cf4a8e31f

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 22 Mar 2023 10:16:47 +0100
parents
children fdb3093ab802
comparison
equal deleted inserted replaced
-1:000000000000 0:3f1cf4a8e31f
1 /**
2 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium
3 * SPDX-License-Identifier: GPL-3.0-or-later
4 */
5
6 /**
7 * Kitware's VolView 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 VolView static assets are gzipped by the "EmbedResources.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(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
102 void ServeFile(OrthancPluginRestOutput* output,
103 const char* url,
104 const OrthancPluginHttpRequest* request)
105 {
106 cache_.Answer(output, request->groups[0]);
107 }
108
109 extern "C"
110 {
111 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
112 {
113 OrthancPlugins::SetGlobalContext(context);
114
115 /* Check the version of the Orthanc core */
116 if (OrthancPluginCheckVersion(OrthancPlugins::GetGlobalContext()) == 0)
117 {
118 char info[1024];
119 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
120 OrthancPlugins::GetGlobalContext()->orthancVersion,
121 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
122 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
123 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
124 OrthancPluginLogError(OrthancPlugins::GetGlobalContext(), info);
125 return -1;
126 }
127
128 #if ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 7, 2)
129 Orthanc::Logging::InitializePluginContext(context);
130 #else
131 Orthanc::Logging::Initialize(context);
132 #endif
133
134 OrthancPluginSetDescription(context, "Kitware's VolView for Orthanc.");
135
136 OrthancPlugins::RegisterRestCallback<ServeFile>("/volview/(.*)", true);
137
138 // Extend the default Orthanc Explorer with custom JavaScript for VolView
139 std::string explorer;
140 Orthanc::EmbeddedResources::GetFileResource(explorer, Orthanc::EmbeddedResources::ORTHANC_EXPLORER);
141 OrthancPluginExtendOrthancExplorer(OrthancPlugins::GetGlobalContext(), explorer.c_str());
142
143 return 0;
144 }
145
146
147 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
148 {
149 }
150
151
152 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
153 {
154 return "volview";
155 }
156
157
158 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
159 {
160 return ORTHANC_VOLVIEW_VERSION;
161 }
162 }