Add FileUtil#getContentType().

This commit is contained in:
takezoe
2013-10-04 04:17:30 +09:00
parent f4f2bf34fc
commit 380cdbcf75
3 changed files with 14 additions and 16 deletions

View File

@@ -94,17 +94,10 @@ trait RepositoryViewerControllerBase extends ControllerBase {
if(raw){
// Download
val mimeType = FileUtil.getMimeType(path)
val bytes = JGitUtil.getContent(git, objectId, false).get
contentType = if(mimeType == "application/octet-stream" && FileUtil.isText(bytes)){
"text/plain"
} else {
mimeType
}
//response.setHeader("Content-Disposition", s"inline; filename=${FileUtil.getFileName(path)}")
defining(JGitUtil.getContent(git, objectId, false).get){ bytes =>
contentType = FileUtil.getContentType(path, bytes)
bytes
}
} else {
// Viewer
val large = FileUtil.isLarge(git.getRepository.getObjectDatabase.open(objectId).getSize)

View File

@@ -164,12 +164,7 @@ trait WikiControllerBase extends ControllerBase with FlashMapSupport {
val path = multiParams("splat").head
getFileContent(repository.owner, repository.name, path).map { bytes =>
val mimeType = FileUtil.getMimeType(path)
contentType = if(mimeType == "application/octet-stream" && FileUtil.isText(bytes)){
"text/plain"
} else {
mimeType
}
contentType = FileUtil.getContentType(path, bytes)
bytes
} getOrElse NotFound
})

View File

@@ -16,6 +16,16 @@ object FileUtil {
}
}
def getContentType(name: String, bytes: Array[Byte]): String = {
defining(getMimeType(name)){ mimeType =>
if(mimeType == "application/octet-stream" && isText(bytes)){
"text/plain"
} else {
mimeType
}
}
}
def isImage(name: String): Boolean = getMimeType(name).startsWith("image/")
def isLarge(size: Long): Boolean = (size > 1024 * 1000)