| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
| 2 | /* |
| 3 | * This file is part of the LibreOffice project. |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * This file incorporates work covered by the following license notice: |
| 10 | * |
| 11 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 | * contributor license agreements. See the NOTICE file distributed |
| 13 | * with this work for additional information regarding copyright |
| 14 | * ownership. The ASF licenses this file to you under the Apache |
| 15 | * License, Version 2.0 (the "License"); you may not use this file |
| 16 | * except in compliance with the License. You may obtain a copy of |
| 17 | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 | */ |
| 19 | |
| 20 | #include <comphelper/graphicmimetype.hxx> |
| 21 | #include <comphelper/mediamimetype.hxx> |
| 22 | |
| 23 | #include <map> |
| 24 | #include <set> |
| 25 | |
| 26 | #include <com/sun/star/beans/PropertyValue.hpp> |
| 27 | #include <com/sun/star/beans/XPropertySet.hpp> |
| 28 | #include <com/sun/star/graphic/GraphicProvider.hpp> |
| 29 | #include <com/sun/star/graphic/XGraphicProvider.hpp> |
| 30 | #include <com/sun/star/io/XInputStream.hpp> |
| 31 | #include <com/sun/star/uno/Reference.hxx> |
| 32 | |
| 33 | #include <comphelper/processfactory.hxx> |
| 34 | #include <comphelper/propertyvalue.hxx> |
| 35 | #include <vcl/salctype.hxx> |
| 36 | |
| 37 | #include <frozen/bits/elsa_std.h> |
| 38 | #include <frozen/unordered_map.h> |
| 39 | |
| 40 | using namespace css; |
| 41 | using namespace css::beans; |
| 42 | using namespace css::graphic; |
| 43 | using namespace css::io; |
| 44 | using namespace css::uno; |
| 45 | |
| 46 | namespace comphelper |
| 47 | { |
| 48 | OUString GraphicMimeTypeHelper::GetMimeTypeForExtension(std::string_view rExt) |
| 49 | { |
| 50 | #if defined(_MSC_VER) && !defined(__clang__) |
| 51 | static const |
| 52 | #else |
| 53 | static constexpr |
| 54 | #endif |
| 55 | auto aMapper |
| 56 | = frozen::make_unordered_map<std::string_view, OUString>( |
| 57 | { { "gif", u"image/gif"_ustr }, |
| 58 | { "png", u"image/png"_ustr }, |
| 59 | { "jpg", u"image/jpeg"_ustr }, |
| 60 | { "tif", u"image/tiff"_ustr }, |
| 61 | { "svg", u"image/svg+xml"_ustr }, |
| 62 | { "pdf", u"application/pdf"_ustr }, |
| 63 | { "wmf", u"image/x-wmf"_ustr }, |
| 64 | { "emf", u"image/x-emf"_ustr }, |
| 65 | { "eps", u"image/x-eps"_ustr }, |
| 66 | { "bmp", u"image/bmp"_ustr }, |
| 67 | { "pct", u"image/x-pict"_ustr }, |
| 68 | { "svm", u"image/x-svm"_ustr } }); |
| 69 | |
| 70 | auto iterator = aMapper.find(rExt); |
| 71 | if (iterator != aMapper.end()) |
| 72 | return iterator->second; |
| 73 | |
| 74 | return OUString(); |
| 75 | } |
| 76 | |
| 77 | OUString GraphicMimeTypeHelper::GetMimeTypeForXGraphic(const Reference<XGraphic>& xGraphic) |
| 78 | { |
| 79 | OUString aSourceMimeType; |
| 80 | Reference<XPropertySet> const xGraphicPropertySet(xGraphic, UNO_QUERY); |
| 81 | if (xGraphicPropertySet.is() && // it's null if it's an external link |
| 82 | (xGraphicPropertySet->getPropertyValue(u"MimeType"_ustr) >>= aSourceMimeType)) |
| 83 | { |
| 84 | return aSourceMimeType; |
| 85 | } |
| 86 | return u""_ustr; |
| 87 | } |
| 88 | |
| 89 | OUString |
| 90 | GraphicMimeTypeHelper::GetMimeTypeForImageStream(const Reference<XInputStream>& xInputStream) |
| 91 | { |
| 92 | // Create the graphic to retrieve the mimetype from it |
| 93 | Reference<XGraphicProvider> xProvider |
| 94 | = css::graphic::GraphicProvider::create(comphelper::getProcessComponentContext()); |
| 95 | Sequence<PropertyValue> aMediaProperties{ comphelper::makePropertyValue(u"InputStream"_ustr, |
| 96 | xInputStream) }; |
| 97 | Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties)); |
| 98 | |
| 99 | return GetMimeTypeForXGraphic(xGraphic); |
| 100 | } |
| 101 | |
| 102 | OUString GraphicMimeTypeHelper::GetMimeTypeForConvertDataFormat(ConvertDataFormat convertDataFormat) |
| 103 | { |
| 104 | switch (convertDataFormat) |
| 105 | { |
| 106 | case ConvertDataFormat::BMP: |
| 107 | return u"image/bmp"_ustr; |
| 108 | case ConvertDataFormat::GIF: |
| 109 | return u"image/gif"_ustr; |
| 110 | case ConvertDataFormat::JPG: |
| 111 | return u"image/jpeg"_ustr; |
| 112 | case ConvertDataFormat::PCT: |
| 113 | return u"image/x-pict"_ustr; |
| 114 | case ConvertDataFormat::PNG: |
| 115 | return u"image/png"_ustr; |
| 116 | case ConvertDataFormat::SVM: |
| 117 | return u"image/x-svm"_ustr; |
| 118 | case ConvertDataFormat::TIF: |
| 119 | return u"image/tiff"_ustr; |
| 120 | case ConvertDataFormat::WMF: |
| 121 | return u"image/x-wmf"_ustr; |
| 122 | case ConvertDataFormat::EMF: |
| 123 | return u"image/x-emf"_ustr; |
| 124 | case ConvertDataFormat::SVG: |
| 125 | return u"image/svg+xml"_ustr; |
| 126 | case ConvertDataFormat::MET: // What is this? |
| 127 | case ConvertDataFormat::Unknown: |
| 128 | default: |
| 129 | return u""_ustr; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | char const* GraphicMimeTypeHelper::GetExtensionForConvertDataFormat(ConvertDataFormat nFormat) |
| 134 | { |
| 135 | char const* pExt = nullptr; |
| 136 | // create extension |
| 137 | if (nFormat != ConvertDataFormat::Unknown) |
| 138 | { |
| 139 | switch (nFormat) |
| 140 | { |
| 141 | case ConvertDataFormat::BMP: |
| 142 | pExt = ".bmp"; |
| 143 | break; |
| 144 | case ConvertDataFormat::GIF: |
| 145 | pExt = ".gif"; |
| 146 | break; |
| 147 | case ConvertDataFormat::JPG: |
| 148 | pExt = ".jpg"; |
| 149 | break; |
| 150 | case ConvertDataFormat::MET: |
| 151 | pExt = ".met"; |
| 152 | break; |
| 153 | case ConvertDataFormat::PCT: |
| 154 | pExt = ".pct"; |
| 155 | break; |
| 156 | case ConvertDataFormat::PNG: |
| 157 | pExt = ".png"; |
| 158 | break; |
| 159 | case ConvertDataFormat::SVM: |
| 160 | pExt = ".svm"; |
| 161 | break; |
| 162 | case ConvertDataFormat::TIF: |
| 163 | pExt = ".tif"; |
| 164 | break; |
| 165 | case ConvertDataFormat::WMF: |
| 166 | pExt = ".wmf"; |
| 167 | break; |
| 168 | case ConvertDataFormat::EMF: |
| 169 | pExt = ".emf"; |
| 170 | break; |
| 171 | |
| 172 | default: |
| 173 | pExt = ".grf"; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | return pExt; |
| 178 | } |
| 179 | |
| 180 | static auto GetMediaMimes() -> std::map<OString, OString> const& |
| 181 | { |
| 182 | static std::map<OString, OString> const mimes = { |
| 183 | { "mp4", "video/mp4" }, |
| 184 | { "ts", "video/MP2T" }, |
| 185 | { "mpeg", "video/mpeg" }, |
| 186 | { "mpg", "video/mpeg" }, |
| 187 | { "mkv", "video/x-matroska" }, |
| 188 | { "webm", "video/webm" }, |
| 189 | { "ogv", "video/ogg" }, |
| 190 | { "mov", "video/quicktime" }, |
| 191 | { "wmv", "video/x-ms-wmv" }, |
| 192 | { "avi", "video/x-msvideo" }, |
| 193 | { "flv", "video/x-flv" }, |
| 194 | { "m4a", "audio/mp4" }, |
| 195 | { "aac", "audio/aac" }, |
| 196 | { "mp3", "audio/mpeg" }, // https://bugs.chromium.org/p/chromium/issues/detail?id=227004 |
| 197 | { "ogg", "audio/ogg" }, |
| 198 | { "oga", "audio/ogg" }, |
| 199 | { "opus", "audio/ogg" }, |
| 200 | { "flac", "audio/flac" }, // missing at IANA? |
| 201 | // note there is RFC 2631 but i got the impression that vnd.wave |
| 202 | // requires specifying the codec in the container; also this page |
| 203 | // says "Historic" whatever that means: |
| 204 | // https://www.iana.org/assignments/wave-avi-codec-registry/wave-avi-codec-registry.xhtml |
| 205 | { "wav", "audio/x-wav" }, |
| 206 | }; |
| 207 | return mimes; |
| 208 | } |
| 209 | |
| 210 | auto IsMediaMimeType(::std::string_view const rMimeType) -> bool |
| 211 | { |
| 212 | return IsMediaMimeType(OStringToOUString(rMimeType, RTL_TEXTENCODING_UTF8)); |
| 213 | } |
| 214 | |
| 215 | auto IsMediaMimeType(OUString const& rMimeType) -> bool |
| 216 | { |
| 217 | static std::set<OUString> mimes; |
| 218 | if (mimes.empty()) |
| 219 | { |
| 220 | auto const& rMap(GetMediaMimes()); |
| 221 | for (auto const& it : rMap) |
| 222 | { |
| 223 | mimes.insert(OStringToOUString(it.second, RTL_TEXTENCODING_UTF8)); |
| 224 | } |
| 225 | } |
| 226 | return rMimeType == AVMEDIA_MIMETYPE_COMMON || mimes.find(rMimeType) != mimes.end(); |
| 227 | } |
| 228 | |
| 229 | auto GuessMediaMimeType(::std::u16string_view rFileName) -> OUString |
| 230 | { |
| 231 | if (auto const i = rFileName.rfind('.'); i != ::std::string_view::npos) |
| 232 | { |
| 233 | OString const ext( |
| 234 | OUStringToOString(rFileName.substr(i + 1), RTL_TEXTENCODING_UTF8).toAsciiLowerCase()); |
| 235 | auto const& rMap(GetMediaMimes()); |
| 236 | auto const it(rMap.find(ext)); |
| 237 | if (it != rMap.end()) |
| 238 | { |
| 239 | return OStringToOUString(it->second, RTL_TEXTENCODING_ASCII_US); |
| 240 | } |
| 241 | } |
| 242 | return OUString(); |
| 243 | } |
| 244 | |
| 245 | } // namespace comphelper |
| 246 | |
| 247 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |