LibreOffice Module comphelper (master) 1
graphicmimetype.cxx
Go to the documentation of this file.
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
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
35
36using namespace css;
37using namespace css::beans;
38using namespace css::graphic;
39using namespace css::io;
40using namespace css::uno;
41
42namespace comphelper
43{
45{
46 struct XMLGraphicMimeTypeMapper
47 {
48 const char* pExt;
49 const char* pMimeType;
50 };
51
52 static const XMLGraphicMimeTypeMapper aMapper[]
53 = { { "gif", "image/gif" }, { "png", "image/png" }, { "jpg", "image/jpeg" },
54 { "tif", "image/tiff" }, { "svg", "image/svg+xml" }, { "pdf", "application/pdf" },
55 { "wmf", "image/x-wmf" }, { "emf", "image/x-emf" }, { "eps", "image/x-eps" },
56 { "bmp", "image/bmp" }, { "pct", "image/x-pict" }, { "svm", "image/x-svm" } };
57
58 OUString aMimeType;
59
60 size_t const nCount = std::size(aMapper);
61 for (size_t i = 0; (i < nCount) && aMimeType.isEmpty(); ++i)
62 {
63 if (rExt == aMapper[i].pExt)
64 aMimeType = OUString(aMapper[i].pMimeType, strlen(aMapper[i].pMimeType),
65 RTL_TEXTENCODING_ASCII_US);
66 }
67
68 return aMimeType;
69}
70
71OUString GraphicMimeTypeHelper::GetMimeTypeForXGraphic(const Reference<XGraphic>& xGraphic)
72{
73 OUString aSourceMimeType;
74 Reference<XPropertySet> const xGraphicPropertySet(xGraphic, UNO_QUERY);
75 if (xGraphicPropertySet.is() && // it's null if it's an external link
76 (xGraphicPropertySet->getPropertyValue("MimeType") >>= aSourceMimeType))
77 {
78 return aSourceMimeType;
79 }
80 return "";
81}
82
83OUString
84GraphicMimeTypeHelper::GetMimeTypeForImageStream(const Reference<XInputStream>& xInputStream)
85{
86 // Create the graphic to retrieve the mimetype from it
87 Reference<XGraphicProvider> xProvider
88 = css::graphic::GraphicProvider::create(comphelper::getProcessComponentContext());
89 Sequence<PropertyValue> aMediaProperties{ comphelper::makePropertyValue("InputStream",
90 xInputStream) };
91 Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
92
93 return GetMimeTypeForXGraphic(xGraphic);
94}
95
97{
98 switch (convertDataFormat)
99 {
100 case ConvertDataFormat::BMP:
101 return "image/bmp";
102 case ConvertDataFormat::GIF:
103 return "image/gif";
104 case ConvertDataFormat::JPG:
105 return "image/jpeg";
106 case ConvertDataFormat::PCT:
107 return "image/x-pict";
108 case ConvertDataFormat::PNG:
109 return "image/png";
110 case ConvertDataFormat::SVM:
111 return "image/x-svm";
112 case ConvertDataFormat::TIF:
113 return "image/tiff";
114 case ConvertDataFormat::WMF:
115 return "image/x-wmf";
116 case ConvertDataFormat::EMF:
117 return "image/x-emf";
118 case ConvertDataFormat::SVG:
119 return "image/svg+xml";
120 case ConvertDataFormat::MET: // What is this?
121 case ConvertDataFormat::Unknown:
122 default:
123 return "";
124 }
125}
126
128{
129 char const* pExt = nullptr;
130 // create extension
131 if (nFormat != ConvertDataFormat::Unknown)
132 {
133 switch (nFormat)
134 {
135 case ConvertDataFormat::BMP:
136 pExt = ".bmp";
137 break;
138 case ConvertDataFormat::GIF:
139 pExt = ".gif";
140 break;
141 case ConvertDataFormat::JPG:
142 pExt = ".jpg";
143 break;
144 case ConvertDataFormat::MET:
145 pExt = ".met";
146 break;
147 case ConvertDataFormat::PCT:
148 pExt = ".pct";
149 break;
150 case ConvertDataFormat::PNG:
151 pExt = ".png";
152 break;
153 case ConvertDataFormat::SVM:
154 pExt = ".svm";
155 break;
156 case ConvertDataFormat::TIF:
157 pExt = ".tif";
158 break;
159 case ConvertDataFormat::WMF:
160 pExt = ".wmf";
161 break;
162 case ConvertDataFormat::EMF:
163 pExt = ".emf";
164 break;
165
166 default:
167 pExt = ".grf";
168 break;
169 }
170 }
171 return pExt;
172}
173
174static auto GetMediaMimes() -> std::map<OString, OString> const&
175{
176 static std::map<OString, OString> const mimes = {
177 { "mp4", "video/mp4" },
178 { "ts", "video/MP2T" },
179 { "mpeg", "video/mpeg" },
180 { "mpg", "video/mpeg" },
181 { "mkv", "video/x-matroska" },
182 { "webm", "video/webm" },
183 { "ogv", "video/ogg" },
184 { "mov", "video/quicktime" },
185 { "wmv", "video/x-ms-wmv" },
186 { "avi", "video/x-msvideo" },
187 { "m4a", "audio/mp4" },
188 { "aac", "audio/aac" },
189 { "mp3", "audio/mpeg" }, // https://bugs.chromium.org/p/chromium/issues/detail?id=227004
190 { "ogg", "audio/ogg" },
191 { "oga", "audio/ogg" },
192 { "opus", "audio/ogg" },
193 { "flac", "audio/flac" }, // missing at IANA?
194 // note there is RFC 2631 but i got the impression that vnd.wave
195 // requires specifying the codec in the container; also this page
196 // says "Historic" whatever that means:
197 // https://www.iana.org/assignments/wave-avi-codec-registry/wave-avi-codec-registry.xhtml
198 { "wav", "audio/x-wav" },
199 };
200 return mimes;
201}
202
203auto IsMediaMimeType(::std::string_view const rMimeType) -> bool
204{
205 return IsMediaMimeType(OStringToOUString(rMimeType, RTL_TEXTENCODING_UTF8));
206}
207
208auto IsMediaMimeType(OUString const& rMimeType) -> bool
209{
210 static std::set<OUString> mimes;
211 if (mimes.empty())
212 {
213 auto const& rMap(GetMediaMimes());
214 for (auto const& it : rMap)
215 {
216 mimes.insert(OStringToOUString(it.second, RTL_TEXTENCODING_UTF8));
217 }
218 }
219 return rMimeType == AVMEDIA_MIMETYPE_COMMON || mimes.find(rMimeType) != mimes.end();
220}
221
222auto GuessMediaMimeType(::std::u16string_view rFileName) -> OUString
223{
224 if (auto const i = rFileName.rfind('.'); i != ::std::string_view::npos)
225 {
226 OString const ext(OUStringToOString(rFileName.substr(i + 1), RTL_TEXTENCODING_UTF8));
227 auto const& rMap(GetMediaMimes());
228 auto const it(rMap.find(ext));
229 if (it != rMap.end())
230 {
231 return OStringToOUString(it->second, RTL_TEXTENCODING_ASCII_US);
232 }
233 }
234 return OUString();
235}
236
237} // namespace comphelper
238
239/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static OUString GetMimeTypeForXGraphic(const css::uno::Reference< css::graphic::XGraphic > &xGraphic)
static OUString GetMimeTypeForImageStream(const css::uno::Reference< css::io::XInputStream > &xInputStream)
static char const * GetExtensionForConvertDataFormat(ConvertDataFormat nFormat)
static OUString GetMimeTypeForConvertDataFormat(ConvertDataFormat convertDataFormat)
static OUString GetMimeTypeForExtension(std::string_view rExt)
int nCount
constexpr OUStringLiteral AVMEDIA_MIMETYPE_COMMON
static auto GetMediaMimes() -> std::map< OString, OString > const &
auto GuessMediaMimeType(::std::u16string_view rFileName) -> OUString
auto IsMediaMimeType(::std::string_view const rMimeType) -> bool
Reference< XComponentContext > getProcessComponentContext()
This function gets the process service factory's default component context.
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
Creates a beans::PropertyValue easily, i.e.
int i
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
ConvertDataFormat
const sal_Unicode *const aMimeType[]
const char * pExt