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