diff --git a/app/controllers/CodeApp.java b/app/controllers/CodeApp.java index d02e41bcb..2dc84f1fe 100644 --- a/app/controllers/CodeApp.java +++ b/app/controllers/CodeApp.java @@ -24,6 +24,7 @@ import controllers.annotation.IsAllowed; import models.Project; import models.enumeration.Operation; +import org.apache.commons.io.FilenameUtils; import org.apache.tika.Tika; import org.codehaus.jackson.node.ObjectNode; import org.eclipse.jgit.api.errors.GitAPIException; @@ -35,6 +36,7 @@ import playRepository.PlayRepository; import playRepository.RepositoryService; import utils.ErrorViews; +import utils.FileUtil; import views.html.code.nohead; import views.html.code.nohead_svn; import views.html.code.view; @@ -254,6 +256,6 @@ public static Result openFile(String userName, String projectName, String revisi return notFound(ErrorViews.NotFound.render("error.notfound")); } - return ok(raw).as(tika.detect(raw)); + return ok(raw).as(FileUtil.detectMediaType(raw, FilenameUtils.getName(path))); } } diff --git a/app/models/Attachment.java b/app/models/Attachment.java index 27aa0aa74..f4583056f 100644 --- a/app/models/Attachment.java +++ b/app/models/Attachment.java @@ -278,18 +278,7 @@ public boolean store(File file, String name, Resource container) throws IOExcept } if (this.mimeType == null) { - Metadata meta = new Metadata(); - meta.add(Metadata.RESOURCE_NAME_KEY, this.name); - MediaType mediaType = new Tika().getDetector().detect( - new BufferedInputStream(new FileInputStream(file)), meta); - this.mimeType = mediaType.toString(); - if (mediaType.getType().toLowerCase().equals("text")) { - this.mimeType += "; charset=" + FileUtil.detectCharset(new FileInputStream(file)); - } else if (mediaType.equals(MediaType.audio("ogg")) - && FilenameUtils.getExtension(name).toLowerCase().equals("ogv")) { - // This fixes Tika's misjudge of media type for ogg videos. - this.mimeType = "video/ogg"; - } + this.mimeType = FileUtil.detectMediaType(file, name); } // the size must be set before it is moved. diff --git a/app/utils/FileUtil.java b/app/utils/FileUtil.java index 1c4390a24..c51645de9 100644 --- a/app/utils/FileUtil.java +++ b/app/utils/FileUtil.java @@ -20,12 +20,14 @@ */ package utils; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang.StringUtils; +import org.apache.tika.Tika; +import org.apache.tika.metadata.Metadata; +import org.apache.tika.mime.MediaType; import org.mozilla.universalchardet.UniversalDetector; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; public class FileUtil { @@ -91,4 +93,42 @@ public static String detectCharset(InputStream is) throws IOException { return or(detector.getDetectedCharset(), "UTF-8"); } + + public static String detectMediaType(File file, String name) throws IOException { + return detectMediaType(new FileInputStream(file), name); + } + + public static String detectMediaType(byte[] bytes, String name) throws IOException { + return detectMediaType(new ByteArrayInputStream(bytes), name); + } + + /** + * Detects media type of the given resource, by using Apache Tika. + * + * This method does following additional tasks besides Tika: + * 1. Adds a charset parameter for text resources. + * 2. Fixes Tika's misjudge of media type for ogg videos + * + * @param is the input stream to read the resource + * @param name the filename of the resource + * @return the detected media type which optionally includes a charset parameter + * e.g. "text/plain; charset=utf-8" + * @throws IOException + */ + public static String detectMediaType(InputStream is, String name) throws IOException { + Metadata meta = new Metadata(); + meta.add(Metadata.RESOURCE_NAME_KEY, name); + MediaType mediaType = new Tika().getDetector().detect( + new BufferedInputStream(is), meta); + String mimeType = mediaType.toString(); + if (mediaType.getType().toLowerCase().equals("text")) { + mimeType += "; charset=" + FileUtil.detectCharset(is); + } else if (mediaType.equals(MediaType.audio("ogg")) + && FilenameUtils.getExtension(name).toLowerCase().equals("ogv")) { + // This fixes Tika's misjudge of media type for ogg videos. + mimeType = "video/ogg"; + } + + return mimeType; + } }