|
52 | 52 | import org.apache.tika.extractor.EmbeddedDocumentExtractor; |
53 | 53 | import org.apache.tika.extractor.EmbeddedDocumentUtil; |
54 | 54 | import org.apache.tika.io.FilenameUtils; |
| 55 | +import org.apache.tika.io.TemporaryResources; |
55 | 56 | import org.apache.tika.io.TikaInputStream; |
56 | 57 | import org.apache.tika.metadata.HttpHeaders; |
57 | 58 | import org.apache.tika.metadata.Metadata; |
@@ -204,30 +205,29 @@ private void handleThumbnail(ContentHandler handler, Metadata metadata) throws S |
204 | 205 | if (tPart == null) { |
205 | 206 | continue; |
206 | 207 | } |
207 | | - try (InputStream tStream = tPart.getInputStream()) { |
208 | | - Metadata thumbnailMetadata = Metadata.newInstance(context); |
209 | | - String thumbName = tPart.getPartName().getName(); |
210 | | - thumbnailMetadata.set(TikaCoreProperties.INTERNAL_PATH, thumbName); |
211 | | - thumbnailMetadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, |
212 | | - FilenameUtils.getName(thumbName)); |
213 | | - |
214 | | - AttributesImpl attributes = new AttributesImpl(); |
215 | | - attributes.addAttribute(XHTML, "class", "class", "CDATA", "embedded"); |
216 | | - attributes.addAttribute(XHTML, "id", "id", "CDATA", thumbName); |
217 | | - handler.startElement(XHTML, "div", "div", attributes); |
218 | | - handler.endElement(XHTML, "div", "div"); |
219 | | - |
220 | | - thumbnailMetadata.set(TikaCoreProperties.EMBEDDED_RELATIONSHIP_ID, thumbName); |
221 | | - thumbnailMetadata.set(HttpHeaders.CONTENT_TYPE, tPart.getContentType()); |
222 | | - thumbnailMetadata.set(TikaCoreProperties.TITLE, tPart.getPartName().getName()); |
223 | | - thumbnailMetadata.set(TikaCoreProperties.EMBEDDED_RESOURCE_TYPE, |
224 | | - TikaCoreProperties.EmbeddedResourceType.THUMBNAIL.name()); |
225 | | - |
226 | | - if (embeddedExtractor.shouldParseEmbedded(thumbnailMetadata, context)) { |
227 | | - try (TikaInputStream tis = TikaInputStream.get(tStream)) { |
228 | | - embeddedExtractor.parseEmbedded(tis, |
229 | | - new EmbeddedContentHandler(handler), thumbnailMetadata, context, false); |
230 | | - } |
| 208 | + Metadata thumbnailMetadata = Metadata.newInstance(context); |
| 209 | + String thumbName = tPart.getPartName().getName(); |
| 210 | + thumbnailMetadata.set(TikaCoreProperties.INTERNAL_PATH, thumbName); |
| 211 | + thumbnailMetadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, |
| 212 | + FilenameUtils.getName(thumbName)); |
| 213 | + |
| 214 | + AttributesImpl attributes = new AttributesImpl(); |
| 215 | + attributes.addAttribute(XHTML, "class", "class", "CDATA", "embedded"); |
| 216 | + attributes.addAttribute(XHTML, "id", "id", "CDATA", thumbName); |
| 217 | + handler.startElement(XHTML, "div", "div", attributes); |
| 218 | + handler.endElement(XHTML, "div", "div"); |
| 219 | + |
| 220 | + thumbnailMetadata.set(TikaCoreProperties.EMBEDDED_RELATIONSHIP_ID, thumbName); |
| 221 | + thumbnailMetadata.set(HttpHeaders.CONTENT_TYPE, tPart.getContentType()); |
| 222 | + thumbnailMetadata.set(TikaCoreProperties.TITLE, tPart.getPartName().getName()); |
| 223 | + thumbnailMetadata.set(TikaCoreProperties.EMBEDDED_RESOURCE_TYPE, |
| 224 | + TikaCoreProperties.EmbeddedResourceType.THUMBNAIL.name()); |
| 225 | + |
| 226 | + if (embeddedExtractor.shouldParseEmbedded(thumbnailMetadata, context)) { |
| 227 | + try (TikaInputStream tis = TikaInputStream.get(tPart::getInputStream, |
| 228 | + new TemporaryResources(), null)) { |
| 229 | + embeddedExtractor.parseEmbedded(tis, |
| 230 | + new EmbeddedContentHandler(handler), thumbnailMetadata, context, false); |
231 | 231 | } |
232 | 232 | } |
233 | 233 | } |
@@ -400,7 +400,8 @@ private void handleEmbeddedOLE(PackagePart part, XHTMLContentHandler xhtml, Stri |
400 | 400 | //OLE 2.0 |
401 | 401 | updateMetadata(metadata, embeddedPartMetadata); |
402 | 402 |
|
403 | | - tis = TikaInputStream.get(fs.createDocumentInputStream(packageEntryName)); |
| 403 | + tis = TikaInputStream.get(() -> fs.createDocumentInputStream(packageEntryName), |
| 404 | + new TemporaryResources(), null); |
404 | 405 | if (embeddedExtractor.shouldParseEmbedded(metadata, context)) { |
405 | 406 | embeddedExtractor |
406 | 407 | .parseEmbedded(tis, xhtml, metadata, context, true); |
@@ -437,10 +438,10 @@ private void handleEmbeddedOLE(PackagePart part, XHTMLContentHandler xhtml, Stri |
437 | 438 | } catch (IOException e) { |
438 | 439 | EmbeddedDocumentUtil.recordEmbeddedStreamException(e, parentMetadata, context); |
439 | 440 | } finally { |
440 | | - fs.close(); |
441 | 441 | if (tis != null) { |
442 | 442 | tis.close(); |
443 | 443 | } |
| 444 | + fs.close(); |
444 | 445 | } |
445 | 446 | } |
446 | 447 |
|
@@ -506,7 +507,10 @@ protected void handleEmbeddedFile(PackagePart part, XHTMLContentHandler xhtml, |
506 | 507 |
|
507 | 508 | // Call the recursing handler |
508 | 509 | if (embeddedExtractor.shouldParseEmbedded(metadata, context)) { |
509 | | - try (TikaInputStream tis = TikaInputStream.get(part.getInputStream())) { |
| 510 | + //the part is in the package already: re-open it on rewind instead of |
| 511 | + //caching a copy that a digest of a large part would spill to disk |
| 512 | + try (TikaInputStream tis = TikaInputStream.get(part::getInputStream, |
| 513 | + new TemporaryResources(), null)) { |
510 | 514 | embeddedExtractor |
511 | 515 | .parseEmbedded(tis, xhtml, metadata, context, true); |
512 | 516 | } |
|
0 commit comments