download.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "github.com/Unknwon/com"
  10. "github.com/go-martini/martini"
  11. "github.com/gogits/git"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/middleware"
  14. )
  15. func SingleDownload(ctx *middleware.Context, params martini.Params) {
  16. treename := params["_1"]
  17. blob, err := ctx.Repo.Commit.GetBlobByPath(treename)
  18. if err != nil {
  19. ctx.Handle(500, "repo.SingleDownload(GetBlobByPath)", err)
  20. return
  21. }
  22. dataRc, err := blob.Data()
  23. if err != nil {
  24. ctx.Handle(500, "repo.SingleDownload(Data)", err)
  25. return
  26. }
  27. buf := make([]byte, 1024)
  28. n, _ := dataRc.Read(buf)
  29. if n > 0 {
  30. buf = buf[:n]
  31. }
  32. defer func() {
  33. dataRc.Close()
  34. }()
  35. contentType, isTextFile := base.IsTextFile(buf)
  36. _, isImageFile := base.IsImageFile(buf)
  37. ctx.Res.Header().Set("Content-Type", contentType)
  38. if !isTextFile && !isImageFile {
  39. ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(treename))
  40. ctx.Res.Header().Set("Content-Transfer-Encoding", "binary")
  41. }
  42. ctx.Res.Write(buf)
  43. io.Copy(ctx.Res, dataRc)
  44. }
  45. func ZipDownload(ctx *middleware.Context, params martini.Params) {
  46. commitId := ctx.Repo.CommitId
  47. archivesPath := filepath.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  48. if !com.IsDir(archivesPath) {
  49. if err := os.MkdirAll(archivesPath, 0755); err != nil {
  50. ctx.Handle(500, "ZipDownload -> os.Mkdir(archivesPath)", err)
  51. return
  52. }
  53. }
  54. archivePath := filepath.Join(archivesPath, commitId+".zip")
  55. if com.IsFile(archivePath) {
  56. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".zip")
  57. return
  58. }
  59. if err := ctx.Repo.Commit.CreateArchive(archivePath, git.AT_ZIP); err != nil {
  60. ctx.Handle(500, "ZipDownload -> CreateArchive "+archivePath, err)
  61. return
  62. }
  63. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".zip")
  64. }
  65. func TarGzDownload(ctx *middleware.Context, params martini.Params) {
  66. commitId := ctx.Repo.CommitId
  67. archivesPath := filepath.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  68. if !com.IsDir(archivesPath) {
  69. if err := os.MkdirAll(archivesPath, 0755); err != nil {
  70. ctx.Handle(500, "TarGzDownload -> os.Mkdir(archivesPath)", err)
  71. return
  72. }
  73. }
  74. archivePath := filepath.Join(archivesPath, commitId+".tar.gz")
  75. if com.IsFile(archivePath) {
  76. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".tar.gz")
  77. return
  78. }
  79. if err := ctx.Repo.Commit.CreateArchive(archivePath, git.AT_TARGZ); err != nil {
  80. ctx.Handle(500, "TarGzDownload -> CreateArchive "+archivePath, err)
  81. return
  82. }
  83. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".tar.gz")
  84. }