gitea源码

issue_comment_attachment.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. attachment_service "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.APIErrorNotFound("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.APIErrorInternal(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.APIErrorInternal(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_service.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.APIError(http.StatusUnprocessableEntity, err)
  187. } else {
  188. ctx.APIErrorInternal(err)
  189. }
  190. return
  191. }
  192. if err := comment.LoadAttachments(ctx); err != nil {
  193. ctx.APIErrorInternal(err)
  194. return
  195. }
  196. if err = issue_service.UpdateComment(ctx, comment, comment.ContentVersion, ctx.Doer, comment.Content); err != nil {
  197. if errors.Is(err, user_model.ErrBlockedUser) {
  198. ctx.APIError(http.StatusForbidden, err)
  199. } else {
  200. ctx.APIErrorInternal(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. // "422":
  248. // "$ref": "#/responses/validationError"
  249. // "423":
  250. // "$ref": "#/responses/repoArchivedError"
  251. attach := getIssueCommentAttachmentSafeWrite(ctx)
  252. if attach == nil {
  253. return
  254. }
  255. form := web.GetForm(ctx).(*api.EditAttachmentOptions)
  256. if form.Name != "" {
  257. attach.Name = form.Name
  258. }
  259. if err := attachment_service.UpdateAttachment(ctx, setting.Attachment.AllowedTypes, attach); err != nil {
  260. if upload.IsErrFileTypeForbidden(err) {
  261. ctx.APIError(http.StatusUnprocessableEntity, err)
  262. return
  263. }
  264. ctx.APIErrorInternal(err)
  265. return
  266. }
  267. ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
  268. }
  269. // DeleteIssueCommentAttachment delete a given attachment
  270. func DeleteIssueCommentAttachment(ctx *context.APIContext) {
  271. // swagger:operation DELETE /repos/{owner}/{repo}/issues/comments/{id}/assets/{attachment_id} issue issueDeleteIssueCommentAttachment
  272. // ---
  273. // summary: Delete a comment attachment
  274. // produces:
  275. // - application/json
  276. // parameters:
  277. // - name: owner
  278. // in: path
  279. // description: owner of the repo
  280. // type: string
  281. // required: true
  282. // - name: repo
  283. // in: path
  284. // description: name of the repo
  285. // type: string
  286. // required: true
  287. // - name: id
  288. // in: path
  289. // description: id of the comment
  290. // type: integer
  291. // format: int64
  292. // required: true
  293. // - name: attachment_id
  294. // in: path
  295. // description: id of the attachment to delete
  296. // type: integer
  297. // format: int64
  298. // required: true
  299. // responses:
  300. // "204":
  301. // "$ref": "#/responses/empty"
  302. // "404":
  303. // "$ref": "#/responses/error"
  304. // "423":
  305. // "$ref": "#/responses/repoArchivedError"
  306. attach := getIssueCommentAttachmentSafeWrite(ctx)
  307. if attach == nil {
  308. return
  309. }
  310. if err := repo_model.DeleteAttachment(ctx, attach, true); err != nil {
  311. ctx.APIErrorInternal(err)
  312. return
  313. }
  314. ctx.Status(http.StatusNoContent)
  315. }
  316. func getIssueCommentSafe(ctx *context.APIContext) *issues_model.Comment {
  317. comment, err := issues_model.GetCommentByID(ctx, ctx.PathParamInt64("id"))
  318. if err != nil {
  319. ctx.NotFoundOrServerError(err)
  320. return nil
  321. }
  322. if err := comment.LoadIssue(ctx); err != nil {
  323. ctx.APIErrorInternal(err)
  324. return nil
  325. }
  326. if comment.Issue == nil || comment.Issue.RepoID != ctx.Repo.Repository.ID {
  327. ctx.APIError(http.StatusNotFound, "no matching issue comment found")
  328. return nil
  329. }
  330. if !ctx.Repo.CanReadIssuesOrPulls(comment.Issue.IsPull) {
  331. return nil
  332. }
  333. comment.Issue.Repo = ctx.Repo.Repository
  334. return comment
  335. }
  336. func getIssueCommentAttachmentSafeWrite(ctx *context.APIContext) *repo_model.Attachment {
  337. comment := getIssueCommentSafe(ctx)
  338. if comment == nil {
  339. return nil
  340. }
  341. if !canUserWriteIssueCommentAttachment(ctx, comment) {
  342. return nil
  343. }
  344. return getIssueCommentAttachmentSafeRead(ctx, comment)
  345. }
  346. func canUserWriteIssueCommentAttachment(ctx *context.APIContext, comment *issues_model.Comment) bool {
  347. canEditComment := ctx.IsSigned && (ctx.Doer.ID == comment.PosterID || ctx.IsUserRepoAdmin() || ctx.IsUserSiteAdmin()) && ctx.Repo.CanWriteIssuesOrPulls(comment.Issue.IsPull)
  348. if !canEditComment {
  349. ctx.APIError(http.StatusForbidden, "user should have permission to edit comment")
  350. return false
  351. }
  352. return true
  353. }
  354. func getIssueCommentAttachmentSafeRead(ctx *context.APIContext, comment *issues_model.Comment) *repo_model.Attachment {
  355. attachment, err := repo_model.GetAttachmentByID(ctx, ctx.PathParamInt64("attachment_id"))
  356. if err != nil {
  357. ctx.NotFoundOrServerError(err)
  358. return nil
  359. }
  360. if !attachmentBelongsToRepoOrComment(ctx, attachment, comment) {
  361. return nil
  362. }
  363. return attachment
  364. }
  365. func attachmentBelongsToRepoOrComment(ctx *context.APIContext, attachment *repo_model.Attachment, comment *issues_model.Comment) bool {
  366. if attachment.RepoID != ctx.Repo.Repository.ID {
  367. log.Debug("Requested attachment[%d] does not belong to repo[%-v].", attachment.ID, ctx.Repo.Repository)
  368. ctx.APIErrorNotFound("no such attachment in repo")
  369. return false
  370. }
  371. if attachment.IssueID == 0 || attachment.CommentID == 0 {
  372. log.Debug("Requested attachment[%d] is not in a comment.", attachment.ID)
  373. ctx.APIErrorNotFound("no such attachment in comment")
  374. return false
  375. }
  376. if comment != nil && attachment.CommentID != comment.ID {
  377. log.Debug("Requested attachment[%d] does not belong to comment[%d].", attachment.ID, comment.ID)
  378. ctx.APIErrorNotFound("no such attachment in comment")
  379. return false
  380. }
  381. return true
  382. }