[Libreoffice-commits] core.git: comphelper/source include/comphelper include/xmloff xmloff/source

Samuel Mehrbrodt Samuel.Mehrbrodt at cib.de
Thu Nov 30 16:43:22 UTC 2017


 comphelper/source/misc/graphicmimetype.cxx |   54 +++++++++++++++++++++++++++++
 include/comphelper/graphicmimetype.hxx     |    8 ++++
 include/xmloff/xmlexp.hxx                  |    2 +
 xmloff/source/core/xmlexp.cxx              |   18 +++++++++
 xmloff/source/draw/shapeexport.cxx         |   11 +++++
 xmloff/source/text/txtparae.cxx            |   22 -----------
 6 files changed, 94 insertions(+), 21 deletions(-)

New commits:
commit 32efde5cef2b8516a9decd0bf7091d7def1da971
Author: Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>
Date:   Wed Nov 29 21:12:32 2017 +0100

    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/45528
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>

diff --git a/comphelper/source/misc/graphicmimetype.cxx b/comphelper/source/misc/graphicmimetype.cxx
index f8eeb3e92de5..bf88312304b1 100644
--- a/comphelper/source/misc/graphicmimetype.cxx
+++ b/comphelper/source/misc/graphicmimetype.cxx
@@ -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: */
diff --git a/include/comphelper/graphicmimetype.hxx b/include/comphelper/graphicmimetype.hxx
index 3f2b5308b2d6..970342f2e5f5 100644
--- a/include/comphelper/graphicmimetype.hxx
+++ b/include/comphelper/graphicmimetype.hxx
@@ -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);
 };
 }
 
diff --git a/include/xmloff/xmlexp.hxx b/include/xmloff/xmlexp.hxx
index 474d718d5a41..4127e2a883fa 100644
--- a/include/xmloff/xmlexp.hxx
+++ b/include/xmloff/xmlexp.hxx
@@ -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 );
 
diff --git a/xmloff/source/core/xmlexp.cxx b/xmloff/source/core/xmlexp.cxx
index f8970a58f2c2..329fe258b766 100644
--- a/xmloff/source/core/xmlexp.cxx
+++ b/xmloff/source/core/xmlexp.cxx
@@ -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;
diff --git a/xmloff/source/draw/shapeexport.cxx b/xmloff/source/draw/shapeexport.cxx
index 3f7f1d54ec18..29d3c74a84af 100644
--- a/xmloff/source/draw/shapeexport.cxx
+++ b/xmloff/source/draw/shapeexport.cxx
@@ -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() )
diff --git a/xmloff/source/text/txtparae.cxx b/xmloff/source/text/txtparae.cxx
index 4ab345e599c3..eaaf3ff0f2aa 100644
--- a/xmloff/source/text/txtparae.cxx
+++ b/xmloff/source/text/txtparae.cxx
@@ -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 ) );


More information about the Libreoffice-commits mailing list