From 2a489870a19da57cbee64feee8ba3238248eef07 Mon Sep 17 00:00:00 2001 From: Naoki Takezoe Date: Thu, 24 May 2018 01:25:07 +0900 Subject: [PATCH 1/2] Fix for repository downloading improvement in #2014 --- .../RepositoryViewerController.scala | 40 +++++++++---------- .../scala/gitbucket/core/util/JGitUtil.scala | 4 +- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index ed24a88f0..93277038b 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -31,7 +31,6 @@ import org.eclipse.jgit.archive.{TgzFormat, ZipFormat} import org.eclipse.jgit.dircache.{DirCache, DirCacheBuilder} import org.eclipse.jgit.errors.MissingObjectException import org.eclipse.jgit.lib._ -import org.eclipse.jgit.revwalk.RevWalk import org.eclipse.jgit.transport.{ReceiveCommand, ReceivePack} import org.eclipse.jgit.treewalk.TreeWalk import org.eclipse.jgit.treewalk.filter.PathFilter @@ -1151,28 +1150,25 @@ trait RepositoryViewerControllerBase extends ControllerBase { val repositorySuffix = (if (sha1.startsWith(revision)) sha1 else revision).replace('/', '-') val pathSuffix = if (path.isEmpty) "" else '-' + path.replace('/', '-') val baseName = repository.name + "-" + repositorySuffix + pathSuffix - val filename = baseName + archiveFormat - using(new RevWalk(git.getRepository)) { revWalk => - using(new TreeWalk(git.getRepository)) { treeWalk => - treeWalk.addTree(revCommit.getTree) - treeWalk.setRecursive(true) - if (!path.isEmpty) { - treeWalk.setFilter(PathFilter.create(path)) - } - if (treeWalk != null) { - while (treeWalk.next()) { - val entryPath = - if (path.isEmpty) baseName + "/" + treeWalk.getPathString - else path.split("/").last + treeWalk.getPathString.substring(path.length) - val size = JGitUtil.getFileSize(git, repository, treeWalk) - val mode = treeWalk.getFileMode.getBits - val entry: ArchiveEntry = entryCreator(entryPath, size, mode) - JGitUtil.openFile(git, repository, revCommit.getTree, treeWalk.getPathString) { in => - archive.putArchiveEntry(entry) - IOUtils.copy(in, archive) - archive.closeArchiveEntry() - } + using(new TreeWalk(git.getRepository)) { treeWalk => + treeWalk.addTree(revCommit.getTree) + treeWalk.setRecursive(true) + if (!path.isEmpty) { + treeWalk.setFilter(PathFilter.create(path)) + } + if (treeWalk != null) { + while (treeWalk.next()) { + val entryPath = + if (path.isEmpty) baseName + "/" + treeWalk.getPathString + else path.split("/").last + treeWalk.getPathString.substring(path.length) + val size = JGitUtil.getFileSize(git, repository, treeWalk) + val mode = treeWalk.getFileMode.getBits + val entry: ArchiveEntry = entryCreator(entryPath, size, mode) + JGitUtil.openFile(git, repository, revCommit.getTree, treeWalk.getPathString) { in => + archive.putArchiveEntry(entry) + IOUtils.copy(in, archive) + archive.closeArchiveEntry() } } } diff --git a/src/main/scala/gitbucket/core/util/JGitUtil.scala b/src/main/scala/gitbucket/core/util/JGitUtil.scala index 01cd50076..b9f08304f 100644 --- a/src/main/scala/gitbucket/core/util/JGitUtil.scala +++ b/src/main/scala/gitbucket/core/util/JGitUtil.scala @@ -1253,7 +1253,7 @@ object JGitUtil { f(in) } } else { - throw new EmptyLfsAttributesException + throw new NoSuchElementException("LFS attribute is empty.") } } else { using(loader.openStream()) { in => @@ -1277,6 +1277,4 @@ object JGitUtil { JGitUtil.getLfsObjects(text) } - case class EmptyLfsAttributesException() extends Exception - } From 1adb0b7bcf2179edd7661e5542c451d13fcb414b Mon Sep 17 00:00:00 2001 From: Naoki Takezoe Date: Thu, 24 May 2018 16:25:43 +0900 Subject: [PATCH 2/2] Include repository name and directory name in download filename --- .../RepositoryViewerController.scala | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index 93277038b..61642c739 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -1175,11 +1175,17 @@ trait RepositoryViewerControllerBase extends ControllerBase { } } } - val tarRe = """\.tar\.(gz|bz2|xz)$""".r + + val suffix = path.split("/").lastOption.map("-" + _).getOrElse("") + val zipRe = """(.+)\.zip$""".r + val tarRe = """(.+)\.tar\.(gz|bz2|xz)$""".r filename match { - case name if name.endsWith(".zip") => - response.setHeader("Content-Disposition", s"attachment; filename=${filename}") + case zipRe(branch) => + response.setHeader( + "Content-Disposition", + s"attachment; filename=${repository.name}-${branch}${suffix}.zip" + ) contentType = "application/octet-stream" response.setBufferSize(1024 * 1024); using(new ZipArchiveOutputStream(response.getOutputStream)) { zip => @@ -1191,8 +1197,11 @@ trait RepositoryViewerControllerBase extends ControllerBase { } } () - case tarRe(compressor) => - response.setHeader("Content-Disposition", s"attachment; filename=${filename}") + case tarRe(branch, compressor) => + response.setHeader( + "Content-Disposition", + s"attachment; filename=${repository.name}-${branch}${suffix}.tar.${compressor}" + ) contentType = "application/octet-stream" response.setBufferSize(1024 * 1024) using(compressor match {