Merge pull request #2027 from gitbucket/fix-download

Fix for repository downloading improvement
This commit is contained in:
Naoki Takezoe
2018-05-24 18:20:30 +09:00
committed by GitHub
2 changed files with 33 additions and 30 deletions

View File

@@ -31,7 +31,6 @@ import org.eclipse.jgit.archive.{TgzFormat, ZipFormat}
import org.eclipse.jgit.dircache.{DirCache, DirCacheBuilder} import org.eclipse.jgit.dircache.{DirCache, DirCacheBuilder}
import org.eclipse.jgit.errors.MissingObjectException import org.eclipse.jgit.errors.MissingObjectException
import org.eclipse.jgit.lib._ import org.eclipse.jgit.lib._
import org.eclipse.jgit.revwalk.RevWalk
import org.eclipse.jgit.transport.{ReceiveCommand, ReceivePack} import org.eclipse.jgit.transport.{ReceiveCommand, ReceivePack}
import org.eclipse.jgit.treewalk.TreeWalk import org.eclipse.jgit.treewalk.TreeWalk
import org.eclipse.jgit.treewalk.filter.PathFilter import org.eclipse.jgit.treewalk.filter.PathFilter
@@ -1151,9 +1150,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
val repositorySuffix = (if (sha1.startsWith(revision)) sha1 else revision).replace('/', '-') val repositorySuffix = (if (sha1.startsWith(revision)) sha1 else revision).replace('/', '-')
val pathSuffix = if (path.isEmpty) "" else '-' + path.replace('/', '-') val pathSuffix = if (path.isEmpty) "" else '-' + path.replace('/', '-')
val baseName = repository.name + "-" + repositorySuffix + pathSuffix val baseName = repository.name + "-" + repositorySuffix + pathSuffix
val filename = baseName + archiveFormat
using(new RevWalk(git.getRepository)) { revWalk =>
using(new TreeWalk(git.getRepository)) { treeWalk => using(new TreeWalk(git.getRepository)) { treeWalk =>
treeWalk.addTree(revCommit.getTree) treeWalk.addTree(revCommit.getTree)
treeWalk.setRecursive(true) treeWalk.setRecursive(true)
@@ -1178,12 +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 { filename match {
case name if name.endsWith(".zip") => case zipRe(branch) =>
response.setHeader("Content-Disposition", s"attachment; filename=${filename}") response.setHeader(
"Content-Disposition",
s"attachment; filename=${repository.name}-${branch}${suffix}.zip"
)
contentType = "application/octet-stream" contentType = "application/octet-stream"
response.setBufferSize(1024 * 1024); response.setBufferSize(1024 * 1024);
using(new ZipArchiveOutputStream(response.getOutputStream)) { zip => using(new ZipArchiveOutputStream(response.getOutputStream)) { zip =>
@@ -1195,8 +1197,11 @@ trait RepositoryViewerControllerBase extends ControllerBase {
} }
} }
() ()
case tarRe(compressor) => case tarRe(branch, compressor) =>
response.setHeader("Content-Disposition", s"attachment; filename=${filename}") response.setHeader(
"Content-Disposition",
s"attachment; filename=${repository.name}-${branch}${suffix}.tar.${compressor}"
)
contentType = "application/octet-stream" contentType = "application/octet-stream"
response.setBufferSize(1024 * 1024) response.setBufferSize(1024 * 1024)
using(compressor match { using(compressor match {

View File

@@ -1253,7 +1253,7 @@ object JGitUtil {
f(in) f(in)
} }
} else { } else {
throw new EmptyLfsAttributesException throw new NoSuchElementException("LFS attribute is empty.")
} }
} else { } else {
using(loader.openStream()) { in => using(loader.openStream()) { in =>
@@ -1277,6 +1277,4 @@ object JGitUtil {
JGitUtil.getLfsObjects(text) JGitUtil.getLfsObjects(text)
} }
case class EmptyLfsAttributesException() extends Exception
} }