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

download.go 2.2 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "fmt"
  8. "io"
  9. "path"
  10. "strings"
  11. "code.gitea.io/git"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/context"
  14. )
  15. // ServeData download file from io.Reader
  16. func ServeData(ctx *context.Context, name string, reader io.Reader) error {
  17. buf := make([]byte, 1024)
  18. n, _ := reader.Read(buf)
  19. if n >= 0 {
  20. buf = buf[:n]
  21. }
  22. ctx.Resp.Header().Set("Cache-Control", "public,max-age=86400")
  23. name = path.Base(name)
  24. // Google Chrome dislike commas in filenames, so let's change it to a space
  25. name = strings.Replace(name, ",", " ", -1)
  26. if base.IsTextFile(buf) || ctx.QueryBool("render") {
  27. ctx.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
  28. } else if base.IsImageFile(buf) || base.IsPDFFile(buf) {
  29. ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name))
  30. } else {
  31. ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))
  32. }
  33. ctx.Resp.Write(buf)
  34. _, err := io.Copy(ctx.Resp, reader)
  35. return err
  36. }
  37. // ServeBlob download a git.Blob
  38. func ServeBlob(ctx *context.Context, blob *git.Blob) error {
  39. dataRc, err := blob.DataAsync()
  40. if err != nil {
  41. return err
  42. }
  43. defer dataRc.Close()
  44. return ServeData(ctx, ctx.Repo.TreePath, dataRc)
  45. }
  46. // SingleDownload download a file by repos path
  47. func SingleDownload(ctx *context.Context) {
  48. blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
  49. if err != nil {
  50. if git.IsErrNotExist(err) {
  51. ctx.NotFound("GetBlobByPath", nil)
  52. } else {
  53. ctx.ServerError("GetBlobByPath", err)
  54. }
  55. return
  56. }
  57. if err = ServeBlob(ctx, blob); err != nil {
  58. ctx.ServerError("ServeBlob", err)
  59. }
  60. }
  61. // DownloadByID download a file by sha1 ID
  62. func DownloadByID(ctx *context.Context) {
  63. blob, err := ctx.Repo.GitRepo.GetBlob(ctx.Params("sha"))
  64. if err != nil {
  65. if git.IsErrNotExist(err) {
  66. ctx.NotFound("GetBlob", nil)
  67. } else {
  68. ctx.ServerError("GetBlob", err)
  69. }
  70. return
  71. }
  72. if err = ServeBlob(ctx, blob); err != nil {
  73. ctx.ServerError("ServeBlob", err)
  74. }
  75. }