|
23 | 23 | import java.io.InputStream; |
24 | 24 | import java.nio.file.Files; |
25 | 25 | import java.nio.file.Path; |
| 26 | +import java.nio.file.StandardCopyOption; |
26 | 27 |
|
27 | | -import org.apache.commons.io.input.CloseShieldInputStream; |
28 | 28 | import org.slf4j.Logger; |
29 | 29 | import org.slf4j.LoggerFactory; |
30 | 30 | import org.xml.sax.ContentHandler; |
@@ -56,6 +56,7 @@ public class RUnpackExtractor extends ParsingEmbeddedDocumentExtractor { |
56 | 56 |
|
57 | 57 | private EmbeddedBytesSelector embeddedBytesSelector = EmbeddedBytesSelector.ACCEPT_ALL; |
58 | 58 |
|
| 59 | + private final EmbeddedStreamTranslator embeddedStreamTranslator = new DefaultEmbeddedStreamTranslator(); |
59 | 60 | private long bytesExtracted = 0; |
60 | 61 | private final long maxEmbeddedBytesForExtraction; |
61 | 62 |
|
@@ -113,13 +114,20 @@ private void parseWithBytes(TikaInputStream stream, ContentHandler handler, Meta |
113 | 114 | throws TikaException, IOException, SAXException { |
114 | 115 | //TODO -- improve the efficiency of this so that we're not |
115 | 116 | //literally writing out a file per request |
116 | | - Path p = stream.getPath(); |
117 | | - try { |
118 | | - //warp in CloseShieldInputStream to ensure that a misbehaving parser isn't closing |
119 | | - //the stream and thereby deleting the temp file. |
120 | | - parse(CloseShieldInputStream.wrap(stream), handler, metadata); |
| 117 | + Path tmp = Files.createTempFile("tika-tmp-", ".bin"); |
| 118 | + if (embeddedStreamTranslator.shouldTranslate(stream, metadata)) { |
| 119 | + Files.copy(embeddedStreamTranslator.translate(stream, metadata), tmp, StandardCopyOption.REPLACE_EXISTING); |
| 120 | + } else { |
| 121 | + Files.copy(stream, tmp, StandardCopyOption.REPLACE_EXISTING); |
| 122 | + } |
| 123 | + try (TikaInputStream tmpTis = TikaInputStream.get(tmp)) { |
| 124 | + parse(tmpTis, handler, metadata); |
121 | 125 | } finally { |
122 | | - storeEmbeddedBytes(p, metadata); |
| 126 | + try { |
| 127 | + storeEmbeddedBytes(tmp, metadata); |
| 128 | + } finally { |
| 129 | + Files.delete(tmp); |
| 130 | + } |
123 | 131 | } |
124 | 132 | } |
125 | 133 |
|
|
0 commit comments