download.go 2.5 KB

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