You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2020/02/27 09:36:07 UTC

[sling-org-apache-sling-commons-contentdetection] branch master updated: SLING-9153 - MIME Type detection leaks temporary files when given a ZIP file

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-contentdetection.git


The following commit(s) were added to refs/heads/master by this push:
     new cebf68b  SLING-9153 - MIME Type detection leaks temporary files when given a ZIP file
cebf68b is described below

commit cebf68b3e6e21ea42b966c54c03fe10933a5278f
Author: Ankita Agarwal <an...@adobe.com>
AuthorDate: Thu Feb 27 15:06:01 2020 +0530

    SLING-9153 - MIME Type detection leaks temporary files when given a ZIP file
---
 .../internal/ContentAwareMimeTypeServiceImpl.java            | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java b/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java
index 8641678..aab5d01 100644
--- a/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java
+++ b/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java
@@ -20,6 +20,7 @@ package org.apache.sling.commons.contentdetection.internal;
 import org.apache.sling.commons.contentdetection.ContentAwareMimeTypeService;
 import org.apache.sling.commons.mime.MimeTypeService;
 import org.apache.tika.detect.Detector;
+import org.apache.tika.io.TemporaryResources;
 import org.apache.tika.io.TikaInputStream;
 import org.apache.tika.metadata.Metadata;
 import org.apache.tika.mime.MediaType;
@@ -52,10 +53,13 @@ public class ContentAwareMimeTypeServiceImpl implements  ContentAwareMimeTypeSer
         if(!content.markSupported()) {
             throw new IllegalArgumentException("Supplied InputStream does not support mark/reset");
         }
-        TikaInputStream stream = TikaInputStream.get(content);
-        Metadata metadata = new Metadata();
-        metadata.set(Metadata.RESOURCE_NAME_KEY, filename);
-        MediaType mediaType = detector.detect(stream, metadata);
+        MediaType mediaType;
+        try (TemporaryResources tmp = new TemporaryResources()){
+            TikaInputStream stream = TikaInputStream.get(content, tmp);
+            Metadata metadata = new Metadata();
+            metadata.set(Metadata.RESOURCE_NAME_KEY, filename);
+            mediaType = detector.detect(stream, metadata);
+        }
         return mediaType.toString();
     }