Skip to content

Commit 4e63ea8

Browse files
committed
Download snippet repository as zip or tar.gz archive file
1 parent 1c10913 commit 4e63ea8

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/main/scala/app/GistController.scala

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package app
33
import java.io.File
44
import jp.sf.amateras.scalatra.forms._
55
import model.{GistUser, Gist, Account}
6-
import service.{AccountService, GistService}
6+
import org.apache.commons.io.FileUtils
7+
import org.scalatra.BadRequest
8+
import service.{RepositoryService, AccountService, GistService}
79
import util.Directory._
810
import util._
911
import util.ControlUtil._
@@ -221,6 +223,37 @@ trait GistControllerBase extends ControllerBase {
221223
} else NotFound
222224
}
223225

226+
get("/gist/:userName/:repoName/download/*"){
227+
val format = multiParams("splat").head match {
228+
case name if name.endsWith(".zip") => "zip"
229+
case name if name.endsWith(".tar.gz") => "tar.gz"
230+
}
231+
232+
val userName = params("userName")
233+
val repoName = params("repoName")
234+
235+
val workDir = getDownloadWorkDir(userName, repoName, session.getId)
236+
if(workDir.exists) {
237+
FileUtils.deleteDirectory(workDir)
238+
}
239+
workDir.mkdirs
240+
241+
using(Git.open(new File(GistRepoDir, userName + "/" + repoName))){ git =>
242+
val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve("master"))
243+
244+
contentType = "application/octet-stream"
245+
response.setHeader("Content-Disposition", s"attachment; filename=${repoName}.${format}")
246+
response.setBufferSize(1024 * 1024);
247+
248+
git.archive
249+
.setFormat(format)
250+
.setTree(revCommit.getTree)
251+
.setOutputStream(response.getOutputStream)
252+
.call()
253+
}
254+
}
255+
256+
224257
get("/gist/:userName"){
225258
_gist(params("userName"))
226259
}

src/main/twirl/gist/menu.scala.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,25 @@
3838
"Revision")
3939
<li style="height: 12px"><div class="gradient pull-left" style="height: 12px"></div></li>
4040
</ul>
41+
@*
42+
<div class="small">
43+
<strong id="repository-url-proto">HTTP</strong> <span class="mute">clone URL</span>
44+
</div>
45+
@helper.html.copy("repository-url-copy", "http://localhost:8080/"){
46+
<input type="text" value="http://localhost:8080/" id="repository-url" readonly>
47+
}
48+
@if(settings.ssh && loginAccount.isDefined){
49+
<div class="small">
50+
<span class="mute">You can clone <a href="javascript:void(0);" id="repository-url-http">HTTP</a> or <a href="javascript:void(0);" id="repository-url-ssh">SSH</a>.</span>
51+
</div>
52+
}
53+
*@
54+
<div style="margin-top: 10px;">
55+
<a href="@path/gist/@{gist.userName}/@{gist.repositoryName}/download/@{gist.repositoryName}.zip"
56+
class="btn btn-small" style="width: 147px;"><i class="icon-download-alt"></i>Download ZIP</a>
57+
</div>
58+
<div style="margin-top: 10px;">
59+
<a href="@path/gist/@{gist.userName}/@{gist.repositoryName}/download/@{gist.repositoryName}.tar.gz"
60+
class="btn btn-small" style="width: 147px;"><i class="icon-download-alt"></i>Download TAR.GZ</a>
61+
</div>
4162
</div>

0 commit comments

Comments
 (0)