diff options
author | Unknwon <joe2010xtmf@163.com> | 2014-07-26 02:28:04 -0400 |
---|---|---|
committer | Unknwon <joe2010xtmf@163.com> | 2014-07-26 02:28:04 -0400 |
commit | 5c4bc3c848fb4bd46ad5ceeacd82cdfa8f2b5635 (patch) | |
tree | 213666141efaf773c4411d41482387f1250068f8 /routers/repo/download.go | |
parent | 3f38ff6c09f0497980ad13fda9803907cee6d612 (diff) | |
download | gitea-5c4bc3c848fb4bd46ad5ceeacd82cdfa8f2b5635.tar.gz gitea-5c4bc3c848fb4bd46ad5ceeacd82cdfa8f2b5635.zip |
Huge updates!!!!! Be careful to merge!!!!
Diffstat (limited to 'routers/repo/download.go')
-rw-r--r-- | routers/repo/download.go | 73 |
1 files changed, 32 insertions, 41 deletions
diff --git a/routers/repo/download.go b/routers/repo/download.go index 42bce2b1ff..abb9b06292 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -5,50 +5,41 @@ package repo import ( - // "io" - // "os" - // "path/filepath" + "io" + "path" - // "github.com/Unknwon/com" - - // "github.com/gogits/git" - - // "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" ) func SingleDownload(ctx *middleware.Context) { - // treename := params["_1"] - - // blob, err := ctx.Repo.Commit.GetBlobByPath(treename) - // if err != nil { - // ctx.Handle(500, "repo.SingleDownload(GetBlobByPath)", err) - // return - // } - - // dataRc, err := blob.Data() - // if err != nil { - // ctx.Handle(500, "repo.SingleDownload(Data)", err) - // return - // } - - // buf := make([]byte, 1024) - // n, _ := dataRc.Read(buf) - // if n > 0 { - // buf = buf[:n] - // } - - // defer func() { - // dataRc.Close() - // }() - - // contentType, isTextFile := base.IsTextFile(buf) - // _, isImageFile := base.IsImageFile(buf) - // 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(buf) - // io.Copy(ctx.Res, dataRc) + treename := ctx.Params("*") + + blob, err := ctx.Repo.Commit.GetBlobByPath(treename) + if err != nil { + ctx.Handle(500, "GetBlobByPath", err) + return + } + + dataRc, err := blob.Data() + if err != nil { + ctx.Handle(500, "repo.SingleDownload(Data)", err) + return + } + + buf := make([]byte, 1024) + n, _ := dataRc.Read(buf) + if n > 0 { + buf = buf[:n] + } + + contentType, isTextFile := base.IsTextFile(buf) + _, isImageFile := base.IsImageFile(buf) + ctx.Resp.Header().Set("Content-Type", contentType) + if !isTextFile && !isImageFile { + ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+path.Base(treename)) + ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary") + } + ctx.Resp.Write(buf) + io.Copy(ctx.Resp, dataRc) } |