]> source.dussan.org Git - poi.git/commitdiff
fix content-type handling in *SSF-EmbeddedExtractor
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 1 Jan 2017 00:56:51 +0000 (00:56 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 1 Jan 2017 00:56:51 +0000 (00:56 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1776823 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java

index a06566b54c61c09c9c10e49c6cd81c5fee51df53..8f63d5098f6a4d9e6d548b5c4358d01042b44f62 100644 (file)
@@ -45,6 +45,7 @@ import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.LocaleUtil;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
+import org.apache.poi.xssf.usermodel.XSSFObjectData;
 
 public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
     private static final POILogger LOG = POILogFactory.getLogger(EmbeddedExtractor.class);
@@ -97,7 +98,11 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
                     if (od.hasDirectoryEntry()) {
                         data = extractOne((DirectoryNode)od.getDirectory());
                     } else {
-                        data = new EmbeddedData(od.getFileName(), od.getObjectData(), "binary/octet-stream");
+                        String contentType = "binary/octet-stream";
+                        if (od instanceof XSSFObjectData) {
+                            contentType = ((XSSFObjectData)od).getObjectPart().getContentType();
+                        }
+                        data = new EmbeddedData(od.getFileName(), od.getObjectData(), contentType);
                     }
                 } catch (Exception e) {
                     LOG.log(POILogger.WARN, "Entry not found / readable - ignoring OLE embedding", e);
@@ -169,6 +174,7 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
         @Override
         public EmbeddedData extract(DirectoryNode dn) throws IOException {
             try {
+                // TODO: inspect the CompObj record for more details, i.e. the content type
                 Ole10Native ole10 = Ole10Native.createFromEmbeddedOleObject(dn);
                 return new EmbeddedData(ole10.getFileName(), ole10.getDataBuffer(), "binary/octet-stream");
             } catch (Ole10NativeException e) {
@@ -254,6 +260,7 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
         public EmbeddedData extract(DirectoryNode dn) throws IOException {
             EmbeddedData ed = super.extract(dn);
             ed.setFilename(dn.getName()+".doc");
+            ed.setContentType("application/msword");
             return ed;
         }
     }
@@ -271,6 +278,7 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
         public EmbeddedData extract(DirectoryNode dn) throws IOException {
             EmbeddedData ed = super.extract(dn);
             ed.setFilename(dn.getName()+".xls");
+            ed.setContentType("application/vnd.ms-excel");
             return ed;
         }
     }