Avoid any uses or lookups of ffmpeg by Tika I75d8b85d7 reintroduced a dependency on Tika to fix an issue. Switching back to Tika introduces another issue, though. On startup, Tika reaches out to the local system to find out whether ffmpeg is installed, which it will then automatically use as parser. Depending on the environment the uploadvalidator plugin is run, this lookup of ffmpeg on the local system might not be permitted. There are two options to avoid this lookup of ffmpeg: 1) Use a custom Tika config which excludes ExternalParser (which is responsible for the ffmpeg lookup). 2) Directly use the default detector of Tika as we don't need the parser functionality anyway. In this change, we went with 2). For 1), we would have needed to identify which exact configuration we're currently using. With 2), we're also safe to not introduce a regression if we change the config without further considerations in the future. A third option would be to remove the Tika dependency but compared to that, this change seems like a better option. Change-Id: I76ec0c488f93e4a9c71f0527895acbc798c5a9ba
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeUtil.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeUtil.java index 67ae289..6e6ad98 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeUtil.java +++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeUtil.java
@@ -29,8 +29,8 @@ import java.io.InputStream; import java.util.concurrent.ExecutionException; import java.util.regex.Pattern; -import org.apache.tika.Tika; -import org.apache.tika.config.TikaConfig; +import org.apache.tika.detect.DefaultDetector; +import org.apache.tika.detect.Detector; import org.apache.tika.io.TikaInputStream; import org.apache.tika.metadata.Metadata; import org.eclipse.jgit.lib.ObjectLoader; @@ -68,7 +68,7 @@ } private final LoadingCache<String, Pattern> patternCache; - private final Tika tika = new Tika(TikaConfig.getDefaultConfig()); + private final Detector detector = new DefaultDetector(); @Inject ContentTypeUtil(@Named(CACHE_NAME) LoadingCache<String, Pattern> patternCache) { @@ -85,7 +85,7 @@ public String getContentType(InputStream is, String pathname) throws IOException { Metadata metadata = new Metadata(); metadata.set(Metadata.RESOURCE_NAME_KEY, pathname); - return tika.detect(TikaInputStream.get(is), metadata); + return detector.detect(TikaInputStream.get(is), metadata).toString(); } @VisibleForTesting
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MimeTypeDetection.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MimeTypeDetection.java index ac38404..eb3820f 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MimeTypeDetection.java +++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MimeTypeDetection.java
@@ -16,19 +16,19 @@ import java.io.ByteArrayInputStream; import java.io.IOException; -import org.apache.tika.Tika; -import org.apache.tika.config.TikaConfig; +import org.apache.tika.detect.DefaultDetector; +import org.apache.tika.detect.Detector; import org.apache.tika.io.TikaInputStream; import org.apache.tika.metadata.Metadata; class MimeTypeDetection { public String getMimeType(String path, byte[] content) throws IOException { - Tika tika = new Tika(TikaConfig.getDefaultConfig()); + Detector detector = new DefaultDetector(); Metadata metadata = new Metadata(); metadata.set(Metadata.RESOURCE_NAME_KEY, path); ByteArrayInputStream bis = new ByteArrayInputStream(content); - return tika.detect(TikaInputStream.get(bis), metadata); + return detector.detect(TikaInputStream.get(bis), metadata).toString(); } }