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.

attachment.go 4.3KB

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