Add file size to ContentInfo

This commit is contained in:
Mitsuhiro Koga
2017-02-11 23:49:55 +09:00
parent 6ec533990f
commit 39e55bde2d
3 changed files with 27 additions and 5 deletions

View File

@@ -173,7 +173,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
val (branch, path) = repository.splitPath(multiParams("splat").head)
val protectedBranch = getProtectedBranchInfo(repository.owner, repository.name, branch).needStatusCheck(context.loginAccount.get.userName)
html.editor(branch, repository, if(path.length == 0) Nil else path.split("/").toList,
None, JGitUtil.ContentInfo("text", None, Some("UTF-8")),
None, JGitUtil.ContentInfo("text", None, None, Some("UTF-8")),
protectedBranch)
})

View File

@@ -66,4 +66,5 @@ object FileUtil {
def getLfsFilePath(owner: String, repository: String, oid: String): String =
Directory.getLfsDir(owner, repository) + "/" + oid
def readableSize(size: Long): String = FileUtils.byteCountToDisplaySize(size)
}

View File

@@ -120,14 +120,19 @@ object JGitUtil {
* The file content data for the file content view of the repository viewer.
*
* @param viewType "image", "large" or "other"
* @param size total size of object in bytes
* @param content the string content
* @param charset the character encoding
*/
case class ContentInfo(viewType: String, content: Option[String], charset: Option[String]){
case class ContentInfo(viewType: String, size: Option[Long], content: Option[String], charset: Option[String]){
/**
* the line separator of this content ("LF" or "CRLF")
*/
val lineSeparator: String = if(content.exists(_.indexOf("\r\n") >= 0)) "CRLF" else "LF"
/**
* a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes)
*/
val readableSize = FileUtil.readableSize(size.getOrElse(0))
}
/**
@@ -804,6 +809,21 @@ object JGitUtil {
}
}
def getContentSize(loader: ObjectLoader): Long = {
if(loader.isLarge) {
loader.getSize
} else {
val bytes = loader.getCachedBytes
val text = new String(bytes, "UTF-8")
val attr = getLfsObjects(text)
attr.get("size") match {
case Some(size) => size.toLong
case None => loader.getSize
}
}
}
def getContentInfo(git: Git, path: String, objectId: ObjectId): ContentInfo = {
// Viewer
using(git.getRepository.getObjectDatabase){ db =>
@@ -811,18 +831,19 @@ object JGitUtil {
val large = FileUtil.isLarge(loader.getSize)
val viewer = if(FileUtil.isImage(path)) "image" else if(large) "large" else "other"
val bytes = if(viewer == "other") JGitUtil.getContentFromId(git, objectId, false) else None
val size = Some(getContentSize(loader))
if(viewer == "other"){
if(bytes.isDefined && FileUtil.isText(bytes.get)){
// text
ContentInfo("text", Some(StringUtil.convertFromByteArray(bytes.get)), Some(StringUtil.detectEncoding(bytes.get)))
ContentInfo("text", size, Some(StringUtil.convertFromByteArray(bytes.get)), Some(StringUtil.detectEncoding(bytes.get)))
} else {
// binary
ContentInfo("binary", None, None)
ContentInfo("binary", size, None, None)
}
} else {
// image or large
ContentInfo(viewer, None, None)
ContentInfo(viewer, size, None, None)
}
}
}