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_attachment.go 9.7KB

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