From a3e5a9b22246665b41d04d3d7e43a204547d662c Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Sat, 15 Apr 2017 18:20:54 +0200 Subject: [PATCH] MIME: add lookup function for file name extension for MIME type --- src/common/extern_data.cpp | 7 +++++++ src/common/extern_data.h | 1 + 2 files changed, 8 insertions(+) diff --git a/src/common/extern_data.cpp b/src/common/extern_data.cpp index bd96e5d7e..f9031f33a 100644 --- a/src/common/extern_data.cpp +++ b/src/common/extern_data.cpp @@ -2636,6 +2636,13 @@ guess_mime_type_by_ext(std::string ext) { return ""; } +std::string +primary_file_extension_for_mime_type(std::string const &mime_type) { + auto itr = brng::find_if(mime_types, [&mime_type](auto const &m) { return m.name == mime_type; }); + + return (itr != mime_types.end()) && !itr->extensions.empty() ? itr->extensions[0] : std::string{}; +} + #if HAVE_MAGIC_H static std::string guess_mime_type_by_content(magic_t &m, diff --git a/src/common/extern_data.h b/src/common/extern_data.h index 3fc4e7585..79327b9cf 100644 --- a/src/common/extern_data.h +++ b/src/common/extern_data.h @@ -37,6 +37,7 @@ extern std::vector const g_popular_country_codes; extern std::vector const mime_types; std::string guess_mime_type(std::string ext, bool is_file); +std::string primary_file_extension_for_mime_type(std::string const &mime_type); boost::optional map_to_cctld(std::string const &s); #endif // MTX_COMMON_EXTERN_DATA_H