Mercurial > hg > orthanc
changeset 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 |
files | Core/HttpServer/EmbeddedResourceHttpHandler.cpp Core/HttpServer/HttpFileSender.cpp Core/SystemToolbox.cpp Core/SystemToolbox.h Core/Toolbox.cpp Core/Toolbox.h UnitTestsSources/UnitTestsMain.cpp |
diffstat | 7 files changed, 96 insertions(+), 74 deletions(-) [+] |
line wrap: on
line diff
--- a/Core/HttpServer/EmbeddedResourceHttpHandler.cpp Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/HttpServer/EmbeddedResourceHttpHandler.cpp Tue Oct 30 12:29:55 2018 +0100 @@ -36,6 +36,7 @@ #include "../Logging.h" #include "../OrthancException.h" +#include "../SystemToolbox.h" #include "HttpOutput.h" #include <stdio.h> @@ -77,7 +78,7 @@ } std::string resourcePath = Toolbox::FlattenUri(uri, baseUri_.size()); - std::string contentType = Toolbox::AutodetectMimeType(resourcePath); + std::string contentType = SystemToolbox::AutodetectMimeType(resourcePath); try {
--- a/Core/HttpServer/HttpFileSender.cpp Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/HttpServer/HttpFileSender.cpp Tue Oct 30 12:29:55 2018 +0100 @@ -36,6 +36,7 @@ #include "../OrthancException.h" #include "../Toolbox.h" +#include "../SystemToolbox.h" #include <boost/lexical_cast.hpp> @@ -47,7 +48,7 @@ if (contentType_.empty()) { - contentType_ = Toolbox::AutodetectMimeType(filename); + contentType_ = SystemToolbox::AutodetectMimeType(filename); } }
--- a/Core/SystemToolbox.cpp Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/SystemToolbox.cpp Tue Oct 30 12:29:55 2018 +0100 @@ -578,4 +578,71 @@ return threads; } } + + + std::string SystemToolbox::AutodetectMimeType(const std::string& path) + { + std::string extension = boost::filesystem::extension(path); + Toolbox::ToLowerCase(extension); + + // http://en.wikipedia.org/wiki/Mime_types + // Text types + if (extension == ".txt") + { + return MIME_PLAIN_TEXT; + } + else if (extension == ".html") + { + return "text/html"; + } + else if (extension == ".xml") + { + return MIME_XML; + } + else if (extension == ".css") + { + return "text/css"; + } + + // Application types + else if (extension == ".js") + { + return "application/javascript"; + } + else if (extension == ".json") + { + return MIME_JSON; + } + else if (extension == ".pdf") + { + return MIME_PDF; + } + else if (extension == ".wasm") + { + return "application/wasm"; + } + + // Images types + else if (extension == ".jpg" || + extension == ".jpeg") + { + return MIME_JPEG; + } + else if (extension == ".gif") + { + return "image/gif"; + } + else if (extension == ".png") + { + return MIME_PNG; + } + else if (extension == ".pam") + { + return MIME_PAM; + } + else + { + return ""; + } + } }
--- a/Core/SystemToolbox.h Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/SystemToolbox.h Tue Oct 30 12:29:55 2018 +0100 @@ -100,5 +100,7 @@ bool utc); unsigned int GetHardwareConcurrency(); + + std::string AutodetectMimeType(const std::string& path); } }
--- a/Core/Toolbox.cpp Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/Toolbox.cpp Tue Oct 30 12:29:55 2018 +0100 @@ -310,57 +310,6 @@ } - std::string Toolbox::AutodetectMimeType(const std::string& path) - { - std::string contentType; - size_t lastDot = path.rfind('.'); - size_t lastSlash = path.rfind('/'); - - if (lastDot == std::string::npos || - (lastSlash != std::string::npos && lastDot < lastSlash)) - { - // No trailing dot, unable to detect the content type - } - else - { - const char* extension = &path[lastDot + 1]; - - // http://en.wikipedia.org/wiki/Mime_types - // Text types - if (!strcmp(extension, "txt")) - contentType = MIME_PLAIN_TEXT; - else if (!strcmp(extension, "html")) - contentType = "text/html"; - else if (!strcmp(extension, "xml")) - contentType = MIME_XML; - else if (!strcmp(extension, "css")) - contentType = "text/css"; - - // Application types - else if (!strcmp(extension, "js")) - contentType = "application/javascript"; - else if (!strcmp(extension, "json")) - contentType = MIME_JSON; - else if (!strcmp(extension, "pdf")) - contentType = MIME_PDF; - else if (!strcmp(extension, "wasm")) - contentType = "application/wasm"; - - // Images types - else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg")) - contentType = MIME_JPEG; - else if (!strcmp(extension, "gif")) - contentType = "image/gif"; - else if (!strcmp(extension, "png")) - contentType = MIME_PNG; - else if (!strcmp(extension, "pam")) - contentType = MIME_PAM; - } - - return contentType; - } - - std::string Toolbox::FlattenUri(const UriComponents& components, size_t fromLevel) {
--- a/Core/Toolbox.h Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/Toolbox.h Tue Oct 30 12:29:55 2018 +0100 @@ -117,8 +117,6 @@ bool IsChildUri(const UriComponents& baseUri, const UriComponents& testedUri); - std::string AutodetectMimeType(const std::string& path); - std::string FlattenUri(const UriComponents& components, size_t fromLevel = 0);
--- a/UnitTestsSources/UnitTestsMain.cpp Tue Oct 30 11:55:23 2018 +0100 +++ b/UnitTestsSources/UnitTestsMain.cpp Tue Oct 30 12:29:55 2018 +0100 @@ -301,26 +301,30 @@ TEST(Uri, AutodetectMimeType) { - ASSERT_EQ("", Toolbox::AutodetectMimeType("../NOTES")); - ASSERT_EQ("", Toolbox::AutodetectMimeType("")); - ASSERT_EQ("", Toolbox::AutodetectMimeType("/")); - ASSERT_EQ("", Toolbox::AutodetectMimeType("a/a")); - - ASSERT_EQ("text/plain", Toolbox::AutodetectMimeType("../NOTES.txt")); - ASSERT_EQ("text/plain", Toolbox::AutodetectMimeType("../coucou.xml/NOTES.txt")); - ASSERT_EQ("application/xml", Toolbox::AutodetectMimeType("../.xml")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("../NOTES")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("/")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("a/a")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("..\\a\\")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("..\\a\\a")); - ASSERT_EQ("application/javascript", Toolbox::AutodetectMimeType("NOTES.js")); - ASSERT_EQ("application/json", Toolbox::AutodetectMimeType("NOTES.json")); - ASSERT_EQ("application/pdf", Toolbox::AutodetectMimeType("NOTES.pdf")); - ASSERT_EQ("text/css", Toolbox::AutodetectMimeType("NOTES.css")); - ASSERT_EQ("text/html", Toolbox::AutodetectMimeType("NOTES.html")); - ASSERT_EQ("text/plain", Toolbox::AutodetectMimeType("NOTES.txt")); - ASSERT_EQ("application/xml", Toolbox::AutodetectMimeType("NOTES.xml")); - ASSERT_EQ("image/gif", Toolbox::AutodetectMimeType("NOTES.gif")); - ASSERT_EQ("image/jpeg", Toolbox::AutodetectMimeType("NOTES.jpg")); - ASSERT_EQ("image/jpeg", Toolbox::AutodetectMimeType("NOTES.jpeg")); - ASSERT_EQ("image/png", Toolbox::AutodetectMimeType("NOTES.png")); + ASSERT_EQ("text/plain", SystemToolbox::AutodetectMimeType("../NOTES.txt")); + ASSERT_EQ("text/plain", SystemToolbox::AutodetectMimeType("../coucou.xml/NOTES.txt")); + ASSERT_EQ("application/xml", SystemToolbox::AutodetectMimeType("..\\coucou.\\NOTES.xml")); + ASSERT_EQ("application/xml", SystemToolbox::AutodetectMimeType("../.xml")); + ASSERT_EQ("application/xml", SystemToolbox::AutodetectMimeType("../.XmL")); + + ASSERT_EQ("application/javascript", SystemToolbox::AutodetectMimeType("NOTES.js")); + ASSERT_EQ("application/json", SystemToolbox::AutodetectMimeType("NOTES.json")); + ASSERT_EQ("application/pdf", SystemToolbox::AutodetectMimeType("NOTES.pdf")); + ASSERT_EQ("text/css", SystemToolbox::AutodetectMimeType("NOTES.css")); + ASSERT_EQ("text/html", SystemToolbox::AutodetectMimeType("NOTES.html")); + ASSERT_EQ("text/plain", SystemToolbox::AutodetectMimeType("NOTES.txt")); + ASSERT_EQ("application/xml", SystemToolbox::AutodetectMimeType("NOTES.xml")); + ASSERT_EQ("image/gif", SystemToolbox::AutodetectMimeType("NOTES.gif")); + ASSERT_EQ("image/jpeg", SystemToolbox::AutodetectMimeType("NOTES.jpg")); + ASSERT_EQ("image/jpeg", SystemToolbox::AutodetectMimeType("NOTES.jpeg")); + ASSERT_EQ("image/png", SystemToolbox::AutodetectMimeType("NOTES.png")); } TEST(Toolbox, ComputeMD5)