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.

release_attachment.go 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "net/http"
  6. repo_model "code.gitea.io/gitea/models/repo"
  7. "code.gitea.io/gitea/modules/context"
  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/upload"
  12. "code.gitea.io/gitea/modules/web"
  13. "code.gitea.io/gitea/services/attachment"
  14. "code.gitea.io/gitea/services/convert"
  15. )
  16. func checkReleaseMatchRepo(ctx *context.APIContext, releaseID int64) bool {
  17. release, err := repo_model.GetReleaseByID(ctx, releaseID)
  18. if err != nil {
  19. if repo_model.IsErrReleaseNotExist(err) {
  20. ctx.NotFound()
  21. return false
  22. }
  23. ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
  24. return false
  25. }
  26. if release.RepoID != ctx.Repo.Repository.ID {
  27. ctx.NotFound()
  28. return false
  29. }
  30. return true
  31. }
  32. // GetReleaseAttachment gets a single attachment of the release
  33. func GetReleaseAttachment(ctx *context.APIContext) {
  34. // swagger:operation GET /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoGetReleaseAttachment
  35. // ---
  36. // summary: Get a release attachment
  37. // produces:
  38. // - application/json
  39. // parameters:
  40. // - name: owner
  41. // in: path
  42. // description: owner of the repo
  43. // type: string
  44. // required: true
  45. // - name: repo
  46. // in: path
  47. // description: name of the repo
  48. // type: string
  49. // required: true
  50. // - name: id
  51. // in: path
  52. // description: id of the release
  53. // type: integer
  54. // format: int64
  55. // required: true
  56. // - name: attachment_id
  57. // in: path
  58. // description: id of the attachment to get
  59. // type: integer
  60. // format: int64
  61. // required: true
  62. // responses:
  63. // "200":
  64. // "$ref": "#/responses/Attachment"
  65. // "404":
  66. // "$ref": "#/responses/notFound"
  67. releaseID := ctx.ParamsInt64(":id")
  68. if !checkReleaseMatchRepo(ctx, releaseID) {
  69. return
  70. }
  71. attachID := ctx.ParamsInt64(":attachment_id")
  72. attach, err := repo_model.GetAttachmentByID(ctx, attachID)
  73. if err != nil {
  74. if repo_model.IsErrAttachmentNotExist(err) {
  75. ctx.NotFound()
  76. return
  77. }
  78. ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err)
  79. return
  80. }
  81. if attach.ReleaseID != releaseID {
  82. log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
  83. ctx.NotFound()
  84. return
  85. }
  86. // FIXME Should prove the existence of the given repo, but results in unnecessary database requests
  87. ctx.JSON(http.StatusOK, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
  88. }
  89. // ListReleaseAttachments lists all attachments of the release
  90. func ListReleaseAttachments(ctx *context.APIContext) {
  91. // swagger:operation GET /repos/{owner}/{repo}/releases/{id}/assets repository repoListReleaseAttachments
  92. // ---
  93. // summary: List release's attachments
  94. // produces:
  95. // - application/json
  96. // parameters:
  97. // - name: owner
  98. // in: path
  99. // description: owner of the repo
  100. // type: string
  101. // required: true
  102. // - name: repo
  103. // in: path
  104. // description: name of the repo
  105. // type: string
  106. // required: true
  107. // - name: id
  108. // in: path
  109. // description: id of the release
  110. // type: integer
  111. // format: int64
  112. // required: true
  113. // responses:
  114. // "200":
  115. // "$ref": "#/responses/AttachmentList"
  116. // "404":
  117. // "$ref": "#/responses/notFound"
  118. releaseID := ctx.ParamsInt64(":id")
  119. release, err := repo_model.GetReleaseByID(ctx, releaseID)
  120. if err != nil {
  121. if repo_model.IsErrReleaseNotExist(err) {
  122. ctx.NotFound()
  123. return
  124. }
  125. ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
  126. return
  127. }
  128. if release.RepoID != ctx.Repo.Repository.ID {
  129. ctx.NotFound()
  130. return
  131. }
  132. if err := release.LoadAttributes(ctx); err != nil {
  133. ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
  134. return
  135. }
  136. ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, release).Attachments)
  137. }
  138. // CreateReleaseAttachment creates an attachment and saves the given file
  139. func CreateReleaseAttachment(ctx *context.APIContext) {
  140. // swagger:operation POST /repos/{owner}/{repo}/releases/{id}/assets repository repoCreateReleaseAttachment
  141. // ---
  142. // summary: Create a release attachment
  143. // produces:
  144. // - application/json
  145. // consumes:
  146. // - multipart/form-data
  147. // parameters:
  148. // - name: owner
  149. // in: path
  150. // description: owner of the repo
  151. // type: string
  152. // required: true
  153. // - name: repo
  154. // in: path
  155. // description: name of the repo
  156. // type: string
  157. // required: true
  158. // - name: id
  159. // in: path
  160. // description: id of the release
  161. // type: integer
  162. // format: int64
  163. // required: true
  164. // - name: name
  165. // in: query
  166. // description: name of the attachment
  167. // type: string
  168. // required: false
  169. // - name: attachment
  170. // in: formData
  171. // description: attachment to upload
  172. // type: file
  173. // required: true
  174. // responses:
  175. // "201":
  176. // "$ref": "#/responses/Attachment"
  177. // "400":
  178. // "$ref": "#/responses/error"
  179. // "404":
  180. // "$ref": "#/responses/notFound"
  181. // Check if attachments are enabled
  182. if !setting.Attachment.Enabled {
  183. ctx.NotFound("Attachment is not enabled")
  184. return
  185. }
  186. // Check if release exists an load release
  187. releaseID := ctx.ParamsInt64(":id")
  188. if !checkReleaseMatchRepo(ctx, releaseID) {
  189. return
  190. }
  191. // Get uploaded file from request
  192. file, header, err := ctx.Req.FormFile("attachment")
  193. if err != nil {
  194. ctx.Error(http.StatusInternalServerError, "GetFile", err)
  195. return
  196. }
  197. defer file.Close()
  198. filename := header.Filename
  199. if query := ctx.FormString("name"); query != "" {
  200. filename = query
  201. }
  202. // Create a new attachment and save the file
  203. attach, err := attachment.UploadAttachment(file, setting.Repository.Release.AllowedTypes, header.Size, &repo_model.Attachment{
  204. Name: filename,
  205. UploaderID: ctx.Doer.ID,
  206. RepoID: ctx.Repo.Repository.ID,
  207. ReleaseID: releaseID,
  208. })
  209. if err != nil {
  210. if upload.IsErrFileTypeForbidden(err) {
  211. ctx.Error(http.StatusBadRequest, "DetectContentType", err)
  212. return
  213. }
  214. ctx.Error(http.StatusInternalServerError, "NewAttachment", err)
  215. return
  216. }
  217. ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
  218. }
  219. // EditReleaseAttachment updates the given attachment
  220. func EditReleaseAttachment(ctx *context.APIContext) {
  221. // swagger:operation PATCH /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoEditReleaseAttachment
  222. // ---
  223. // summary: Edit a release attachment
  224. // produces:
  225. // - application/json
  226. // consumes:
  227. // - application/json
  228. // parameters:
  229. // - name: owner
  230. // in: path
  231. // description: owner of the repo
  232. // type: string
  233. // required: true
  234. // - name: repo
  235. // in: path
  236. // description: name of the repo
  237. // type: string
  238. // required: true
  239. // - name: id
  240. // in: path
  241. // description: id of the release
  242. // type: integer
  243. // format: int64
  244. // required: true
  245. // - name: attachment_id
  246. // in: path
  247. // description: id of the attachment to edit
  248. // type: integer
  249. // format: int64
  250. // required: true
  251. // - name: body
  252. // in: body
  253. // schema:
  254. // "$ref": "#/definitions/EditAttachmentOptions"
  255. // responses:
  256. // "201":
  257. // "$ref": "#/responses/Attachment"
  258. // "404":
  259. // "$ref": "#/responses/notFound"
  260. form := web.GetForm(ctx).(*api.EditAttachmentOptions)
  261. // Check if release exists an load release
  262. releaseID := ctx.ParamsInt64(":id")
  263. if !checkReleaseMatchRepo(ctx, releaseID) {
  264. return
  265. }
  266. attachID := ctx.ParamsInt64(":attachment_id")
  267. attach, err := repo_model.GetAttachmentByID(ctx, attachID)
  268. if err != nil {
  269. if repo_model.IsErrAttachmentNotExist(err) {
  270. ctx.NotFound()
  271. return
  272. }
  273. ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err)
  274. return
  275. }
  276. if attach.ReleaseID != releaseID {
  277. log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
  278. ctx.NotFound()
  279. return
  280. }
  281. // FIXME Should prove the existence of the given repo, but results in unnecessary database requests
  282. if form.Name != "" {
  283. attach.Name = form.Name
  284. }
  285. if err := repo_model.UpdateAttachment(ctx, attach); err != nil {
  286. ctx.Error(http.StatusInternalServerError, "UpdateAttachment", attach)
  287. }
  288. ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
  289. }
  290. // DeleteReleaseAttachment delete a given attachment
  291. func DeleteReleaseAttachment(ctx *context.APIContext) {
  292. // swagger:operation DELETE /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoDeleteReleaseAttachment
  293. // ---
  294. // summary: Delete a release attachment
  295. // produces:
  296. // - application/json
  297. // parameters:
  298. // - name: owner
  299. // in: path
  300. // description: owner of the repo
  301. // type: string
  302. // required: true
  303. // - name: repo
  304. // in: path
  305. // description: name of the repo
  306. // type: string
  307. // required: true
  308. // - name: id
  309. // in: path
  310. // description: id of the release
  311. // type: integer
  312. // format: int64
  313. // required: true
  314. // - name: attachment_id
  315. // in: path
  316. // description: id of the attachment to delete
  317. // type: integer
  318. // format: int64
  319. // required: true
  320. // responses:
  321. // "204":
  322. // "$ref": "#/responses/empty"
  323. // "404":
  324. // "$ref": "#/responses/notFound"
  325. // Check if release exists an load release
  326. releaseID := ctx.ParamsInt64(":id")
  327. if !checkReleaseMatchRepo(ctx, releaseID) {
  328. return
  329. }
  330. attachID := ctx.ParamsInt64(":attachment_id")
  331. attach, err := repo_model.GetAttachmentByID(ctx, attachID)
  332. if err != nil {
  333. if repo_model.IsErrAttachmentNotExist(err) {
  334. ctx.NotFound()
  335. return
  336. }
  337. ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err)
  338. return
  339. }
  340. if attach.ReleaseID != releaseID {
  341. log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
  342. ctx.NotFound()
  343. return
  344. }
  345. // FIXME Should prove the existence of the given repo, but results in unnecessary database requests
  346. if err := repo_model.DeleteAttachment(ctx, attach, true); err != nil {
  347. ctx.Error(http.StatusInternalServerError, "DeleteAttachment", err)
  348. return
  349. }
  350. ctx.Status(http.StatusNoContent)
  351. }