comparison Plugins/Samples/ServeFolders/Plugin.cpp @ 1597:415dfd1d1c61

Improvements to the sample "ServeFolders" plugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 28 Aug 2015 13:47:23 +0200
parents 334d3a92ed83
children 77c4cc4def0f
comparison
equal deleted inserted replaced
1596:f2e3d030ea59 1597:415dfd1d1c61
154 return true; 154 return true;
155 } 155 }
156 } 156 }
157 157
158 158
159 static int32_t IndexCallback(OrthancPluginRestOutput* output, 159 static int32_t FolderCallback(OrthancPluginRestOutput* output,
160 const char* url, 160 const char* url,
161 const OrthancPluginHttpRequest* request) 161 const OrthancPluginHttpRequest* request)
162 { 162 {
163 namespace fs = boost::filesystem; 163 namespace fs = boost::filesystem;
164 164
165 if (request->method != OrthancPluginHttpMethod_Get) 165 if (request->method != OrthancPluginHttpMethod_Get)
166 { 166 {
167 OrthancPluginSendMethodNotAllowed(context_, output, "GET"); 167 OrthancPluginSendMethodNotAllowed(context_, output, "GET");
168 return 0; 168 return 0;
169 } 169 }
170 170
171 std::string folder; 171 std::string folder;
172
172 if (LookupFolder(folder, output, request)) 173 if (LookupFolder(folder, output, request))
173 { 174 {
174 std::string baseUri = "/" + std::string(request->groups[0]); 175 const fs::path item(request->groups[1]);
175 176 const fs::path parent((fs::path(folder) / item).parent_path());
176 if (fs::is_regular_file(fs::path(folder) / "index.html")) 177
177 { 178 if (item.filename().string() == "index.html" &&
178 std::string s = baseUri + "/index.html"; 179 fs::is_directory(parent) &&
179 OrthancPluginRedirect(context_, output, s.c_str()); 180 !fs::is_regular_file(fs::path(folder) / item))
180 } 181 {
181 else 182 // On-the-fly generation of an "index.html"
182 {
183 std::string s; 183 std::string s;
184 s += "<html>\n"; 184 s += "<html>\n";
185 s += " <body>\n"; 185 s += " <body>\n";
186 s += " <ul>\n"; 186 s += " <ul>\n";
187 187
188 fs::directory_iterator end; 188 fs::directory_iterator end;
189 for (fs::directory_iterator it(folder) ; it != end; ++it) 189
190 for (fs::directory_iterator it(parent) ; it != end; ++it)
191 {
192 if (fs::is_directory(it->status()))
193 {
194 std::string f = it->path().filename().string();
195 s += " <li><a href=\"" + f + "/index.html\">" + f + "/</a></li>\n";
196 }
197 }
198
199 for (fs::directory_iterator it(parent) ; it != end; ++it)
190 { 200 {
191 if (fs::is_regular_file(it->status())) 201 if (fs::is_regular_file(it->status()))
192 { 202 {
193 std::string f = it->path().filename().string(); 203 std::string f = it->path().filename().string();
194 s += " <li><a href=\"" + baseUri + "/" + f + "\">" + f + "</a></li>"; 204 s += " <li><a href=\"" + f + "\">" + f + "</a></li>\n";
195 } 205 }
196 } 206 }
197 207
198 s += " </ul>\n"; 208 s += " </ul>\n";
199 s += " </body>\n"; 209 s += " </body>\n";
200 s += "</html>\n"; 210 s += "</html>\n";
201 211
202 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html"); 212 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html");
203 } 213 }
204 }
205
206 return 0;
207 }
208
209
210 static int32_t FolderCallback(OrthancPluginRestOutput* output,
211 const char* url,
212 const OrthancPluginHttpRequest* request)
213 {
214 if (request->method != OrthancPluginHttpMethod_Get)
215 {
216 OrthancPluginSendMethodNotAllowed(context_, output, "GET");
217 return 0;
218 }
219
220 std::string folder;
221
222 if (LookupFolder(folder, output, request))
223 {
224 const std::string item = request->groups[1];
225
226 std::string path = folder + "/" + item;
227 const char* mime = GetMimeType(path);
228
229 std::string s;
230 if (ReadFile(s, path))
231 {
232 const char* resource = s.size() ? s.c_str() : NULL;
233 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime);
234 }
235 else 214 else
236 { 215 {
237 std::string s = "Inexistent file in served folder: " + path; 216 std::string path = folder + "/" + item.string();
238 OrthancPluginLogError(context_, s.c_str()); 217 const char* mime = GetMimeType(path);
239 OrthancPluginSendHttpStatusCode(context_, output, 404); 218
219 std::string s;
220 if (ReadFile(s, path))
221 {
222 const char* resource = s.size() ? s.c_str() : NULL;
223 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime);
224 }
225 else
226 {
227 std::string s = "Inexistent file in served folder: " + path;
228 OrthancPluginLogError(context_, s.c_str());
229 OrthancPluginSendHttpStatusCode(context_, output, 404);
230 }
240 } 231 }
241 } 232 }
242 233
243 return 0; 234 return 0;
244 } 235 }
264 { 255 {
265 s += "<ul>\n"; 256 s += "<ul>\n";
266 for (std::map<std::string, std::string>::const_iterator 257 for (std::map<std::string, std::string>::const_iterator
267 it = folders_.begin(); it != folders_.end(); ++it) 258 it = folders_.begin(); it != folders_.end(); ++it)
268 { 259 {
269 s += "<li><a href=\"/" + it->first + "\">" + it->first + "</li>\n"; 260 // The URI is relative to INDEX_URI ("/app/plugin-serve-folders.html")
261 s += "<li><a href=\"../" + it->first + "/index.html\">" + it->first + "</li>\n";
270 } 262 }
271 263
272 s += "</ul>\n"; 264 s += "</ul>\n";
273 } 265 }
274 266
359 return -1; 351 return -1;
360 } 352 }
361 353
362 folders_[baseUri] = folder; 354 folders_[baseUri] = folder;
363 355
364 // Register the callback to serve the index of the folder
365 {
366 const std::string regex = "/(" + baseUri + ")";
367 OrthancPluginRegisterRestCallback(context, regex.c_str(), IndexCallback);
368 }
369
370 // Register the callback to serve the folder 356 // Register the callback to serve the folder
371 { 357 {
372 const std::string regex = "/(" + baseUri + ")/(.*)"; 358 const std::string regex = "/(" + baseUri + ")/(.*)";
373 OrthancPluginRegisterRestCallback(context, regex.c_str(), FolderCallback); 359 OrthancPluginRegisterRestCallback(context, regex.c_str(), FolderCallback);
374 } 360 }