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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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["IsAttachmentEnabled"] = setting.AttachmentEnabled
  18. ctx.Data["AttachmentAllowedTypes"] = setting.AttachmentAllowedTypes
  19. ctx.Data["AttachmentMaxSize"] = setting.AttachmentMaxSize
  20. ctx.Data["AttachmentMaxFiles"] = setting.AttachmentMaxFiles
  21. }
  22. // UploadAttachment response for uploading issue's attachment
  23. func UploadAttachment(ctx *context.Context) {
  24. if !setting.AttachmentEnabled {
  25. ctx.Error(404, "attachment is not enabled")
  26. return
  27. }
  28. file, header, err := ctx.Req.FormFile("file")
  29. if err != nil {
  30. ctx.Error(500, fmt.Sprintf("FormFile: %v", err))
  31. return
  32. }
  33. defer file.Close()
  34. buf := make([]byte, 1024)
  35. n, _ := file.Read(buf)
  36. if n > 0 {
  37. buf = buf[:n]
  38. }
  39. err = upload.VerifyAllowedContentType(buf, strings.Split(setting.AttachmentAllowedTypes, ","))
  40. if err != nil {
  41. ctx.Error(400, err.Error())
  42. return
  43. }
  44. attach, err := models.NewAttachment(&models.Attachment{
  45. UploaderID: ctx.User.ID,
  46. Name: header.Filename,
  47. }, buf, file)
  48. if err != nil {
  49. ctx.Error(500, fmt.Sprintf("NewAttachment: %v", err))
  50. return
  51. }
  52. log.Trace("New attachment uploaded: %s", attach.UUID)
  53. ctx.JSON(200, 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.Query("file")
  60. attach, err := models.GetAttachmentByUUID(file)
  61. if err != nil {
  62. ctx.Error(400, err.Error())
  63. return
  64. }
  65. if !ctx.IsSigned || (ctx.User.ID != attach.UploaderID) {
  66. ctx.Error(403)
  67. return
  68. }
  69. err = models.DeleteAttachment(attach, true)
  70. if err != nil {
  71. ctx.Error(500, fmt.Sprintf("DeleteAttachment: %v", err))
  72. return
  73. }
  74. ctx.JSON(200, map[string]string{
  75. "uuid": attach.UUID,
  76. })
  77. }
  78. // GetAttachment serve attachements
  79. func GetAttachment(ctx *context.Context) {
  80. attach, err := models.GetAttachmentByUUID(ctx.Params(":uuid"))
  81. if err != nil {
  82. if models.IsErrAttachmentNotExist(err) {
  83. ctx.Error(404)
  84. } else {
  85. ctx.ServerError("GetAttachmentByUUID", err)
  86. }
  87. return
  88. }
  89. repository, unitType, err := attach.LinkedRepository()
  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.User.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 := models.GetUserRepoPermission(repository, ctx.User)
  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 we have matched and access to release or issue
  111. fr, err := os.Open(attach.LocalPath())
  112. if err != nil {
  113. ctx.ServerError("Open", err)
  114. return
  115. }
  116. defer fr.Close()
  117. if err := attach.IncreaseDownloadCount(); err != nil {
  118. ctx.ServerError("Update", err)
  119. return
  120. }
  121. if err = ServeData(ctx, attach.Name, fr); err != nil {
  122. ctx.ServerError("ServeData", err)
  123. return
  124. }
  125. }