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.8KB

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