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.

160 lines
4.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/git"
  14. "code.gitea.io/gitea/modules/lfs"
  15. "code.gitea.io/gitea/modules/log"
  16. )
  17. // ServeData download file from io.Reader
  18. func ServeData(ctx *context.Context, name string, reader io.Reader) error {
  19. buf := make([]byte, 1024)
  20. n, _ := reader.Read(buf)
  21. if n >= 0 {
  22. buf = buf[:n]
  23. }
  24. ctx.Resp.Header().Set("Cache-Control", "public,max-age=86400")
  25. name = path.Base(name)
  26. // Google Chrome dislike commas in filenames, so let's change it to a space
  27. name = strings.Replace(name, ",", " ", -1)
  28. if base.IsTextFile(buf) || ctx.QueryBool("render") {
  29. ctx.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
  30. } else if base.IsImageFile(buf) || base.IsPDFFile(buf) {
  31. ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name))
  32. } else {
  33. ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))
  34. }
  35. _, err := ctx.Resp.Write(buf)
  36. if err != nil {
  37. return err
  38. }
  39. _, err = io.Copy(ctx.Resp, reader)
  40. return err
  41. }
  42. // ServeBlob download a git.Blob
  43. func ServeBlob(ctx *context.Context, blob *git.Blob) error {
  44. dataRc, err := blob.DataAsync()
  45. if err != nil {
  46. return err
  47. }
  48. defer func() {
  49. if err = dataRc.Close(); err != nil {
  50. log.Error("ServeBlob: Close: %v", err)
  51. }
  52. }()
  53. return ServeData(ctx, ctx.Repo.TreePath, dataRc)
  54. }
  55. // ServeBlobOrLFS download a git.Blob redirecting to LFS if necessary
  56. func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob) error {
  57. dataRc, err := blob.DataAsync()
  58. if err != nil {
  59. return err
  60. }
  61. defer func() {
  62. if err = dataRc.Close(); err != nil {
  63. log.Error("ServeBlobOrLFS: Close: %v", err)
  64. }
  65. }()
  66. if meta, _ := lfs.ReadPointerFile(dataRc); meta != nil {
  67. meta, _ = ctx.Repo.Repository.GetLFSMetaObjectByOid(meta.Oid)
  68. if meta == nil {
  69. return ServeBlob(ctx, blob)
  70. }
  71. lfsDataRc, err := lfs.ReadMetaObject(meta)
  72. if err != nil {
  73. return err
  74. }
  75. defer func() {
  76. if err = lfsDataRc.Close(); err != nil {
  77. log.Error("ServeBlobOrLFS: Close: %v", err)
  78. }
  79. }()
  80. return ServeData(ctx, ctx.Repo.TreePath, lfsDataRc)
  81. }
  82. return ServeBlob(ctx, blob)
  83. }
  84. // SingleDownload download a file by repos path
  85. func SingleDownload(ctx *context.Context) {
  86. blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
  87. if err != nil {
  88. if git.IsErrNotExist(err) {
  89. ctx.NotFound("GetBlobByPath", nil)
  90. } else {
  91. ctx.ServerError("GetBlobByPath", err)
  92. }
  93. return
  94. }
  95. if err = ServeBlob(ctx, blob); err != nil {
  96. ctx.ServerError("ServeBlob", err)
  97. }
  98. }
  99. // SingleDownloadOrLFS download a file by repos path redirecting to LFS if necessary
  100. func SingleDownloadOrLFS(ctx *context.Context) {
  101. blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
  102. if err != nil {
  103. if git.IsErrNotExist(err) {
  104. ctx.NotFound("GetBlobByPath", nil)
  105. } else {
  106. ctx.ServerError("GetBlobByPath", err)
  107. }
  108. return
  109. }
  110. if err = ServeBlobOrLFS(ctx, blob); err != nil {
  111. ctx.ServerError("ServeBlobOrLFS", err)
  112. }
  113. }
  114. // DownloadByID download a file by sha1 ID
  115. func DownloadByID(ctx *context.Context) {
  116. blob, err := ctx.Repo.GitRepo.GetBlob(ctx.Params("sha"))
  117. if err != nil {
  118. if git.IsErrNotExist(err) {
  119. ctx.NotFound("GetBlob", nil)
  120. } else {
  121. ctx.ServerError("GetBlob", err)
  122. }
  123. return
  124. }
  125. if err = ServeBlob(ctx, blob); err != nil {
  126. ctx.ServerError("ServeBlob", err)
  127. }
  128. }
  129. // DownloadByIDOrLFS download a file by sha1 ID taking account of LFS
  130. func DownloadByIDOrLFS(ctx *context.Context) {
  131. blob, err := ctx.Repo.GitRepo.GetBlob(ctx.Params("sha"))
  132. if err != nil {
  133. if git.IsErrNotExist(err) {
  134. ctx.NotFound("GetBlob", nil)
  135. } else {
  136. ctx.ServerError("GetBlob", err)
  137. }
  138. return
  139. }
  140. if err = ServeBlobOrLFS(ctx, blob); err != nil {
  141. ctx.ServerError("ServeBlob", err)
  142. }
  143. }