mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-11 16:05:49 +01:00
CompressUtil and FileTypeUtil are merged to FileUtil.
This commit is contained in:
@@ -2,7 +2,7 @@ package app
|
|||||||
|
|
||||||
import util.Directory._
|
import util.Directory._
|
||||||
import util.Implicits._
|
import util.Implicits._
|
||||||
import _root_.util.{ReadableRepositoryAuthenticator, JGitUtil, FileTypeUtil, CompressUtil}
|
import _root_.util.{ReadableRepositoryAuthenticator, JGitUtil, FileUtil}
|
||||||
import service._
|
import service._
|
||||||
import org.scalatra._
|
import org.scalatra._
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -143,12 +143,12 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
JGitUtil.getContent(git, objectId, false).get
|
JGitUtil.getContent(git, objectId, false).get
|
||||||
} else {
|
} else {
|
||||||
// Viewer
|
// Viewer
|
||||||
val large = FileTypeUtil.isLarge(git.getRepository.getObjectDatabase.open(objectId).getSize)
|
val large = FileUtil.isLarge(git.getRepository.getObjectDatabase.open(objectId).getSize)
|
||||||
val viewer = if(FileTypeUtil.isImage(path)) "image" else if(large) "large" else "other"
|
val viewer = if(FileUtil.isImage(path)) "image" else if(large) "large" else "other"
|
||||||
val bytes = if(viewer == "other") JGitUtil.getContent(git, objectId, false) else None
|
val bytes = if(viewer == "other") JGitUtil.getContent(git, objectId, false) else None
|
||||||
|
|
||||||
val content = if(viewer == "other"){
|
val content = if(viewer == "other"){
|
||||||
if(bytes.isDefined && FileTypeUtil.isText(bytes.get)){
|
if(bytes.isDefined && FileUtil.isText(bytes.get)){
|
||||||
// text
|
// text
|
||||||
JGitUtil.ContentInfo("text", bytes.map(new String(_, "UTF-8")))
|
JGitUtil.ContentInfo("text", bytes.map(new String(_, "UTF-8")))
|
||||||
} else {
|
} else {
|
||||||
@@ -227,7 +227,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
|
|
||||||
// create zip file
|
// create zip file
|
||||||
val zipFile = new File(workDir, (if(revision.length == 40) revision.substring(0, 10) else revision) + ".zip")
|
val zipFile = new File(workDir, (if(revision.length == 40) revision.substring(0, 10) else revision) + ".zip")
|
||||||
CompressUtil.zip(zipFile, cloneDir)
|
FileUtil.createZipFile(zipFile, cloneDir)
|
||||||
|
|
||||||
contentType = "application/octet-stream"
|
contentType = "application/octet-stream"
|
||||||
zipFile
|
zipFile
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
package util
|
|
||||||
|
|
||||||
import java.io.File
|
|
||||||
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
|
|
||||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
|
|
||||||
import org.apache.commons.io.FileUtils
|
|
||||||
import org.apache.commons.io.IOUtils
|
|
||||||
|
|
||||||
object CompressUtil {
|
|
||||||
|
|
||||||
def zip(dest: File, dir: File): Unit = {
|
|
||||||
def addDirectoryToZip(out: ZipArchiveOutputStream, dir: File, path: String): Unit = {
|
|
||||||
dir.listFiles.map { file =>
|
|
||||||
if(file.isFile){
|
|
||||||
out.putArchiveEntry(new ZipArchiveEntry(path + "/" + file.getName))
|
|
||||||
out.write(FileUtils.readFileToByteArray(file))
|
|
||||||
out.closeArchiveEntry
|
|
||||||
} else if(file.isDirectory){
|
|
||||||
addDirectoryToZip(out, file, path + "/" + file.getName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val out = new ZipArchiveOutputStream(dest)
|
|
||||||
try {
|
|
||||||
addDirectoryToZip(out, dir, dir.getName)
|
|
||||||
} finally {
|
|
||||||
IOUtils.closeQuietly(out)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package util
|
|
||||||
|
|
||||||
import org.apache.commons.io.FilenameUtils
|
|
||||||
import java.net.URLConnection
|
|
||||||
|
|
||||||
object FileTypeUtil {
|
|
||||||
|
|
||||||
def getMimeType(name: String): String = {
|
|
||||||
val fileNameMap = URLConnection.getFileNameMap()
|
|
||||||
val mimeType = fileNameMap.getContentTypeFor(name)
|
|
||||||
if(mimeType == null){
|
|
||||||
"application/octeat-stream"
|
|
||||||
} else {
|
|
||||||
mimeType
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def isImage(name: String): Boolean = getMimeType(name).startsWith("image/")
|
|
||||||
|
|
||||||
def isLarge(size: Long): Boolean = (size > 1024 * 1000)
|
|
||||||
|
|
||||||
def isText(content: Array[Byte]): Boolean = !content.contains(0)
|
|
||||||
|
|
||||||
}
|
|
||||||
47
src/main/scala/util/FileUtil.scala
Normal file
47
src/main/scala/util/FileUtil.scala
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import org.apache.commons.io.{IOUtils, FileUtils, FilenameUtils}
|
||||||
|
import java.net.URLConnection
|
||||||
|
import java.io.File
|
||||||
|
import org.apache.commons.compress.archivers.zip.{ZipArchiveEntry, ZipArchiveOutputStream}
|
||||||
|
|
||||||
|
object FileUtil {
|
||||||
|
|
||||||
|
def getMimeType(name: String): String = {
|
||||||
|
val fileNameMap = URLConnection.getFileNameMap()
|
||||||
|
val mimeType = fileNameMap.getContentTypeFor(name)
|
||||||
|
if(mimeType == null){
|
||||||
|
"application/octeat-stream"
|
||||||
|
} else {
|
||||||
|
mimeType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def isImage(name: String): Boolean = getMimeType(name).startsWith("image/")
|
||||||
|
|
||||||
|
def isLarge(size: Long): Boolean = (size > 1024 * 1000)
|
||||||
|
|
||||||
|
def isText(content: Array[Byte]): Boolean = !content.contains(0)
|
||||||
|
|
||||||
|
def createZipFile(dest: File, dir: File): Unit = {
|
||||||
|
def addDirectoryToZip(out: ZipArchiveOutputStream, dir: File, path: String): Unit = {
|
||||||
|
dir.listFiles.map { file =>
|
||||||
|
if(file.isFile){
|
||||||
|
out.putArchiveEntry(new ZipArchiveEntry(path + "/" + file.getName))
|
||||||
|
out.write(FileUtils.readFileToByteArray(file))
|
||||||
|
out.closeArchiveEntry
|
||||||
|
} else if(file.isDirectory){
|
||||||
|
addDirectoryToZip(out, file, path + "/" + file.getName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val out = new ZipArchiveOutputStream(dest)
|
||||||
|
try {
|
||||||
|
addDirectoryToZip(out, dir, dir.getName)
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeQuietly(out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -374,7 +374,7 @@ object JGitUtil {
|
|||||||
*/
|
*/
|
||||||
def getContent(git: Git, id: ObjectId, large: Boolean): Option[Array[Byte]] = try {
|
def getContent(git: Git, id: ObjectId, large: Boolean): Option[Array[Byte]] = try {
|
||||||
val loader = git.getRepository.getObjectDatabase.open(id)
|
val loader = git.getRepository.getObjectDatabase.open(id)
|
||||||
if(large == false && FileTypeUtil.isLarge(loader.getSize)){
|
if(large == false && FileUtil.isLarge(loader.getSize)){
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
val db = git.getRepository.getObjectDatabase
|
val db = git.getRepository.getObjectDatabase
|
||||||
@@ -419,12 +419,12 @@ object JGitUtil {
|
|||||||
|
|
||||||
import scala.collection.JavaConverters._
|
import scala.collection.JavaConverters._
|
||||||
git.diff.setNewTree(newTreeIter).setOldTree(oldTreeIter).call.asScala.map { diff =>
|
git.diff.setNewTree(newTreeIter).setOldTree(oldTreeIter).call.asScala.map { diff =>
|
||||||
if(!fetchContent || FileTypeUtil.isImage(diff.getOldPath) || FileTypeUtil.isImage(diff.getNewPath)){
|
if(!fetchContent || FileUtil.isImage(diff.getOldPath) || FileUtil.isImage(diff.getNewPath)){
|
||||||
DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath, None, None)
|
DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath, None, None)
|
||||||
} else {
|
} else {
|
||||||
DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath,
|
DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath,
|
||||||
JGitUtil.getContent(git, diff.getOldId.toObjectId, false).filter(FileTypeUtil.isText).map(new String(_, "UTF-8")),
|
JGitUtil.getContent(git, diff.getOldId.toObjectId, false).filter(FileUtil.isText).map(new String(_, "UTF-8")),
|
||||||
JGitUtil.getContent(git, diff.getNewId.toObjectId, false).filter(FileTypeUtil.isText).map(new String(_, "UTF-8")))
|
JGitUtil.getContent(git, diff.getNewId.toObjectId, false).filter(FileUtil.isText).map(new String(_, "UTF-8")))
|
||||||
}
|
}
|
||||||
}.toList
|
}.toList
|
||||||
} else {
|
} else {
|
||||||
@@ -437,7 +437,7 @@ object JGitUtil {
|
|||||||
DiffInfo(ChangeType.ADD, null, walk.getPathString, None, None)
|
DiffInfo(ChangeType.ADD, null, walk.getPathString, None, None)
|
||||||
} else {
|
} else {
|
||||||
DiffInfo(ChangeType.ADD, null, walk.getPathString, None,
|
DiffInfo(ChangeType.ADD, null, walk.getPathString, None,
|
||||||
JGitUtil.getContent(git, walk.getObjectId(0), false).filter(FileTypeUtil.isText).map(new String(_, "UTF-8")))
|
JGitUtil.getContent(git, walk.getObjectId(0), false).filter(FileUtil.isText).map(new String(_, "UTF-8")))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
walk.release
|
walk.release
|
||||||
|
|||||||
Reference in New Issue
Block a user