comparison Core/Toolbox.cpp @ 2906:2a504fef4ed7

AutodetectMimeType() now using boost::filesystem
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Oct 2018 12:29:55 +0100
parents ae20fccdd867
children 0204af4ece6a
comparison
equal deleted inserted replaced
2905:ae20fccdd867 2906:2a504fef4ed7
308 308
309 return true; 309 return true;
310 } 310 }
311 311
312 312
313 std::string Toolbox::AutodetectMimeType(const std::string& path)
314 {
315 std::string contentType;
316 size_t lastDot = path.rfind('.');
317 size_t lastSlash = path.rfind('/');
318
319 if (lastDot == std::string::npos ||
320 (lastSlash != std::string::npos && lastDot < lastSlash))
321 {
322 // No trailing dot, unable to detect the content type
323 }
324 else
325 {
326 const char* extension = &path[lastDot + 1];
327
328 // http://en.wikipedia.org/wiki/Mime_types
329 // Text types
330 if (!strcmp(extension, "txt"))
331 contentType = MIME_PLAIN_TEXT;
332 else if (!strcmp(extension, "html"))
333 contentType = "text/html";
334 else if (!strcmp(extension, "xml"))
335 contentType = MIME_XML;
336 else if (!strcmp(extension, "css"))
337 contentType = "text/css";
338
339 // Application types
340 else if (!strcmp(extension, "js"))
341 contentType = "application/javascript";
342 else if (!strcmp(extension, "json"))
343 contentType = MIME_JSON;
344 else if (!strcmp(extension, "pdf"))
345 contentType = MIME_PDF;
346 else if (!strcmp(extension, "wasm"))
347 contentType = "application/wasm";
348
349 // Images types
350 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg"))
351 contentType = MIME_JPEG;
352 else if (!strcmp(extension, "gif"))
353 contentType = "image/gif";
354 else if (!strcmp(extension, "png"))
355 contentType = MIME_PNG;
356 else if (!strcmp(extension, "pam"))
357 contentType = MIME_PAM;
358 }
359
360 return contentType;
361 }
362
363
364 std::string Toolbox::FlattenUri(const UriComponents& components, 313 std::string Toolbox::FlattenUri(const UriComponents& components,
365 size_t fromLevel) 314 size_t fromLevel)
366 { 315 {
367 if (components.size() <= fromLevel) 316 if (components.size() <= fromLevel)
368 { 317 {