diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index 4c804d4..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) }) 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 c41722a..257eaf1 100644 --- a/src/main/scala/gitbucket/core/util/JGitUtil.scala +++ b/src/main/scala/gitbucket/core/util/JGitUtil.scala @@ -120,14 +120,19 @@ * 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 @@ } } + 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 @@ 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) } } }