選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

release_attachment.go 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. "errors"
  7. "net/http"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/sdk/gitea"
  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. // required: true
  37. // - name: attachment_id
  38. // in: path
  39. // description: id of the attachment to get
  40. // type: integer
  41. // required: true
  42. // responses:
  43. // "200":
  44. // "$ref": "#/responses/Attachment"
  45. releaseID := ctx.ParamsInt64(":id")
  46. attachID := ctx.ParamsInt64(":asset")
  47. attach, err := models.GetAttachmentByID(attachID)
  48. if err != nil {
  49. ctx.Error(500, "GetAttachmentByID", err)
  50. return
  51. }
  52. if attach.ReleaseID != releaseID {
  53. ctx.Status(404)
  54. return
  55. }
  56. // FIXME Should prove the existence of the given repo, but results in unnecessary database requests
  57. ctx.JSON(200, attach.APIFormat())
  58. }
  59. // ListReleaseAttachments lists all attachments of the release
  60. func ListReleaseAttachments(ctx *context.APIContext) {
  61. // swagger:operation GET /repos/{owner}/{repo}/releases/{id}/assets repository repoListReleaseAttachments
  62. // ---
  63. // summary: List release's attachments
  64. // produces:
  65. // - application/json
  66. // parameters:
  67. // - name: owner
  68. // in: path
  69. // description: owner of the repo
  70. // type: string
  71. // required: true
  72. // - name: repo
  73. // in: path
  74. // description: name of the repo
  75. // type: string
  76. // required: true
  77. // - name: id
  78. // in: path
  79. // description: id of the release
  80. // type: integer
  81. // required: true
  82. // responses:
  83. // "200":
  84. // "$ref": "#/responses/AttachmentList"
  85. releaseID := ctx.ParamsInt64(":id")
  86. release, err := models.GetReleaseByID(releaseID)
  87. if err != nil {
  88. ctx.Error(500, "GetReleaseByID", err)
  89. return
  90. }
  91. if release.RepoID != ctx.Repo.Repository.ID {
  92. ctx.Status(404)
  93. return
  94. }
  95. if err := release.LoadAttributes(); err != nil {
  96. ctx.Error(500, "LoadAttributes", err)
  97. return
  98. }
  99. ctx.JSON(200, release.APIFormat().Attachments)
  100. }
  101. // CreateReleaseAttachment creates an attachment and saves the given file
  102. func CreateReleaseAttachment(ctx *context.APIContext) {
  103. // swagger:operation POST /repos/{owner}/{repo}/releases/{id}/assets repository repoCreateReleaseAttachment
  104. // ---
  105. // summary: Create a release 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: id
  122. // in: path
  123. // description: id of the release
  124. // type: integer
  125. // required: true
  126. // - name: name
  127. // in: query
  128. // description: name of the attachment
  129. // type: string
  130. // required: false
  131. // - name: attachment
  132. // in: formData
  133. // description: attachment to upload
  134. // type: file
  135. // required: true
  136. // responses:
  137. // "201":
  138. // "$ref": "#/responses/Attachment"
  139. // Check if attachments are enabled
  140. if !setting.AttachmentEnabled {
  141. ctx.Error(404, "AttachmentEnabled", errors.New("attachment is not enabled"))
  142. return
  143. }
  144. // Check if release exists an load release
  145. releaseID := ctx.ParamsInt64(":id")
  146. release, err := models.GetReleaseByID(releaseID)
  147. if err != nil {
  148. ctx.Error(500, "GetReleaseByID", err)
  149. return
  150. }
  151. // Get uploaded file from request
  152. file, header, err := ctx.GetFile("attachment")
  153. if err != nil {
  154. ctx.Error(500, "GetFile", err)
  155. return
  156. }
  157. defer file.Close()
  158. buf := make([]byte, 1024)
  159. n, _ := file.Read(buf)
  160. if n > 0 {
  161. buf = buf[:n]
  162. }
  163. // Check if the filetype is allowed by the settings
  164. fileType := http.DetectContentType(buf)
  165. allowedTypes := strings.Split(setting.AttachmentAllowedTypes, ",")
  166. allowed := false
  167. for _, t := range allowedTypes {
  168. t := strings.Trim(t, " ")
  169. if t == "*/*" || t == fileType {
  170. allowed = true
  171. break
  172. }
  173. }
  174. if !allowed {
  175. ctx.Error(400, "DetectContentType", errors.New("File type is not allowed"))
  176. return
  177. }
  178. var filename = header.Filename
  179. if query := ctx.Query("name"); query != "" {
  180. filename = query
  181. }
  182. // Create a new attachment and save the file
  183. attach, err := models.NewAttachment(filename, buf, file)
  184. if err != nil {
  185. ctx.Error(500, "NewAttachment", err)
  186. return
  187. }
  188. attach.ReleaseID = release.ID
  189. if err := models.UpdateAttachment(attach); err != nil {
  190. ctx.Error(500, "UpdateAttachment", err)
  191. return
  192. }
  193. ctx.JSON(201, attach.APIFormat())
  194. }
  195. // EditReleaseAttachment updates the given attachment
  196. func EditReleaseAttachment(ctx *context.APIContext, form api.EditAttachmentOptions) {
  197. // swagger:operation PATCH /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoEditReleaseAttachment
  198. // ---
  199. // summary: Edit a release attachment
  200. // produces:
  201. // - application/json
  202. // consumes:
  203. // - application/json
  204. // parameters:
  205. // - name: owner
  206. // in: path
  207. // description: owner of the repo
  208. // type: string
  209. // required: true
  210. // - name: repo
  211. // in: path
  212. // description: name of the repo
  213. // type: string
  214. // required: true
  215. // - name: id
  216. // in: path
  217. // description: id of the release
  218. // type: integer
  219. // required: true
  220. // - name: attachment_id
  221. // in: path
  222. // description: id of the attachment to edit
  223. // type: integer
  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(":attachment")
  235. attach, err := models.GetAttachmentByID(attachID)
  236. if err != nil {
  237. ctx.Error(500, "GetAttachmentByID", err)
  238. return
  239. }
  240. if attach.ReleaseID != releaseID {
  241. ctx.Status(404)
  242. return
  243. }
  244. // FIXME Should prove the existence of the given repo, but results in unnecessary database requests
  245. if form.Name != "" {
  246. attach.Name = form.Name
  247. }
  248. if err := models.UpdateAttachment(attach); err != nil {
  249. ctx.Error(500, "UpdateAttachment", attach)
  250. }
  251. ctx.JSON(201, attach.APIFormat())
  252. }
  253. // DeleteReleaseAttachment delete a given attachment
  254. func DeleteReleaseAttachment(ctx *context.APIContext) {
  255. // swagger:operation DELETE /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoDeleteReleaseAttachment
  256. // ---
  257. // summary: Delete a release attachment
  258. // produces:
  259. // - application/json
  260. // parameters:
  261. // - name: owner
  262. // in: path
  263. // description: owner of the repo
  264. // type: string
  265. // required: true
  266. // - name: repo
  267. // in: path
  268. // description: name of the repo
  269. // type: string
  270. // required: true
  271. // - name: id
  272. // in: path
  273. // description: id of the release
  274. // type: integer
  275. // required: true
  276. // - name: attachment_id
  277. // in: path
  278. // description: id of the attachment to delete
  279. // type: integer
  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(":attachment")
  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. ctx.Status(404)
  294. return
  295. }
  296. // FIXME Should prove the existence of the given repo, but results in unnecessary database requests
  297. if err := models.DeleteAttachment(attach, true); err != nil {
  298. ctx.Error(500, "DeleteAttachment", err)
  299. return
  300. }
  301. ctx.Status(204)
  302. }