[ 
https://issues.apache.org/jira/browse/TIKA-4854?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18109519#comment-18109519
 ] 

ASF GitHub Bot commented on TIKA-4854:
--------------------------------------

dschmidt commented on code in PR #3094:
URL: https://github.com/apache/tika/pull/3094#discussion_r3887388129


##########
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,41 @@ 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, and
+        //only when the embedded document extractor wants it at all
+        EmbeddedDocumentExtractor extractor =
+                EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context);
+        Metadata thumbnailMetadata = null;
+        byte[] thumbnail = null;
+        XHTMLContentHandler xhtml = null;
 
         while (entry != null) {
+            if (IWORK_THUMBNAIL_ENTRY.equals(entry.getName()) && 
zip.canReadEntryData(entry)) {
+                thumbnailMetadata = thumbnailMetadata(context);
+                if (extractor.shouldParseEmbedded(thumbnailMetadata, context)) 
{
+                    thumbnail = IOUtils.toByteArray(
+                            BoundedInputStream.builder().setInputStream(zip)
+                                    .setMaxCount(MAX_THUMBNAIL_BYTES).get());

Review Comment:
   Agreed. It now reads one byte past the limit, drops the thumbnail if the 
entry is larger and records a tk:exception:warning on the document instead of 
emitting a cut-off JPEG.





> Emit the preview image of iWork '09 and iWork '18 packages as a THUMBNAIL 
> embedded resource
> -------------------------------------------------------------------------------------------
>
>                 Key: TIKA-4854
>                 URL: https://issues.apache.org/jira/browse/TIKA-4854
>             Project: Tika
>          Issue Type: Improvement
>            Reporter: Dominik Schmidt
>            Priority: Major
>
> IWork13PackageParser emits preview.jpg as a THUMBNAIL embedded document. The 
> two other iWork package parsers do not emit any embedded documents: 
> IWorkPackageParser (iWork '09, QuickLook/Thumbnail.jpg) only parses the index 
> XML, and IWork18PackageParser only detects the media type although the 
> package carries preview.jpg as well. Both previews should be emitted as 
> THUMBNAIL like the '13 one, so a client gets the representative image of 
> every iWork file the same way.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to