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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "strings"
  7. "code.gitea.io/gitea/models"
  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/upload"
  13. )
  14. // GetReleaseAttachment gets a single attachment of the release
  15. func GetReleaseAttachment(ctx *context.APIContext) {
  16. // swagger:operation GET /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoGetReleaseAttachment
  17. // ---
  18. // summary: Get a release attachment
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: owner
  23. // in: path
  24. // description: owner of the repo
  25. // type: string
  26. // required: true
  27. // - name: repo
  28. // in: path
  29. // description: name of the repo
  30. // type: string
  31. // required: true
  32. // - name: id
  33. // in: path
  34. // description: id of the release
  35. // type: integer
  36. // format: int64
  37. // required: true
  38. // - name: attachment_id
  39. // in: path
  40. // description: id of the attachment to get
  41. // type: integer
  42. // format: int64
  43. // required: true
  44. // responses:
  45. // "200":
  46. // "$ref": "#/responses/Attachment"
  47. releaseID := ctx.ParamsInt64(":id")
  48. attachID := ctx.ParamsInt64(":asset")
  49. attach, err := models.GetAttachmentByID(attachID)
  50. if err != nil {
  51. ctx.Error(500, "GetAttachmentByID", err)
  52. return
  53. }
  54. if attach.ReleaseID != releaseID {
  55. log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
  56. ctx.NotFound()
  57. return
  58. }
  59. // FIXME Should prove the existence of the given repo, but results in unnecessary database requests
  60. ctx.JSON(200, attach.APIFormat())
  61. }
  62. // ListReleaseAttachments lists all attachments of the release
  63. func ListReleaseAttachments(ctx *context.APIContext) {
  64. // swagger:operation GET /repos/{owner}/{repo}/releases/{id}/assets repository repoListReleaseAttachments
  65. // ---
  66. // summary: List release'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: id
  81. // in: path
  82. // description: id of the release
  83. // type: integer
  84. // format: int64
  85. // required: true
  86. // responses:
  87. // "200":
  88. // "$ref": "#/responses/AttachmentList"
  89. releaseID := ctx.ParamsInt64(":id")
  90. release, err := models.GetReleaseByID(releaseID)
  91. if err != nil {
  92. ctx.Error(500, "GetReleaseByID", err)
  93. return
  94. }
  95. if release.RepoID != ctx.Repo.Repository.ID {
  96. ctx.NotFound()
  97. return
  98. }
  99. if err := release.LoadAttributes(); err != nil {
  100. ctx.Error(500, "LoadAttributes", err)
  101. return
  102. }
  103. ctx.JSON(200, release.APIFormat().Attachments)
  104. }
  105. // CreateReleaseAttachment creates an attachment and saves the given file
  106. func CreateReleaseAttachment(ctx *context.APIContext) {
  107. // swagger:operation POST /repos/{owner}/{repo}/releases/{id}/assets repository repoCreateReleaseAttachment
  108. // ---
  109. // summary: Create a release attachment
  110. // produces:
  111. // - application/json
  112. // consumes:
  113. // - multipart/form-data
  114. // parameters:
  115. // - name: owner
  116. // in: path
  117. // description: owner of the repo
  118. // type: string
  119. // required: true
  120. // - name: repo
  121. // in: path
  122. // description: name of the repo
  123. // type: string
  124. // required: true
  125. // - name: id
  126. // in: path
  127. // description: id of the release
  128. // type: integer
  129. // format: int64
  130. // required: true
  131. // - name: name
  132. // in: query
  133. // description: name of the attachment
  134. // type: string
  135. // required: false
  136. // - name: attachment
  137. // in: formData
  138. // description: attachment to upload
  139. // type: file
  140. // required: true
  141. // responses:
  142. // "201":
  143. // "$ref": "#/responses/Attachment"
  144. // Check if attachments are enabled
  145. if !setting.AttachmentEnabled {
  146. ctx.NotFound("Attachment is not enabled")
  147. return
  148. }
  149. // Check if release exists an load release
  150. releaseID := ctx.ParamsInt64(":id")
  151. release, err := models.GetReleaseByID(releaseID)
  152. if err != nil {
  153. ctx.Error(500, "GetReleaseByID", err)
  154. return
  155. }
  156. // Get uploaded file from request
  157. file, header, err := ctx.GetFile("attachment")
  158. if err != nil {
  159. ctx.Error(500, "GetFile", err)
  160. return
  161. }
  162. defer file.Close()
  163. buf := make([]byte, 1024)
  164. n, _ := file.Read(buf)
  165. if n > 0 {
  166. buf = buf[:n]
  167. }
  168. // Check if the filetype is allowed by the settings
  169. err = upload.VerifyAllowedContentType(buf, strings.Split(setting.AttachmentAllowedTypes, ","))
  170. if err != nil {
  171. ctx.Error(400, "DetectContentType", err)
  172. return
  173. }
  174. var filename = header.Filename
  175. if query := ctx.Query("name"); query != "" {
  176. filename = query
  177. }
  178. // Create a new attachment and save the file
  179. attach, err := models.NewAttachment(&models.Attachment{
  180. UploaderID: ctx.User.ID,
  181. Name: filename,
  182. ReleaseID: release.ID,
  183. }, buf, file)
  184. if err != nil {
  185. ctx.Error(500, "NewAttachment", err)
  186. return
  187. }
  188. ctx.JSON(201, attach.APIFormat())
  189. }
  190. // EditReleaseAttachment updates the given attachment
  191. func EditReleaseAttachment(ctx *context.APIContext, form api.EditAttachmentOptions) {
  192. // swagger:operation PATCH /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoEditReleaseAttachment
  193. // ---
  194. // summary: Edit a release attachment
  195. // produces:
  196. // - application/json
  197. // consumes:
  198. // - application/json
  199. // parameters:
  200. // - name: owner
  201. // in: path
  202. // description: owner of the repo
  203. // type: string
  204. // required: true
  205. // - name: repo
  206. // in: path
  207. // description: name of the repo
  208. // type: string
  209. // required: true
  210. // - name: id
  211. // in: path
  212. // description: id of the release
  213. // type: integer
  214. // format: int64
  215. // required: true
  216. // - name: attachment_id
  217. // in: path
  218. // description: id of the attachment to edit
  219. // type: integer
  220. // format: int64
  221. // required: true
  222. // - name: body
  223. // in: body
  224. // schema:
  225. // "$ref": "#/definitions/EditAttachmentOptions"
  226. // responses:
  227. // "201":
  228. // "$ref": "#/responses/Attachment"
  229. // Check if release exists an load release
  230. releaseID := ctx.ParamsInt64(":id")
  231. attachID := ctx.ParamsInt64(":asset")
  232. attach, err := models.GetAttachmentByID(attachID)
  233. if err != nil {
  234. ctx.Error(500, "GetAttachmentByID", err)
  235. return
  236. }
  237. if attach.ReleaseID != releaseID {
  238. log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
  239. ctx.NotFound()
  240. return
  241. }
  242. // FIXME Should prove the existence of the given repo, but results in unnecessary database requests
  243. if form.Name != "" {
  244. attach.Name = form.Name
  245. }
  246. if err := models.UpdateAttachment(attach); err != nil {
  247. ctx.Error(500, "UpdateAttachment", attach)
  248. }
  249. ctx.JSON(201, attach.APIFormat())
  250. }
  251. // DeleteReleaseAttachment delete a given attachment
  252. func DeleteReleaseAttachment(ctx *context.APIContext) {
  253. // swagger:operation DELETE /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoDeleteReleaseAttachment
  254. // ---
  255. // summary: Delete a release attachment
  256. // produces:
  257. // - application/json
  258. // parameters:
  259. // - name: owner
  260. // in: path
  261. // description: owner of the repo
  262. // type: string
  263. // required: true
  264. // - name: repo
  265. // in: path
  266. // description: name of the repo
  267. // type: string
  268. // required: true
  269. // - name: id
  270. // in: path
  271. // description: id of the release
  272. // type: integer
  273. // format: int64
  274. // required: true
  275. // - name: attachment_id
  276. // in: path
  277. // description: id of the attachment to delete
  278. // type: integer
  279. // format: int64
  280. // required: true
  281. // responses:
  282. // "204":
  283. // "$ref": "#/responses/empty"
  284. // Check if release exists an load release
  285. releaseID := ctx.ParamsInt64(":id")
  286. attachID := ctx.ParamsInt64(":asset")
  287. attach, err := models.GetAttachmentByID(attachID)
  288. if err != nil {
  289. ctx.Error(500, "GetAttachmentByID", err)
  290. return
  291. }
  292. if attach.ReleaseID != releaseID {
  293. log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
  294. ctx.NotFound()
  295. return
  296. }
  297. // FIXME Should prove the existence of the given repo, but results in unnecessary database requests
  298. if err := models.DeleteAttachment(attach, true); err != nil {
  299. ctx.Error(500, "DeleteAttachment", err)
  300. return
  301. }
  302. ctx.Status(204)
  303. }