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 9.3KB

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