You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.7 KiB

  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/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/middleware"
  12. )
  13. func SingleDownload(ctx *middleware.Context, params martini.Params) {
  14. // Get tree path
  15. treename := params["_1"]
  16. blob, err := ctx.Repo.Commit.GetBlobByPath(treename)
  17. if err != nil {
  18. ctx.Handle(404, "repo.SingleDownload(GetBlobByPath)", err)
  19. return
  20. }
  21. data, err := blob.Data()
  22. if err != nil {
  23. ctx.Handle(404, "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")
  38. if !com.IsDir(archivesPath) {
  39. if err := os.Mkdir(archivesPath, 0755); err != nil {
  40. ctx.Handle(404, "ZipDownload -> os.Mkdir(archivesPath)", err)
  41. return
  42. }
  43. }
  44. zipPath := filepath.Join(archivesPath, commitId+".zip")
  45. if com.IsFile(zipPath) {
  46. ctx.ServeFile(zipPath, ctx.Repo.Repository.Name+".zip")
  47. return
  48. }
  49. err := ctx.Repo.Commit.CreateArchive(zipPath)
  50. if err != nil {
  51. ctx.Handle(404, "ZipDownload -> CreateArchive "+zipPath, err)
  52. return
  53. }
  54. ctx.ServeFile(zipPath, ctx.Repo.Repository.Name+".zip")
  55. }