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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2017 The Gitea 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. "fmt"
  7. "net/http"
  8. "os"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/upload"
  15. )
  16. func renderAttachmentSettings(ctx *context.Context) {
  17. ctx.Data["RequireDropzone"] = true
  18. ctx.Data["IsAttachmentEnabled"] = setting.AttachmentEnabled
  19. ctx.Data["AttachmentAllowedTypes"] = setting.AttachmentAllowedTypes
  20. ctx.Data["AttachmentMaxSize"] = setting.AttachmentMaxSize
  21. ctx.Data["AttachmentMaxFiles"] = setting.AttachmentMaxFiles
  22. }
  23. // UploadAttachment response for uploading issue's attachment
  24. func UploadAttachment(ctx *context.Context) {
  25. if !setting.AttachmentEnabled {
  26. ctx.Error(404, "attachment is not enabled")
  27. return
  28. }
  29. file, header, err := ctx.Req.FormFile("file")
  30. if err != nil {
  31. ctx.Error(500, fmt.Sprintf("FormFile: %v", err))
  32. return
  33. }
  34. defer file.Close()
  35. buf := make([]byte, 1024)
  36. n, _ := file.Read(buf)
  37. if n > 0 {
  38. buf = buf[:n]
  39. }
  40. err = upload.VerifyAllowedContentType(buf, strings.Split(setting.AttachmentAllowedTypes, ","))
  41. if err != nil {
  42. ctx.Error(400, err.Error())
  43. return
  44. }
  45. attach, err := models.NewAttachment(&models.Attachment{
  46. UploaderID: ctx.User.ID,
  47. Name: header.Filename,
  48. }, buf, file)
  49. if err != nil {
  50. ctx.Error(500, fmt.Sprintf("NewAttachment: %v", err))
  51. return
  52. }
  53. log.Trace("New attachment uploaded: %s", attach.UUID)
  54. ctx.JSON(200, 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.Query("file")
  61. attach, err := models.GetAttachmentByUUID(file)
  62. if err != nil {
  63. ctx.Error(400, err.Error())
  64. return
  65. }
  66. if !ctx.IsSigned || (ctx.User.ID != attach.UploaderID) {
  67. ctx.Error(403)
  68. return
  69. }
  70. err = models.DeleteAttachment(attach, true)
  71. if err != nil {
  72. ctx.Error(500, fmt.Sprintf("DeleteAttachment: %v", err))
  73. return
  74. }
  75. ctx.JSON(200, map[string]string{
  76. "uuid": attach.UUID,
  77. })
  78. }
  79. // GetAttachment serve attachements
  80. func GetAttachment(ctx *context.Context) {
  81. attach, err := models.GetAttachmentByUUID(ctx.Params(":uuid"))
  82. if err != nil {
  83. if models.IsErrAttachmentNotExist(err) {
  84. ctx.Error(404)
  85. } else {
  86. ctx.ServerError("GetAttachmentByUUID", err)
  87. }
  88. return
  89. }
  90. repository, unitType, err := attach.LinkedRepository()
  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.User.ID) { //We block if not the uploader
  97. ctx.Error(http.StatusNotFound)
  98. return
  99. }
  100. } else { //If we have the repository we check access
  101. perm, err := models.GetUserRepoPermission(repository, ctx.User)
  102. if err != nil {
  103. ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err.Error())
  104. return
  105. }
  106. if !perm.CanRead(unitType) {
  107. ctx.Error(http.StatusNotFound)
  108. return
  109. }
  110. }
  111. //If we have matched and access to release or issue
  112. fr, err := os.Open(attach.LocalPath())
  113. if err != nil {
  114. ctx.ServerError("Open", err)
  115. return
  116. }
  117. defer fr.Close()
  118. if err := attach.IncreaseDownloadCount(); err != nil {
  119. ctx.ServerError("Update", err)
  120. return
  121. }
  122. if err = ServeData(ctx, attach.Name, fr); err != nil {
  123. ctx.ServerError("ServeData", err)
  124. return
  125. }
  126. }