Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Merge branch 'feature/git-detect-media-type-better' of npcode/yobi
Browse files Browse the repository at this point in the history
from pull request 977
  • Loading branch information
채수원 committed May 23, 2014
2 parents f27341f + e0ea98b commit 3f11d29
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
4 changes: 3 additions & 1 deletion app/controllers/CodeApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)));
}
}
13 changes: 1 addition & 12 deletions app/models/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 43 additions & 3 deletions app/utils/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 3f11d29

Please sign in to comment.