mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-08 22:45:51 +01:00
CompressUtil and FileTypeUtil are merged to FileUtil.
This commit is contained in:
@@ -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 {
|
||||
val loader = git.getRepository.getObjectDatabase.open(id)
|
||||
if(large == false && FileTypeUtil.isLarge(loader.getSize)){
|
||||
if(large == false && FileUtil.isLarge(loader.getSize)){
|
||||
None
|
||||
} else {
|
||||
val db = git.getRepository.getObjectDatabase
|
||||
@@ -419,12 +419,12 @@ object JGitUtil {
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
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)
|
||||
} else {
|
||||
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.getNewId.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(FileUtil.isText).map(new String(_, "UTF-8")))
|
||||
}
|
||||
}.toList
|
||||
} else {
|
||||
@@ -437,7 +437,7 @@ object JGitUtil {
|
||||
DiffInfo(ChangeType.ADD, null, walk.getPathString, None, None)
|
||||
} else {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user