Mercurial > hg > orthanc-stone
comparison OrthancStone/Samples/RtViewerPlugin/Plugin.cpp @ 1527:4c4b267e4004
RtViewerPlugin : similar to the StoneWebPlugin, but for the sole RtViewer sample
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Sun, 02 Aug 2020 15:13:58 +0200 |
parents | |
children | c815ad10a10f |
comparison
equal
deleted
inserted
replaced
1526:61023b0d39c8 | 1527:4c4b267e4004 |
---|---|
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 template <enum Orthanc::EmbeddedResources::DirectoryResourceId folder> | |
30 void ServeEmbeddedFolder(OrthancPluginRestOutput* output, | |
31 const char* url, | |
32 const OrthancPluginHttpRequest* request) | |
33 { | |
34 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext(); | |
35 | |
36 if (request->method != OrthancPluginHttpMethod_Get) | |
37 { | |
38 OrthancPluginSendMethodNotAllowed(context, output, "GET"); | |
39 } | |
40 else | |
41 { | |
42 std::string path = "/" + std::string(request->groups[0]); | |
43 const char* mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path)); | |
44 | |
45 std::string s; | |
46 Orthanc::EmbeddedResources::GetDirectoryResource(s, folder, path.c_str()); | |
47 | |
48 const char* resource = s.size() ? s.c_str() : NULL; | |
49 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime); | |
50 } | |
51 } | |
52 | |
53 | |
54 template <enum Orthanc::EmbeddedResources::FileResourceId file> | |
55 void ServeEmbeddedFile(OrthancPluginRestOutput* output, | |
56 const char* url, | |
57 const OrthancPluginHttpRequest* request) | |
58 { | |
59 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext(); | |
60 | |
61 if (request->method != OrthancPluginHttpMethod_Get) | |
62 { | |
63 OrthancPluginSendMethodNotAllowed(context, output, "GET"); | |
64 } | |
65 else | |
66 { | |
67 const char* mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(url)); | |
68 | |
69 std::string s; | |
70 Orthanc::EmbeddedResources::GetFileResource(s, file); | |
71 | |
72 const char* resource = s.size() ? s.c_str() : NULL; | |
73 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime); | |
74 } | |
75 } | |
76 | |
77 | |
78 extern "C" | |
79 { | |
80 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) | |
81 { | |
82 OrthancPlugins::SetGlobalContext(context); | |
83 | |
84 #if defined(ORTHANC_FRAMEWORK_VERSION_IS_ABOVE) // This indicates Orthanc framework >= 1.7.2 | |
85 Orthanc::Logging::InitializePluginContext(context); | |
86 #else | |
87 Orthanc::Logging::Initialize(context); | |
88 #endif | |
89 | |
90 /* Check the version of the Orthanc core */ | |
91 if (OrthancPluginCheckVersion(context) == 0) | |
92 { | |
93 char info[1024]; | |
94 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", | |
95 context->orthancVersion, | |
96 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, | |
97 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, | |
98 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); | |
99 OrthancPluginLogError(context, info); | |
100 return -1; | |
101 } | |
102 | |
103 try | |
104 { | |
105 std::string explorer; | |
106 Orthanc::EmbeddedResources::GetFileResource( | |
107 explorer, Orthanc::EmbeddedResources::ORTHANC_EXPLORER); | |
108 OrthancPluginExtendOrthancExplorer(OrthancPlugins::GetGlobalContext(), explorer.c_str()); | |
109 | |
110 // RtViewer files below. | |
111 // --------------------- | |
112 // we do not serve the whole directory at once (with ServeEmbeddedFolder) | |
113 // because it contains uppercase characters that are forbidden by the | |
114 // resource embedding system | |
115 | |
116 OrthancPlugins::RegisterRestCallback | |
117 <ServeEmbeddedFile<Orthanc::EmbeddedResources::RT_VIEWER_WASM_JS> > | |
118 ("/stone-rtviewer/RtViewerWasm.js", true); | |
119 | |
120 OrthancPlugins::RegisterRestCallback | |
121 <ServeEmbeddedFile<Orthanc::EmbeddedResources::RT_VIEWER_WASM> > | |
122 ("/stone-rtviewer/RtViewerWasm.wasm", true); | |
123 | |
124 OrthancPlugins::RegisterRestCallback | |
125 <ServeEmbeddedFile<Orthanc::EmbeddedResources::RT_VIEWER_WASM_APP_JS> > | |
126 ("/stone-rtviewer/RtViewerWasmApp.js", true); | |
127 | |
128 OrthancPlugins::RegisterRestCallback | |
129 <ServeEmbeddedFile<Orthanc::EmbeddedResources::RT_VIEWER_INDEX_HTML> > | |
130 ("/stone-rtviewer/index.html", true); | |
131 } | |
132 catch (...) | |
133 { | |
134 OrthancPlugins::LogError("Exception while initializing the Stone Web viewer plugin"); | |
135 return -1; | |
136 } | |
137 | |
138 return 0; | |
139 } | |
140 | |
141 | |
142 ORTHANC_PLUGINS_API void OrthancPluginFinalize() | |
143 { | |
144 } | |
145 | |
146 | |
147 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() | |
148 { | |
149 return PLUGIN_NAME; | |
150 } | |
151 | |
152 | |
153 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion() | |
154 { | |
155 return PLUGIN_VERSION; | |
156 } | |
157 } |