Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

issue_attachment.go 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/web"
  12. "code.gitea.io/gitea/services/attachment"
  13. "code.gitea.io/gitea/services/context"
  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. // "423":
  145. // "$ref": "#/responses/repoArchivedError"
  146. issue := getIssueFromContext(ctx)
  147. if issue == nil {
  148. return
  149. }
  150. if !canUserWriteIssueAttachment(ctx, issue) {
  151. return
  152. }
  153. // Get uploaded file from request
  154. file, header, err := ctx.Req.FormFile("attachment")
  155. if err != nil {
  156. ctx.Error(http.StatusInternalServerError, "FormFile", err)
  157. return
  158. }
  159. defer file.Close()
  160. filename := header.Filename
  161. if query := ctx.FormString("name"); query != "" {
  162. filename = query
  163. }
  164. attachment, err := attachment.UploadAttachment(ctx, file, setting.Attachment.AllowedTypes, header.Size, &repo_model.Attachment{
  165. Name: filename,
  166. UploaderID: ctx.Doer.ID,
  167. RepoID: ctx.Repo.Repository.ID,
  168. IssueID: issue.ID,
  169. })
  170. if err != nil {
  171. ctx.Error(http.StatusInternalServerError, "UploadAttachment", err)
  172. return
  173. }
  174. issue.Attachments = append(issue.Attachments, attachment)
  175. if err := issue_service.ChangeContent(ctx, issue, ctx.Doer, issue.Content); err != nil {
  176. ctx.Error(http.StatusInternalServerError, "ChangeContent", err)
  177. return
  178. }
  179. ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attachment))
  180. }
  181. // EditIssueAttachment updates the given attachment
  182. func EditIssueAttachment(ctx *context.APIContext) {
  183. // swagger:operation PATCH /repos/{owner}/{repo}/issues/{index}/assets/{attachment_id} issue issueEditIssueAttachment
  184. // ---
  185. // summary: Edit an issue attachment
  186. // produces:
  187. // - application/json
  188. // consumes:
  189. // - application/json
  190. // parameters:
  191. // - name: owner
  192. // in: path
  193. // description: owner of the repo
  194. // type: string
  195. // required: true
  196. // - name: repo
  197. // in: path
  198. // description: name of the repo
  199. // type: string
  200. // required: true
  201. // - name: index
  202. // in: path
  203. // description: index of the issue
  204. // type: integer
  205. // format: int64
  206. // required: true
  207. // - name: attachment_id
  208. // in: path
  209. // description: id of the attachment to edit
  210. // type: integer
  211. // format: int64
  212. // required: true
  213. // - name: body
  214. // in: body
  215. // schema:
  216. // "$ref": "#/definitions/EditAttachmentOptions"
  217. // responses:
  218. // "201":
  219. // "$ref": "#/responses/Attachment"
  220. // "404":
  221. // "$ref": "#/responses/error"
  222. // "423":
  223. // "$ref": "#/responses/repoArchivedError"
  224. attachment := getIssueAttachmentSafeWrite(ctx)
  225. if attachment == nil {
  226. return
  227. }
  228. // do changes to attachment. only meaningful change is name.
  229. form := web.GetForm(ctx).(*api.EditAttachmentOptions)
  230. if form.Name != "" {
  231. attachment.Name = form.Name
  232. }
  233. if err := repo_model.UpdateAttachment(ctx, attachment); err != nil {
  234. ctx.Error(http.StatusInternalServerError, "UpdateAttachment", err)
  235. }
  236. ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attachment))
  237. }
  238. // DeleteIssueAttachment delete a given attachment
  239. func DeleteIssueAttachment(ctx *context.APIContext) {
  240. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/assets/{attachment_id} issue issueDeleteIssueAttachment
  241. // ---
  242. // summary: Delete an issue attachment
  243. // produces:
  244. // - application/json
  245. // parameters:
  246. // - name: owner
  247. // in: path
  248. // description: owner of the repo
  249. // type: string
  250. // required: true
  251. // - name: repo
  252. // in: path
  253. // description: name of the repo
  254. // type: string
  255. // required: true
  256. // - name: index
  257. // in: path
  258. // description: index of the issue
  259. // type: integer
  260. // format: int64
  261. // required: true
  262. // - name: attachment_id
  263. // in: path
  264. // description: id of the attachment to delete
  265. // type: integer
  266. // format: int64
  267. // required: true
  268. // responses:
  269. // "204":
  270. // "$ref": "#/responses/empty"
  271. // "404":
  272. // "$ref": "#/responses/error"
  273. // "423":
  274. // "$ref": "#/responses/repoArchivedError"
  275. attachment := getIssueAttachmentSafeWrite(ctx)
  276. if attachment == nil {
  277. return
  278. }
  279. if err := repo_model.DeleteAttachment(ctx, attachment, true); err != nil {
  280. ctx.Error(http.StatusInternalServerError, "DeleteAttachment", err)
  281. return
  282. }
  283. ctx.Status(http.StatusNoContent)
  284. }
  285. func getIssueFromContext(ctx *context.APIContext) *issues_model.Issue {
  286. issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64("index"))
  287. if err != nil {
  288. ctx.NotFoundOrServerError("GetIssueByIndex", issues_model.IsErrIssueNotExist, err)
  289. return nil
  290. }
  291. issue.Repo = ctx.Repo.Repository
  292. return issue
  293. }
  294. func getIssueAttachmentSafeWrite(ctx *context.APIContext) *repo_model.Attachment {
  295. issue := getIssueFromContext(ctx)
  296. if issue == nil {
  297. return nil
  298. }
  299. if !canUserWriteIssueAttachment(ctx, issue) {
  300. return nil
  301. }
  302. return getIssueAttachmentSafeRead(ctx, issue)
  303. }
  304. func getIssueAttachmentSafeRead(ctx *context.APIContext, issue *issues_model.Issue) *repo_model.Attachment {
  305. attachment, err := repo_model.GetAttachmentByID(ctx, ctx.ParamsInt64("attachment_id"))
  306. if err != nil {
  307. ctx.NotFoundOrServerError("GetAttachmentByID", repo_model.IsErrAttachmentNotExist, err)
  308. return nil
  309. }
  310. if !attachmentBelongsToRepoOrIssue(ctx, attachment, issue) {
  311. return nil
  312. }
  313. return attachment
  314. }
  315. func canUserWriteIssueAttachment(ctx *context.APIContext, issue *issues_model.Issue) bool {
  316. canEditIssue := ctx.IsSigned && (ctx.Doer.ID == issue.PosterID || ctx.IsUserRepoAdmin() || ctx.IsUserSiteAdmin() || ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull))
  317. if !canEditIssue {
  318. ctx.Error(http.StatusForbidden, "", "user should have permission to write issue")
  319. return false
  320. }
  321. return true
  322. }
  323. func attachmentBelongsToRepoOrIssue(ctx *context.APIContext, attachment *repo_model.Attachment, issue *issues_model.Issue) bool {
  324. if attachment.RepoID != ctx.Repo.Repository.ID {
  325. log.Debug("Requested attachment[%d] does not belong to repo[%-v].", attachment.ID, ctx.Repo.Repository)
  326. ctx.NotFound("no such attachment in repo")
  327. return false
  328. }
  329. if attachment.IssueID == 0 {
  330. log.Debug("Requested attachment[%d] is not in an issue.", attachment.ID)
  331. ctx.NotFound("no such attachment in issue")
  332. return false
  333. } else if issue != nil && attachment.IssueID != issue.ID {
  334. log.Debug("Requested attachment[%d] does not belong to issue[%d, #%d].", attachment.ID, issue.ID, issue.Index)
  335. ctx.NotFound("no such attachment in issue")
  336. return false
  337. }
  338. return true
  339. }