comparison OrthancServer/Plugins/Samples/WebSkeleton/Framework/Plugin.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Plugins/Samples/WebSkeleton/Framework/Plugin.cpp@94f4a18a79cc
children d9473bd5ed43
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
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 General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * 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 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "../Configuration.h"
23
24 #include <orthanc/OrthancCPlugin.h>
25 #include <string>
26 #include <stdexcept>
27 #include <algorithm>
28 #include <sys/stat.h>
29
30 #if ORTHANC_PLUGIN_STANDALONE == 1
31 // This is an auto-generated file for standalone builds
32 #include <EmbeddedResources.h>
33 #endif
34
35 static OrthancPluginContext* context = NULL;
36
37
38 static const char* GetMimeType(const std::string& path)
39 {
40 size_t dot = path.find_last_of('.');
41
42 std::string extension = (dot == std::string::npos) ? "" : path.substr(dot);
43 std::transform(extension.begin(), extension.end(), extension.begin(), tolower);
44
45 if (extension == ".html")
46 {
47 return "text/html";
48 }
49 else if (extension == ".css")
50 {
51 return "text/css";
52 }
53 else if (extension == ".js")
54 {
55 return "application/javascript";
56 }
57 else if (extension == ".gif")
58 {
59 return "image/gif";
60 }
61 else if (extension == ".json")
62 {
63 return "application/json";
64 }
65 else if (extension == ".xml")
66 {
67 return "application/xml";
68 }
69 else if (extension == ".wasm")
70 {
71 return "application/wasm";
72 }
73 else if (extension == ".png")
74 {
75 return "image/png";
76 }
77 else if (extension == ".jpg" || extension == ".jpeg")
78 {
79 return "image/jpeg";
80 }
81 else
82 {
83 std::string s = "Unknown MIME type for extension: " + extension;
84 OrthancPluginLogWarning(context, s.c_str());
85 return "application/octet-stream";
86 }
87 }
88
89
90 static bool ReadFile(std::string& content,
91 const std::string& path)
92 {
93 struct stat s;
94 if (stat(path.c_str(), &s) != 0 ||
95 !(s.st_mode & S_IFREG))
96 {
97 // Either the path does not exist, or it is not a regular file
98 return false;
99 }
100
101 FILE* fp = fopen(path.c_str(), "rb");
102 if (fp == NULL)
103 {
104 return false;
105 }
106
107 long size;
108
109 if (fseek(fp, 0, SEEK_END) == -1 ||
110 (size = ftell(fp)) < 0)
111 {
112 fclose(fp);
113 return false;
114 }
115
116 content.resize(size);
117
118 if (fseek(fp, 0, SEEK_SET) == -1)
119 {
120 fclose(fp);
121 return false;
122 }
123
124 bool ok = true;
125
126 if (size > 0 &&
127 fread(&content[0], size, 1, fp) != 1)
128 {
129 ok = false;
130 }
131
132 fclose(fp);
133
134 return ok;
135 }
136
137
138 #if ORTHANC_PLUGIN_STANDALONE == 1
139 static OrthancPluginErrorCode ServeStaticResource(OrthancPluginRestOutput* output,
140 const char* url,
141 const OrthancPluginHttpRequest* request)
142 {
143 if (request->method != OrthancPluginHttpMethod_Get)
144 {
145 OrthancPluginSendMethodNotAllowed(context, output, "GET");
146 return OrthancPluginErrorCode_Success;
147 }
148
149 std::string path = "/" + std::string(request->groups[0]);
150 const char* mime = GetMimeType(path);
151
152 try
153 {
154 std::string s;
155 Orthanc::EmbeddedResources::GetDirectoryResource
156 (s, Orthanc::EmbeddedResources::STATIC_RESOURCES, path.c_str());
157
158 const char* resource = s.size() ? s.c_str() : NULL;
159 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime);
160 }
161 catch (std::runtime_error&)
162 {
163 std::string s = "Unknown static resource in plugin: " + std::string(request->groups[0]);
164 OrthancPluginLogError(context, s.c_str());
165 OrthancPluginSendHttpStatusCode(context, output, 404);
166 }
167
168 return OrthancPluginErrorCode_Success;
169 }
170 #endif
171
172
173 #if ORTHANC_PLUGIN_STANDALONE == 0
174 static OrthancPluginErrorCode ServeFolder(OrthancPluginRestOutput* output,
175 const char* url,
176 const OrthancPluginHttpRequest* request)
177 {
178 if (request->method != OrthancPluginHttpMethod_Get)
179 {
180 OrthancPluginSendMethodNotAllowed(context, output, "GET");
181 return OrthancPluginErrorCode_Success;
182 }
183
184 std::string path = ORTHANC_PLUGIN_RESOURCES_ROOT "/" + std::string(request->groups[0]);
185 const char* mime = GetMimeType(path);
186
187 std::string s;
188 if (ReadFile(s, path))
189 {
190 const char* resource = s.size() ? s.c_str() : NULL;
191 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime);
192 }
193 else
194 {
195 std::string s = "Unknown static resource in plugin: " + std::string(request->groups[0]);
196 OrthancPluginLogError(context, s.c_str());
197 OrthancPluginSendHttpStatusCode(context, output, 404);
198 }
199
200 return OrthancPluginErrorCode_Success;
201 }
202 #endif
203
204
205 static OrthancPluginErrorCode RedirectRoot(OrthancPluginRestOutput* output,
206 const char* url,
207 const OrthancPluginHttpRequest* request)
208 {
209 if (request->method != OrthancPluginHttpMethod_Get)
210 {
211 OrthancPluginSendMethodNotAllowed(context, output, "GET");
212 }
213 else
214 {
215 OrthancPluginRedirect(context, output, ORTHANC_PLUGIN_WEB_ROOT "index.html");
216 }
217
218 return OrthancPluginErrorCode_Success;
219 }
220
221
222 extern "C"
223 {
224 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
225 {
226 context = c;
227
228 /* Check the version of the Orthanc core */
229 if (OrthancPluginCheckVersion(c) == 0)
230 {
231 char info[256];
232 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
233 c->orthancVersion,
234 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
235 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
236 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
237 OrthancPluginLogError(context, info);
238 return -1;
239 }
240
241 /* Register the callbacks */
242
243 #if ORTHANC_PLUGIN_STANDALONE == 1
244 OrthancPluginLogInfo(context, "Serving static resources (standalone build)");
245 OrthancPluginRegisterRestCallback(context, ORTHANC_PLUGIN_WEB_ROOT "(.*)", ServeStaticResource);
246 #else
247 OrthancPluginLogInfo(context, "Serving resources from folder: " ORTHANC_PLUGIN_RESOURCES_ROOT);
248 OrthancPluginRegisterRestCallback(context, ORTHANC_PLUGIN_WEB_ROOT "(.*)", ServeFolder);
249 #endif
250
251 OrthancPluginRegisterRestCallback(context, "/", RedirectRoot);
252
253 return 0;
254 }
255
256
257 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
258 {
259 }
260
261
262 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
263 {
264 return ORTHANC_PLUGIN_NAME;
265 }
266
267
268 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
269 {
270 return ORTHANC_PLUGIN_VERSION;
271 }
272 }