Kaydet (Commit) 32efde5c authored tarafından Samuel Mehrbrodt's avatar Samuel Mehrbrodt

tdf#113696 Add mimetype to image element

Otherwise browsers don't recognize base64 encoded svg files.

Change-Id: I54d0b87c52a1ca9da1d820751ae32159b88ed28f
Reviewed-on: https://gerrit.libreoffice.org/45528Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarSamuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
üst b17c29d4
......@@ -19,6 +19,22 @@
#include <comphelper/graphicmimetype.hxx>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/graphic/GraphicProvider.hpp>
#include <com/sun/star/graphic/XGraphicProvider.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/graphic/XGraphic.hpp>
#include <com/sun/star/uno/Reference.hxx>
#include <comphelper/processfactory.hxx>
using namespace css;
using namespace css::beans;
using namespace css::graphic;
using namespace css::io;
using namespace css::uno;
namespace comphelper
{
OUString GraphicMimeTypeHelper::GetMimeTypeForExtension(const OString& rExt)
......@@ -47,5 +63,43 @@ OUString GraphicMimeTypeHelper::GetMimeTypeForExtension(const OString& rExt)
return aMimeType;
}
OUString GraphicMimeTypeHelper::GetMimeTypeForXGraphic(Reference<XGraphic> xGraphic)
{
OUString aSourceMimeType;
Reference<XPropertySet> const xGraphicPropertySet(xGraphic, UNO_QUERY);
if (xGraphicPropertySet.is() && // it's null if it's an external link
(xGraphicPropertySet->getPropertyValue("MimeType") >>= aSourceMimeType))
{
return aSourceMimeType;
}
return OUString("");
}
OUString GraphicMimeTypeHelper::GetMimeTypeForImageUrl(const OUString& rImageUrl)
{
// Create the graphic to retrieve the mimetype from it
Reference<XGraphicProvider> xProvider
= css::graphic::GraphicProvider::create(comphelper::getProcessComponentContext());
Sequence<PropertyValue> aMediaProperties(1);
aMediaProperties[0].Name = "URL";
aMediaProperties[0].Value <<= rImageUrl;
Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
return GetMimeTypeForXGraphic(xGraphic);
}
OUString GraphicMimeTypeHelper::GetMimeTypeForImageStream(Reference<XInputStream> xInputStream)
{
// Create the graphic to retrieve the mimetype from it
Reference<XGraphicProvider> xProvider
= css::graphic::GraphicProvider::create(comphelper::getProcessComponentContext());
Sequence<PropertyValue> aMediaProperties(1);
aMediaProperties[0].Name = "InputStream";
aMediaProperties[0].Value <<= xInputStream;
Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
return GetMimeTypeForXGraphic(xGraphic);
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -13,12 +13,20 @@
#include <comphelper/comphelperdllapi.h>
#include <rtl/ustring.hxx>
#include <com/sun/star/graphic/XGraphic.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/uno/Reference.hxx>
namespace comphelper
{
class COMPHELPER_DLLPUBLIC GraphicMimeTypeHelper
{
public:
static OUString GetMimeTypeForExtension(const OString& rExt);
static OUString GetMimeTypeForXGraphic(css::uno::Reference<css::graphic::XGraphic> xGraphic);
static OUString GetMimeTypeForImageUrl(const OUString& rImageUrl);
static OUString
GetMimeTypeForImageStream(css::uno::Reference<css::io::XInputStream> xInputStream);
};
}
......
......@@ -460,6 +460,8 @@ public:
OUString AddEmbeddedGraphicObject(
const OUString& rGraphicObjectURL );
css::uno::Reference<css::io::XInputStream> GetEmbeddedGraphicObjectStream(
const OUString& rGraphicObjectURL);
bool AddEmbeddedGraphicObjectAsBase64(
const OUString& rGraphicObjectURL );
......
......@@ -1885,6 +1885,24 @@ OUString SvXMLExport::AddEmbeddedGraphicObject( const OUString& rGraphicObjectUR
return sRet;
}
Reference< XInputStream > SvXMLExport::GetEmbeddedGraphicObjectStream( const OUString& rGraphicObjectURL )
{
if( (getExportFlags() & SvXMLExportFlags::EMBEDDED) &&
rGraphicObjectURL.startsWith( msGraphicObjectProtocol ) &&
mxGraphicResolver.is() )
{
Reference< XBinaryStreamResolver > xStmResolver( mxGraphicResolver, UNO_QUERY );
if( xStmResolver.is() )
{
Reference< XInputStream > xIn( xStmResolver->getInputStream( rGraphicObjectURL ) );
return xIn;
}
}
return nullptr;
}
bool SvXMLExport::AddEmbeddedGraphicObjectAsBase64( const OUString& rGraphicObjectURL )
{
bool bRet = false;
......
......@@ -82,6 +82,7 @@
#include <com/sun/star/document/XStorageBasedDocument.hpp>
#include <comphelper/classids.hxx>
#include <comphelper/graphicmimetype.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/storagehelper.hxx>
......@@ -2352,6 +2353,16 @@ void XMLShapeExport::ImpExportGraphicObjectShape(
}
{
// We can't guess the mimetype from sImageURL because the image type might be changed
// while creating the stream (by SvXMLGraphicInputStream). So we first need to create
// the stream, get the mime type and then write the stream.
uno::Reference<io::XInputStream> xInputStream(
mrExport.GetEmbeddedGraphicObjectStream(sImageURL));
OUString aMimeType(
comphelper::GraphicMimeTypeHelper::GetMimeTypeForImageStream(xInputStream));
if (!aMimeType.isEmpty())
GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, "mime-type", aMimeType);
SvXMLElementExport aOBJ(mrExport, XML_NAMESPACE_DRAW, XML_IMAGE, true, true);
if( !sImageURL.isEmpty() )
......
......@@ -117,7 +117,6 @@
#include <vector>
#include <algorithm>
#include <iterator>
#include <comphelper/processfactory.hxx>
#include <comphelper/graphicmimetype.hxx>
using namespace ::std;
......@@ -3016,25 +3015,6 @@ void XMLTextParagraphExport::exportContour(
true, true );
}
static OUString getMimeType(const OUString& sImageUrl)
{
// Create the graphic to retrieve the mimetype from it
Reference< XGraphicProvider > xProvider = css::graphic::GraphicProvider::create(comphelper::getProcessComponentContext());
Sequence< PropertyValue > aMediaProperties( 1 );
aMediaProperties[0].Name = "URL";
aMediaProperties[0].Value <<= sImageUrl;
Reference< XGraphic > xGraphic( xProvider->queryGraphic( aMediaProperties ) );
OUString aSourceMimeType;
Reference<XPropertySet> const xGraphicPropertySet(xGraphic, UNO_QUERY);
if (xGraphicPropertySet.is() && // it's null if it's an external link
(xGraphicPropertySet->getPropertyValue("MimeType") >>= aSourceMimeType))
{
return aSourceMimeType;
}
return OUString("");
}
void XMLTextParagraphExport::_exportTextGraphic(
const Reference < XPropertySet > & rPropSet,
const Reference < XPropertySetInfo > & rPropSetInfo )
......@@ -3101,7 +3081,7 @@ void XMLTextParagraphExport::_exportTextGraphic(
OUString aSourceMimeType = GetExport().GetImageFilterName();
// otherwise determine mimetype from graphic
if ( aSourceMimeType.isEmpty() )
aSourceMimeType = getMimeType(sOrigURL);
aSourceMimeType = comphelper::GraphicMimeTypeHelper::GetMimeTypeForImageUrl(sOrigURL);
else // !aSourceMimeType.isEmpty()
{
const OString aExt( OUStringToOString( aSourceMimeType, RTL_TEXTENCODING_ASCII_US ) );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment