download.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "os"
  7. "path/filepath"
  8. "github.com/Unknwon/com"
  9. "github.com/go-martini/martini"
  10. "github.com/gogits/git"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/middleware"
  13. )
  14. func SingleDownload(ctx *middleware.Context, params martini.Params) {
  15. // Get tree path
  16. treename := params["_1"]
  17. blob, err := ctx.Repo.Commit.GetBlobByPath(treename)
  18. if err != nil {
  19. ctx.Handle(404, "repo.SingleDownload(GetBlobByPath)", err)
  20. return
  21. }
  22. data, err := blob.Data()
  23. if err != nil {
  24. ctx.Handle(404, "repo.SingleDownload(Data)", err)
  25. return
  26. }
  27. contentType, isTextFile := base.IsTextFile(data)
  28. _, isImageFile := base.IsImageFile(data)
  29. ctx.Res.Header().Set("Content-Type", contentType)
  30. if !isTextFile && !isImageFile {
  31. ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(treename))
  32. ctx.Res.Header().Set("Content-Transfer-Encoding", "binary")
  33. }
  34. ctx.Res.Write(data)
  35. }
  36. func ZipDownload(ctx *middleware.Context, params martini.Params) {
  37. commitId := ctx.Repo.CommitId
  38. archivesPath := filepath.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  39. if !com.IsDir(archivesPath) {
  40. if err := os.MkdirAll(archivesPath, 0755); err != nil {
  41. ctx.Handle(404, "ZipDownload -> os.Mkdir(archivesPath)", err)
  42. return
  43. }
  44. }
  45. archivePath := filepath.Join(archivesPath, commitId+".zip")
  46. if com.IsFile(archivePath) {
  47. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".zip")
  48. return
  49. }
  50. err := ctx.Repo.Commit.CreateArchive(archivePath, git.AT_ZIP)
  51. if err != nil {
  52. ctx.Handle(404, "ZipDownload -> CreateArchive "+archivePath, err)
  53. return
  54. }
  55. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".zip")
  56. }
  57. func TarGzDownload(ctx *middleware.Context, params martini.Params) {
  58. commitId := ctx.Repo.CommitId
  59. archivesPath := filepath.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  60. if !com.IsDir(archivesPath) {
  61. if err := os.MkdirAll(archivesPath, 0755); err != nil {
  62. ctx.Handle(404, "TarGzDownload -> os.Mkdir(archivesPath)", err)
  63. return
  64. }
  65. }
  66. archivePath := filepath.Join(archivesPath, commitId+".tar.gz")
  67. if com.IsFile(archivePath) {
  68. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".tar.gz")
  69. return
  70. }
  71. err := ctx.Repo.Commit.CreateArchive(archivePath, git.AT_TARGZ)
  72. if err != nil {
  73. ctx.Handle(404, "TarGzDownload -> CreateArchive "+archivePath, err)
  74. return
  75. }
  76. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".tar.gz")
  77. }