Copilot commented on code in PR #3094:
URL: https://github.com/apache/tika/pull/3094#discussion_r3886904635
##########
tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-apple-module/src/main/java/org/apache/tika/parser/iwork/IWorkPackageParser.java:
##########
@@ -87,12 +91,34 @@ public Set<MediaType> getSupportedTypes(ParseContext
context) {
return supportedTypes;
}
+ /**
+ * The document preview of an iWork '09 package.
+ */
+ public final static String IWORK_THUMBNAIL_ENTRY =
"QuickLook/Thumbnail.jpg";
+
+ /**
+ * Bound on the preview held in memory until the content has been
+ * parsed; a real one is well under a megabyte.
+ */
+ private static final long MAX_THUMBNAIL_BYTES = 20 * 1024 * 1024;
+
public void parse(TikaInputStream tis, ContentHandler handler, Metadata
metadata,
ParseContext context) throws IOException, SAXException,
TikaException {
ZipArchiveInputStream zip = new ZipArchiveInputStream(tis);
ZipArchiveEntry entry = zip.getNextEntry();
+ //the package is read as a stream, so the preview may come before the
+ //content: hold it back and emit it once the content is written
+ byte[] thumbnail = null;
+ XHTMLContentHandler xhtml = null;
while (entry != null) {
+ if (IWORK_THUMBNAIL_ENTRY.equals(entry.getName()) &&
zip.canReadEntryData(entry)) {
+ thumbnail = IOUtils.toByteArray(
+ BoundedInputStream.builder().setInputStream(zip)
+ .setMaxCount(MAX_THUMBNAIL_BYTES).get());
+ entry = zip.getNextEntry();
+ continue;
+ }
Review Comment:
The thumbnail bytes are buffered into memory as soon as the
`QuickLook/Thumbnail.jpg` entry is encountered, even if embedded extraction is
disabled via `EmbeddedDocumentExtractor.shouldParseEmbedded(...)` (or if the
thumbnail ends up never being emitted). This can cause unnecessary I/O and up
to `MAX_THUMBNAIL_BYTES` heap usage in cases where the caller has disabled
embedded parsing.
Consider checking `shouldParseEmbedded` before reading the thumbnail entry
so you only buffer the preview when it will actually be parsed/emitted.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]