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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  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(500, "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(200, 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(500, "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(500, "LoadAttributes", err)
  102. return
  103. }
  104. ctx.JSON(200, 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. // Check if attachments are enabled
  146. if !setting.AttachmentEnabled {
  147. ctx.NotFound("Attachment is not enabled")
  148. return
  149. }
  150. // Check if release exists an load release
  151. releaseID := ctx.ParamsInt64(":id")
  152. release, err := models.GetReleaseByID(releaseID)
  153. if err != nil {
  154. ctx.Error(500, "GetReleaseByID", err)
  155. return
  156. }
  157. // Get uploaded file from request
  158. file, header, err := ctx.GetFile("attachment")
  159. if err != nil {
  160. ctx.Error(500, "GetFile", err)
  161. return
  162. }
  163. defer file.Close()
  164. buf := make([]byte, 1024)
  165. n, _ := file.Read(buf)
  166. if n > 0 {
  167. buf = buf[:n]
  168. }
  169. // Check if the filetype is allowed by the settings
  170. fileType := http.DetectContentType(buf)
  171. allowedTypes := strings.Split(setting.AttachmentAllowedTypes, ",")
  172. allowed := false
  173. for _, t := range allowedTypes {
  174. t := strings.Trim(t, " ")
  175. if t == "*/*" || t == fileType ||
  176. strings.HasPrefix(fileType, t+";") {
  177. allowed = true
  178. break
  179. }
  180. }
  181. if !allowed {
  182. ctx.Error(400, "DetectContentType", errors.New("File type is not allowed"))
  183. return
  184. }
  185. var filename = header.Filename
  186. if query := ctx.Query("name"); query != "" {
  187. filename = query
  188. }
  189. // Create a new attachment and save the file
  190. attach, err := models.NewAttachment(&models.Attachment{
  191. UploaderID: ctx.User.ID,
  192. Name: filename,
  193. ReleaseID: release.ID,
  194. }, buf, file)
  195. if err != nil {
  196. ctx.Error(500, "NewAttachment", err)
  197. return
  198. }
  199. ctx.JSON(201, attach.APIFormat())
  200. }
  201. // EditReleaseAttachment updates the given attachment
  202. func EditReleaseAttachment(ctx *context.APIContext, form api.EditAttachmentOptions) {
  203. // swagger:operation PATCH /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoEditReleaseAttachment
  204. // ---
  205. // summary: Edit a release attachment
  206. // produces:
  207. // - application/json
  208. // consumes:
  209. // - application/json
  210. // parameters:
  211. // - name: owner
  212. // in: path
  213. // description: owner of the repo
  214. // type: string
  215. // required: true
  216. // - name: repo
  217. // in: path
  218. // description: name of the repo
  219. // type: string
  220. // required: true
  221. // - name: id
  222. // in: path
  223. // description: id of the release
  224. // type: integer
  225. // format: int64
  226. // required: true
  227. // - name: attachment_id
  228. // in: path
  229. // description: id of the attachment to edit
  230. // type: integer
  231. // format: int64
  232. // required: true
  233. // - name: body
  234. // in: body
  235. // schema:
  236. // "$ref": "#/definitions/EditAttachmentOptions"
  237. // responses:
  238. // "201":
  239. // "$ref": "#/responses/Attachment"
  240. // Check if release exists an load release
  241. releaseID := ctx.ParamsInt64(":id")
  242. attachID := ctx.ParamsInt64(":asset")
  243. attach, err := models.GetAttachmentByID(attachID)
  244. if err != nil {
  245. ctx.Error(500, "GetAttachmentByID", err)
  246. return
  247. }
  248. if attach.ReleaseID != releaseID {
  249. log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
  250. ctx.NotFound()
  251. return
  252. }
  253. // FIXME Should prove the existence of the given repo, but results in unnecessary database requests
  254. if form.Name != "" {
  255. attach.Name = form.Name
  256. }
  257. if err := models.UpdateAttachment(attach); err != nil {
  258. ctx.Error(500, "UpdateAttachment", attach)
  259. }
  260. ctx.JSON(201, attach.APIFormat())
  261. }
  262. // DeleteReleaseAttachment delete a given attachment
  263. func DeleteReleaseAttachment(ctx *context.APIContext) {
  264. // swagger:operation DELETE /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoDeleteReleaseAttachment
  265. // ---
  266. // summary: Delete a release attachment
  267. // produces:
  268. // - application/json
  269. // parameters:
  270. // - name: owner
  271. // in: path
  272. // description: owner of the repo
  273. // type: string
  274. // required: true
  275. // - name: repo
  276. // in: path
  277. // description: name of the repo
  278. // type: string
  279. // required: true
  280. // - name: id
  281. // in: path
  282. // description: id of the release
  283. // type: integer
  284. // format: int64
  285. // required: true
  286. // - name: attachment_id
  287. // in: path
  288. // description: id of the attachment to delete
  289. // type: integer
  290. // format: int64
  291. // required: true
  292. // responses:
  293. // "204":
  294. // "$ref": "#/responses/empty"
  295. // Check if release exists an load release
  296. releaseID := ctx.ParamsInt64(":id")
  297. attachID := ctx.ParamsInt64(":asset")
  298. attach, err := models.GetAttachmentByID(attachID)
  299. if err != nil {
  300. ctx.Error(500, "GetAttachmentByID", err)
  301. return
  302. }
  303. if attach.ReleaseID != releaseID {
  304. log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
  305. ctx.NotFound()
  306. return
  307. }
  308. // FIXME Should prove the existence of the given repo, but results in unnecessary database requests
  309. if err := models.DeleteAttachment(attach, true); err != nil {
  310. ctx.Error(500, "DeleteAttachment", err)
  311. return
  312. }
  313. ctx.Status(204)
  314. }