gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "fmt"
  6. "net/http"
  7. access_model "code.gitea.io/gitea/models/perm/access"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. "code.gitea.io/gitea/modules/httpcache"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/storage"
  13. "code.gitea.io/gitea/modules/util"
  14. "code.gitea.io/gitea/routers/common"
  15. "code.gitea.io/gitea/services/attachment"
  16. "code.gitea.io/gitea/services/context"
  17. "code.gitea.io/gitea/services/context/upload"
  18. repo_service "code.gitea.io/gitea/services/repository"
  19. )
  20. // UploadIssueAttachment response for Issue/PR attachments
  21. func UploadIssueAttachment(ctx *context.Context) {
  22. uploadAttachment(ctx, ctx.Repo.Repository.ID, setting.Attachment.AllowedTypes)
  23. }
  24. // UploadReleaseAttachment response for uploading release attachments
  25. func UploadReleaseAttachment(ctx *context.Context) {
  26. uploadAttachment(ctx, ctx.Repo.Repository.ID, setting.Repository.Release.AllowedTypes)
  27. }
  28. // UploadAttachment response for uploading attachments
  29. func uploadAttachment(ctx *context.Context, repoID int64, allowedTypes string) {
  30. if !setting.Attachment.Enabled {
  31. ctx.HTTPError(http.StatusNotFound, "attachment is not enabled")
  32. return
  33. }
  34. file, header, err := ctx.Req.FormFile("file")
  35. if err != nil {
  36. ctx.HTTPError(http.StatusInternalServerError, fmt.Sprintf("FormFile: %v", err))
  37. return
  38. }
  39. defer file.Close()
  40. attach, err := attachment.UploadAttachment(ctx, file, allowedTypes, header.Size, &repo_model.Attachment{
  41. Name: header.Filename,
  42. UploaderID: ctx.Doer.ID,
  43. RepoID: repoID,
  44. })
  45. if err != nil {
  46. if upload.IsErrFileTypeForbidden(err) {
  47. ctx.HTTPError(http.StatusBadRequest, err.Error())
  48. return
  49. }
  50. ctx.HTTPError(http.StatusInternalServerError, fmt.Sprintf("NewAttachment: %v", err))
  51. return
  52. }
  53. log.Trace("New attachment uploaded: %s", attach.UUID)
  54. ctx.JSON(http.StatusOK, map[string]string{
  55. "uuid": attach.UUID,
  56. })
  57. }
  58. // DeleteAttachment response for deleting issue's attachment
  59. func DeleteAttachment(ctx *context.Context) {
  60. file := ctx.FormString("file")
  61. attach, err := repo_model.GetAttachmentByUUID(ctx, file)
  62. if err != nil {
  63. ctx.HTTPError(http.StatusBadRequest, err.Error())
  64. return
  65. }
  66. if !ctx.IsSigned || (ctx.Doer.ID != attach.UploaderID) {
  67. ctx.HTTPError(http.StatusForbidden)
  68. return
  69. }
  70. err = repo_model.DeleteAttachment(ctx, attach, true)
  71. if err != nil {
  72. ctx.HTTPError(http.StatusInternalServerError, fmt.Sprintf("DeleteAttachment: %v", err))
  73. return
  74. }
  75. ctx.JSON(http.StatusOK, map[string]string{
  76. "uuid": attach.UUID,
  77. })
  78. }
  79. // GetAttachment serve attachments with the given UUID
  80. func ServeAttachment(ctx *context.Context, uuid string) {
  81. attach, err := repo_model.GetAttachmentByUUID(ctx, uuid)
  82. if err != nil {
  83. if repo_model.IsErrAttachmentNotExist(err) {
  84. ctx.HTTPError(http.StatusNotFound)
  85. } else {
  86. ctx.ServerError("GetAttachmentByUUID", err)
  87. }
  88. return
  89. }
  90. repository, unitType, err := repo_service.LinkedRepository(ctx, attach)
  91. if err != nil {
  92. ctx.ServerError("LinkedRepository", err)
  93. return
  94. }
  95. if repository == nil { // If not linked
  96. if !(ctx.IsSigned && attach.UploaderID == ctx.Doer.ID) { // We block if not the uploader
  97. ctx.HTTPError(http.StatusNotFound)
  98. return
  99. }
  100. } else { // If we have the repository we check access
  101. perm, err := access_model.GetUserRepoPermission(ctx, repository, ctx.Doer)
  102. if err != nil {
  103. ctx.HTTPError(http.StatusInternalServerError, "GetUserRepoPermission", err.Error())
  104. return
  105. }
  106. if !perm.CanRead(unitType) {
  107. ctx.HTTPError(http.StatusNotFound)
  108. return
  109. }
  110. }
  111. if err := attach.IncreaseDownloadCount(ctx); err != nil {
  112. ctx.ServerError("IncreaseDownloadCount", err)
  113. return
  114. }
  115. if setting.Attachment.Storage.ServeDirect() {
  116. // If we have a signed url (S3, object storage), redirect to this directly.
  117. u, err := storage.Attachments.URL(attach.RelativePath(), attach.Name, ctx.Req.Method, nil)
  118. if u != nil && err == nil {
  119. ctx.Redirect(u.String())
  120. return
  121. }
  122. }
  123. if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+attach.UUID+`"`) {
  124. return
  125. }
  126. // If we have matched and access to release or issue
  127. fr, err := storage.Attachments.Open(attach.RelativePath())
  128. if err != nil {
  129. ctx.ServerError("Open", err)
  130. return
  131. }
  132. defer fr.Close()
  133. common.ServeContentByReadSeeker(ctx.Base, attach.Name, util.ToPointer(attach.CreatedUnix.AsTime()), fr)
  134. }
  135. // GetAttachment serve attachments
  136. func GetAttachment(ctx *context.Context) {
  137. ServeAttachment(ctx, ctx.PathParam("uuid"))
  138. }