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.

issue_comment_attachment.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "errors"
  6. "net/http"
  7. issues_model "code.gitea.io/gitea/models/issues"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/web"
  14. "code.gitea.io/gitea/services/attachment"
  15. "code.gitea.io/gitea/services/context"
  16. "code.gitea.io/gitea/services/context/upload"
  17. "code.gitea.io/gitea/services/convert"
  18. issue_service "code.gitea.io/gitea/services/issue"
  19. )
  20. // GetIssueCommentAttachment gets a single attachment of the comment
  21. func GetIssueCommentAttachment(ctx *context.APIContext) {
  22. // swagger:operation GET /repos/{owner}/{repo}/issues/comments/{id}/assets/{attachment_id} issue issueGetIssueCommentAttachment
  23. // ---
  24. // summary: Get a comment attachment
  25. // produces:
  26. // - application/json
  27. // parameters:
  28. // - name: owner
  29. // in: path
  30. // description: owner of the repo
  31. // type: string
  32. // required: true
  33. // - name: repo
  34. // in: path
  35. // description: name of the repo
  36. // type: string
  37. // required: true
  38. // - name: id
  39. // in: path
  40. // description: id of the comment
  41. // type: integer
  42. // format: int64
  43. // required: true
  44. // - name: attachment_id
  45. // in: path
  46. // description: id of the attachment to get
  47. // type: integer
  48. // format: int64
  49. // required: true
  50. // responses:
  51. // "200":
  52. // "$ref": "#/responses/Attachment"
  53. // "404":
  54. // "$ref": "#/responses/error"
  55. comment := getIssueCommentSafe(ctx)
  56. if comment == nil {
  57. return
  58. }
  59. attachment := getIssueCommentAttachmentSafeRead(ctx, comment)
  60. if attachment == nil {
  61. return
  62. }
  63. if attachment.CommentID != comment.ID {
  64. log.Debug("User requested attachment[%d] is not in comment[%d].", attachment.ID, comment.ID)
  65. ctx.NotFound("attachment not in comment")
  66. return
  67. }
  68. ctx.JSON(http.StatusOK, convert.ToAPIAttachment(ctx.Repo.Repository, attachment))
  69. }
  70. // ListIssueCommentAttachments lists all attachments of the comment
  71. func ListIssueCommentAttachments(ctx *context.APIContext) {
  72. // swagger:operation GET /repos/{owner}/{repo}/issues/comments/{id}/assets issue issueListIssueCommentAttachments
  73. // ---
  74. // summary: List comment's attachments
  75. // produces:
  76. // - application/json
  77. // parameters:
  78. // - name: owner
  79. // in: path
  80. // description: owner of the repo
  81. // type: string
  82. // required: true
  83. // - name: repo
  84. // in: path
  85. // description: name of the repo
  86. // type: string
  87. // required: true
  88. // - name: id
  89. // in: path
  90. // description: id of the comment
  91. // type: integer
  92. // format: int64
  93. // required: true
  94. // responses:
  95. // "200":
  96. // "$ref": "#/responses/AttachmentList"
  97. // "404":
  98. // "$ref": "#/responses/error"
  99. comment := getIssueCommentSafe(ctx)
  100. if comment == nil {
  101. return
  102. }
  103. if err := comment.LoadAttachments(ctx); err != nil {
  104. ctx.Error(http.StatusInternalServerError, "LoadAttachments", err)
  105. return
  106. }
  107. ctx.JSON(http.StatusOK, convert.ToAPIAttachments(ctx.Repo.Repository, comment.Attachments))
  108. }
  109. // CreateIssueCommentAttachment creates an attachment and saves the given file
  110. func CreateIssueCommentAttachment(ctx *context.APIContext) {
  111. // swagger:operation POST /repos/{owner}/{repo}/issues/comments/{id}/assets issue issueCreateIssueCommentAttachment
  112. // ---
  113. // summary: Create a comment attachment
  114. // produces:
  115. // - application/json
  116. // consumes:
  117. // - multipart/form-data
  118. // parameters:
  119. // - name: owner
  120. // in: path
  121. // description: owner of the repo
  122. // type: string
  123. // required: true
  124. // - name: repo
  125. // in: path
  126. // description: name of the repo
  127. // type: string
  128. // required: true
  129. // - name: id
  130. // in: path
  131. // description: id of the comment
  132. // type: integer
  133. // format: int64
  134. // required: true
  135. // - name: name
  136. // in: query
  137. // description: name of the attachment
  138. // type: string
  139. // required: false
  140. // - name: attachment
  141. // in: formData
  142. // description: attachment to upload
  143. // type: file
  144. // required: true
  145. // responses:
  146. // "201":
  147. // "$ref": "#/responses/Attachment"
  148. // "400":
  149. // "$ref": "#/responses/error"
  150. // "403":
  151. // "$ref": "#/responses/forbidden"
  152. // "404":
  153. // "$ref": "#/responses/error"
  154. // "422":
  155. // "$ref": "#/responses/validationError"
  156. // "423":
  157. // "$ref": "#/responses/repoArchivedError"
  158. // Check if comment exists and load comment
  159. comment := getIssueCommentSafe(ctx)
  160. if comment == nil {
  161. return
  162. }
  163. if !canUserWriteIssueCommentAttachment(ctx, comment) {
  164. return
  165. }
  166. // Get uploaded file from request
  167. file, header, err := ctx.Req.FormFile("attachment")
  168. if err != nil {
  169. ctx.Error(http.StatusInternalServerError, "FormFile", err)
  170. return
  171. }
  172. defer file.Close()
  173. filename := header.Filename
  174. if query := ctx.FormString("name"); query != "" {
  175. filename = query
  176. }
  177. attachment, err := attachment.UploadAttachment(ctx, file, setting.Attachment.AllowedTypes, header.Size, &repo_model.Attachment{
  178. Name: filename,
  179. UploaderID: ctx.Doer.ID,
  180. RepoID: ctx.Repo.Repository.ID,
  181. IssueID: comment.IssueID,
  182. CommentID: comment.ID,
  183. })
  184. if err != nil {
  185. if upload.IsErrFileTypeForbidden(err) {
  186. ctx.Error(http.StatusUnprocessableEntity, "", err)
  187. } else {
  188. ctx.Error(http.StatusInternalServerError, "UploadAttachment", err)
  189. }
  190. return
  191. }
  192. if err := comment.LoadAttachments(ctx); err != nil {
  193. ctx.Error(http.StatusInternalServerError, "LoadAttachments", err)
  194. return
  195. }
  196. if err = issue_service.UpdateComment(ctx, comment, ctx.Doer, comment.Content); err != nil {
  197. if errors.Is(err, user_model.ErrBlockedUser) {
  198. ctx.Error(http.StatusForbidden, "UpdateComment", err)
  199. } else {
  200. ctx.ServerError("UpdateComment", err)
  201. }
  202. return
  203. }
  204. ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attachment))
  205. }
  206. // EditIssueCommentAttachment updates the given attachment
  207. func EditIssueCommentAttachment(ctx *context.APIContext) {
  208. // swagger:operation PATCH /repos/{owner}/{repo}/issues/comments/{id}/assets/{attachment_id} issue issueEditIssueCommentAttachment
  209. // ---
  210. // summary: Edit a comment attachment
  211. // produces:
  212. // - application/json
  213. // consumes:
  214. // - application/json
  215. // parameters:
  216. // - name: owner
  217. // in: path
  218. // description: owner of the repo
  219. // type: string
  220. // required: true
  221. // - name: repo
  222. // in: path
  223. // description: name of the repo
  224. // type: string
  225. // required: true
  226. // - name: id
  227. // in: path
  228. // description: id of the comment
  229. // type: integer
  230. // format: int64
  231. // required: true
  232. // - name: attachment_id
  233. // in: path
  234. // description: id of the attachment to edit
  235. // type: integer
  236. // format: int64
  237. // required: true
  238. // - name: body
  239. // in: body
  240. // schema:
  241. // "$ref": "#/definitions/EditAttachmentOptions"
  242. // responses:
  243. // "201":
  244. // "$ref": "#/responses/Attachment"
  245. // "404":
  246. // "$ref": "#/responses/error"
  247. // "423":
  248. // "$ref": "#/responses/repoArchivedError"
  249. attach := getIssueCommentAttachmentSafeWrite(ctx)
  250. if attach == nil {
  251. return
  252. }
  253. form := web.GetForm(ctx).(*api.EditAttachmentOptions)
  254. if form.Name != "" {
  255. attach.Name = form.Name
  256. }
  257. if err := repo_model.UpdateAttachment(ctx, attach); err != nil {
  258. ctx.Error(http.StatusInternalServerError, "UpdateAttachment", attach)
  259. }
  260. ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
  261. }
  262. // DeleteIssueCommentAttachment delete a given attachment
  263. func DeleteIssueCommentAttachment(ctx *context.APIContext) {
  264. // swagger:operation DELETE /repos/{owner}/{repo}/issues/comments/{id}/assets/{attachment_id} issue issueDeleteIssueCommentAttachment
  265. // ---
  266. // summary: Delete a comment attachment
  267. // produces:
  268. // - application/json
  269. // parameters:
  270. // - name: owner
  271. // in: path
  272. // description: owner of the repo
  273. // type: string
  274. // required: true
  275. // - name: repo
  276. // in: path
  277. // description: name of the repo
  278. // type: string
  279. // required: true
  280. // - name: id
  281. // in: path
  282. // description: id of the comment
  283. // type: integer
  284. // format: int64
  285. // required: true
  286. // - name: attachment_id
  287. // in: path
  288. // description: id of the attachment to delete
  289. // type: integer
  290. // format: int64
  291. // required: true
  292. // responses:
  293. // "204":
  294. // "$ref": "#/responses/empty"
  295. // "404":
  296. // "$ref": "#/responses/error"
  297. // "423":
  298. // "$ref": "#/responses/repoArchivedError"
  299. attach := getIssueCommentAttachmentSafeWrite(ctx)
  300. if attach == nil {
  301. return
  302. }
  303. if err := repo_model.DeleteAttachment(ctx, attach, true); err != nil {
  304. ctx.Error(http.StatusInternalServerError, "DeleteAttachment", err)
  305. return
  306. }
  307. ctx.Status(http.StatusNoContent)
  308. }
  309. func getIssueCommentSafe(ctx *context.APIContext) *issues_model.Comment {
  310. comment, err := issues_model.GetCommentByID(ctx, ctx.ParamsInt64("id"))
  311. if err != nil {
  312. ctx.NotFoundOrServerError("GetCommentByID", issues_model.IsErrCommentNotExist, err)
  313. return nil
  314. }
  315. if err := comment.LoadIssue(ctx); err != nil {
  316. ctx.Error(http.StatusInternalServerError, "comment.LoadIssue", err)
  317. return nil
  318. }
  319. if comment.Issue == nil || comment.Issue.RepoID != ctx.Repo.Repository.ID {
  320. ctx.Error(http.StatusNotFound, "", "no matching issue comment found")
  321. return nil
  322. }
  323. if !ctx.Repo.CanReadIssuesOrPulls(comment.Issue.IsPull) {
  324. return nil
  325. }
  326. comment.Issue.Repo = ctx.Repo.Repository
  327. return comment
  328. }
  329. func getIssueCommentAttachmentSafeWrite(ctx *context.APIContext) *repo_model.Attachment {
  330. comment := getIssueCommentSafe(ctx)
  331. if comment == nil {
  332. return nil
  333. }
  334. if !canUserWriteIssueCommentAttachment(ctx, comment) {
  335. return nil
  336. }
  337. return getIssueCommentAttachmentSafeRead(ctx, comment)
  338. }
  339. func canUserWriteIssueCommentAttachment(ctx *context.APIContext, comment *issues_model.Comment) bool {
  340. canEditComment := ctx.IsSigned && (ctx.Doer.ID == comment.PosterID || ctx.IsUserRepoAdmin() || ctx.IsUserSiteAdmin()) && ctx.Repo.CanWriteIssuesOrPulls(comment.Issue.IsPull)
  341. if !canEditComment {
  342. ctx.Error(http.StatusForbidden, "", "user should have permission to edit comment")
  343. return false
  344. }
  345. return true
  346. }
  347. func getIssueCommentAttachmentSafeRead(ctx *context.APIContext, comment *issues_model.Comment) *repo_model.Attachment {
  348. attachment, err := repo_model.GetAttachmentByID(ctx, ctx.ParamsInt64("attachment_id"))
  349. if err != nil {
  350. ctx.NotFoundOrServerError("GetAttachmentByID", repo_model.IsErrAttachmentNotExist, err)
  351. return nil
  352. }
  353. if !attachmentBelongsToRepoOrComment(ctx, attachment, comment) {
  354. return nil
  355. }
  356. return attachment
  357. }
  358. func attachmentBelongsToRepoOrComment(ctx *context.APIContext, attachment *repo_model.Attachment, comment *issues_model.Comment) bool {
  359. if attachment.RepoID != ctx.Repo.Repository.ID {
  360. log.Debug("Requested attachment[%d] does not belong to repo[%-v].", attachment.ID, ctx.Repo.Repository)
  361. ctx.NotFound("no such attachment in repo")
  362. return false
  363. }
  364. if attachment.IssueID == 0 || attachment.CommentID == 0 {
  365. log.Debug("Requested attachment[%d] is not in a comment.", attachment.ID)
  366. ctx.NotFound("no such attachment in comment")
  367. return false
  368. }
  369. if comment != nil && attachment.CommentID != comment.ID {
  370. log.Debug("Requested attachment[%d] does not belong to comment[%d].", attachment.ID, comment.ID)
  371. ctx.NotFound("no such attachment in comment")
  372. return false
  373. }
  374. return true
  375. }