summaryrefslogtreecommitdiffstats
path: root/routers/repo/download.go
diff options
context:
space:
mode:
authorslene <vslene@gmail.com>2014-04-16 00:27:29 +0800
committerslene <vslene@gmail.com>2014-04-16 00:29:03 +0800
commit4fafc7605245ef175a8fb8558eb7c30c4f7b0400 (patch)
tree4479b8792564c088931fd025ce45d31746bf064e /routers/repo/download.go
parent5378bb326b14192cbbc1309c3142cfc49c74a882 (diff)
downloadgitea-4fafc7605245ef175a8fb8558eb7c30c4f7b0400.tar.gz
gitea-4fafc7605245ef175a8fb8558eb7c30c4f7b0400.zip
zip archive download
Diffstat (limited to 'routers/repo/download.go')
-rw-r--r--routers/repo/download.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/routers/repo/download.go b/routers/repo/download.go
new file mode 100644
index 0000000000..017d957155
--- /dev/null
+++ b/routers/repo/download.go
@@ -0,0 +1,68 @@
+// Copyright 2014 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package repo
+
+import (
+ "os"
+ "path/filepath"
+
+ "github.com/Unknwon/com"
+ "github.com/go-martini/martini"
+
+ "github.com/gogits/gogs/modules/base"
+ "github.com/gogits/gogs/modules/middleware"
+)
+
+func SingleDownload(ctx *middleware.Context, params martini.Params) {
+ // Get tree path
+ treename := params["_1"]
+
+ blob, err := ctx.Repo.Commit.GetBlobByPath(treename)
+ if err != nil {
+ ctx.Handle(404, "repo.SingleDownload(GetBlobByPath)", err)
+ return
+ }
+
+ data, err := blob.Data()
+ if err != nil {
+ ctx.Handle(404, "repo.SingleDownload(Data)", err)
+ return
+ }
+
+ contentType, isTextFile := base.IsTextFile(data)
+ _, isImageFile := base.IsImageFile(data)
+ ctx.Res.Header().Set("Content-Type", contentType)
+ if !isTextFile && !isImageFile {
+ ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(treename))
+ ctx.Res.Header().Set("Content-Transfer-Encoding", "binary")
+ }
+ ctx.Res.Write(data)
+}
+
+func ZipDownload(ctx *middleware.Context, params martini.Params) {
+ commitId := ctx.Repo.CommitId
+ archivesPath := filepath.Join(ctx.Repo.GitRepo.Path, "archives")
+ if !com.IsDir(archivesPath) {
+ if err := os.Mkdir(archivesPath, 0755); err != nil {
+ ctx.Handle(404, "ZipDownload -> os.Mkdir(archivesPath)", err)
+ return
+ }
+ }
+
+ zipPath := filepath.Join(archivesPath, commitId+".zip")
+
+ if com.IsFile(zipPath) {
+ ctx.ServeFile(zipPath, ctx.Repo.Repository.Name+".zip")
+ return
+ }
+
+ err := ctx.Repo.Commit.CreateArchive(zipPath)
+ if err != nil {
+ ctx.Handle(404, "ZipDownload -> CreateArchive "+zipPath, err)
+ return
+ }
+
+ ctx.ServeFile(zipPath, ctx.Repo.Repository.Name+".zip")
+}