diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index b21b03f..8004050 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -173,7 +173,7 @@ 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) }) @@ -308,13 +308,8 @@ val bytes = loader.getCachedBytes val text = new String(bytes, "UTF-8") - if(text.startsWith("version https://git-lfs.github.com/spec/v1")){ - // LFS objects - val attrs = text.split("\n").map { line => - val dim = line.split(" ") - dim(0) -> dim(1) - }.toMap - + val attrs = JGitUtil.getLfsObjects(text) + if(attrs.nonEmpty) { response.setContentLength(attrs("size").toInt) val oid = attrs("oid").split(":")(1) diff --git a/src/main/scala/gitbucket/core/util/FileUtil.scala b/src/main/scala/gitbucket/core/util/FileUtil.scala index 4836c23..8ca49e5 100644 --- a/src/main/scala/gitbucket/core/util/FileUtil.scala +++ b/src/main/scala/gitbucket/core/util/FileUtil.scala @@ -66,4 +66,5 @@ def getLfsFilePath(owner: String, repository: String, oid: String): String = Directory.getLfsDir(owner, repository) + "/" + oid + def readableSize(size: Long): String = FileUtils.byteCountToDisplaySize(size) } diff --git a/src/main/scala/gitbucket/core/util/JGitUtil.scala b/src/main/scala/gitbucket/core/util/JGitUtil.scala index 786edca..e26a733 100644 --- a/src/main/scala/gitbucket/core/util/JGitUtil.scala +++ b/src/main/scala/gitbucket/core/util/JGitUtil.scala @@ -120,10 +120,11 @@ * 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") */ @@ -792,6 +793,33 @@ } } + def getLfsObjects(text: String): Map[String, String] = { + if(text.startsWith("version https://git-lfs.github.com/spec/v1")){ + // LFS objects + text.split("\n").map { line => + val dim = line.split(" ") + dim(0) -> dim(1) + }.toMap + } else { + Map.empty + } + } + + 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 => @@ -799,18 +827,19 @@ 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) } } } diff --git a/src/main/scala/gitbucket/core/view/helpers.scala b/src/main/scala/gitbucket/core/view/helpers.scala index beff9d5..dc4e1c1 100644 --- a/src/main/scala/gitbucket/core/view/helpers.scala +++ b/src/main/scala/gitbucket/core/view/helpers.scala @@ -380,4 +380,11 @@ } } + /** + * a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes) + * + * @param size total size of object in bytes + */ + def readableSize(size: Option[Long]): String = FileUtil.readableSize(size.getOrElse(0)) + } diff --git a/src/main/twirl/gitbucket/core/repo/blob.scala.html b/src/main/twirl/gitbucket/core/repo/blob.scala.html index 9d5406a..8c50273 100644 --- a/src/main/twirl/gitbucket/core/repo/blob.scala.html +++ b/src/main/twirl/gitbucket/core/repo/blob.scala.html @@ -54,6 +54,7 @@ @helpers.avatar(latestCommit, 28) @helpers.user(latestCommit.authorName, latestCommit.authorEmailAddress, "username strong") @gitbucket.core.helper.html.datetimeago(latestCommit.commitTime) + @helpers.readableSize(content.size)