Files
Atay-Makhzan/routers/api/v1/repo/release_attachment.go
T

416 lines
11 KiB
Go
Raw Normal View History

2018-03-06 02:22:16 +01:00
// Copyright 2018 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2018-03-06 02:22:16 +01:00
package repo
import (
"errors"
2019-12-20 18:07:12 +01:00
"net/http"
"strings"
2018-03-29 21:32:40 +08:00
2021-11-19 21:39:57 +08:00
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/log"
2018-03-06 02:22:16 +01:00
"code.gitea.io/gitea/modules/setting"
2019-05-11 18:21:34 +08:00
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
2021-01-26 23:36:53 +08:00
"code.gitea.io/gitea/modules/web"
attachment_service "code.gitea.io/gitea/services/attachment"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/context/upload"
2022-12-29 03:57:15 +01:00
"code.gitea.io/gitea/services/convert"
2018-03-06 02:22:16 +01:00
)
2023-11-26 01:21:21 +08:00
func checkReleaseMatchRepo(ctx *context.APIContext, releaseID int64) bool {
release, err := repo_model.GetReleaseByID(ctx, releaseID)
if err != nil {
if repo_model.IsErrReleaseNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2023-11-26 01:21:21 +08:00
return false
}
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2023-11-26 01:21:21 +08:00
return false
}
if release.RepoID != ctx.Repo.Repository.ID {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2023-11-26 01:21:21 +08:00
return false
}
if release.IsDraft && !canAccessReleaseDraft(ctx) {
ctx.APIErrorNotFound()
return false
}
2023-11-26 01:21:21 +08:00
return true
}
2018-03-06 02:22:16 +01:00
// GetReleaseAttachment gets a single attachment of the release
func GetReleaseAttachment(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoGetReleaseAttachment
// ---
// summary: Get a release attachment
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: id
// in: path
// description: id of the release
// type: integer
// format: int64
2018-03-06 02:22:16 +01:00
// required: true
// - name: attachment_id
// in: path
// description: id of the attachment to get
// type: integer
// format: int64
2018-03-06 02:22:16 +01:00
// required: true
// responses:
// "200":
// "$ref": "#/responses/Attachment"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
2024-12-24 21:47:45 +08:00
releaseID := ctx.PathParamInt64("id")
2023-11-26 01:21:21 +08:00
if !checkReleaseMatchRepo(ctx, releaseID) {
return
}
2024-12-24 21:47:45 +08:00
attachID := ctx.PathParamInt64("attachment_id")
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
2018-03-06 02:22:16 +01:00
if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
return
}
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2018-03-06 02:22:16 +01:00
return
}
if attach.ReleaseID != releaseID {
log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2018-03-06 02:22:16 +01:00
return
}
// FIXME Should prove the existence of the given repo, but results in unnecessary database requests
2023-07-10 17:31:19 +08:00
ctx.JSON(http.StatusOK, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
2018-03-06 02:22:16 +01:00
}
// ListReleaseAttachments lists all attachments of the release
func ListReleaseAttachments(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/releases/{id}/assets repository repoListReleaseAttachments
// ---
// summary: List release's attachments
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: id
// in: path
// description: id of the release
// type: integer
// format: int64
2018-03-06 02:22:16 +01:00
// required: true
// responses:
// "200":
// "$ref": "#/responses/AttachmentList"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
2024-12-24 21:47:45 +08:00
releaseID := ctx.PathParamInt64("id")
release, err := repo_model.GetReleaseByID(ctx, releaseID)
2018-03-06 02:22:16 +01:00
if err != nil {
if repo_model.IsErrReleaseNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
return
}
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2018-03-06 02:22:16 +01:00
return
}
if release.RepoID != ctx.Repo.Repository.ID {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2018-03-06 02:22:16 +01:00
return
}
if release.IsDraft && !canAccessReleaseDraft(ctx) {
ctx.APIErrorNotFound()
return
}
if err := release.LoadAttributes(ctx); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2018-03-06 02:22:16 +01:00
return
}
2023-07-10 17:31:19 +08:00
ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, release).Attachments)
2018-03-06 02:22:16 +01:00
}
// CreateReleaseAttachment creates an attachment and saves the given file
func CreateReleaseAttachment(ctx *context.APIContext) {
// swagger:operation POST /repos/{owner}/{repo}/releases/{id}/assets repository repoCreateReleaseAttachment
// ---
// summary: Create a release attachment
// produces:
// - application/json
// consumes:
// - multipart/form-data
// - application/octet-stream
2018-03-06 02:22:16 +01:00
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: id
// in: path
// description: id of the release
// type: integer
// format: int64
2018-03-06 02:22:16 +01:00
// required: true
// - name: name
// in: query
// description: name of the attachment
// type: string
// required: false
// - name: attachment
// in: formData
// description: attachment to upload
// type: file
// required: false
2018-03-06 02:22:16 +01:00
// responses:
// "201":
// "$ref": "#/responses/Attachment"
2019-12-20 18:07:12 +01:00
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
// "413":
// "$ref": "#/responses/error"
2018-03-06 02:22:16 +01:00
// Check if attachments are enabled
if !setting.Attachment.Enabled {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound("Attachment is not enabled")
2018-03-06 02:22:16 +01:00
return
}
// Check if release exists an load release
2024-12-24 21:47:45 +08:00
releaseID := ctx.PathParamInt64("id")
2023-11-26 01:21:21 +08:00
if !checkReleaseMatchRepo(ctx, releaseID) {
2018-03-06 02:22:16 +01:00
return
}
// Get uploaded file from request
var filename string
var uploaderFile *attachment_service.UploaderFile
if strings.HasPrefix(strings.ToLower(ctx.Req.Header.Get("Content-Type")), "multipart/form-data") {
file, header, err := ctx.Req.FormFile("attachment")
if err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
return
}
defer file.Close()
filename = header.Filename
if name := ctx.FormString("name"); name != "" {
filename = name
}
uploaderFile = attachment_service.NewLimitedUploaderKnownSize(file, header.Size)
} else {
filename = ctx.FormString("name")
uploaderFile = attachment_service.NewLimitedUploaderMaxBytesReader(ctx.Req.Body, ctx.Resp)
2018-03-06 02:22:16 +01:00
}
if filename == "" {
2025-02-17 14:13:17 +08:00
ctx.APIError(http.StatusBadRequest, "Could not determine name of attachment.")
return
2018-03-06 02:22:16 +01:00
}
// Create a new attachment and save the file
2026-04-08 01:17:05 +08:00
attach, err := attachment_service.UploadAttachmentForRelease(ctx, uploaderFile, &repo_model.Attachment{
Name: filename,
UploaderID: ctx.Doer.ID,
2023-11-26 01:21:21 +08:00
RepoID: ctx.Repo.Repository.ID,
ReleaseID: releaseID,
})
2018-03-06 02:22:16 +01:00
if err != nil {
2021-09-08 23:19:30 +08:00
if upload.IsErrFileTypeForbidden(err) {
2025-02-17 14:13:17 +08:00
ctx.APIError(http.StatusBadRequest, err)
2021-09-08 23:19:30 +08:00
return
}
if errors.Is(err, util.ErrContentTooLarge) {
ctx.APIError(http.StatusRequestEntityTooLarge, err)
return
}
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2018-03-06 02:22:16 +01:00
return
}
2019-04-03 03:25:05 +08:00
2023-07-10 17:31:19 +08:00
ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
2018-03-06 02:22:16 +01:00
}
// EditReleaseAttachment updates the given attachment
2021-01-26 23:36:53 +08:00
func EditReleaseAttachment(ctx *context.APIContext) {
2018-03-06 02:22:16 +01:00
// swagger:operation PATCH /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoEditReleaseAttachment
// ---
// summary: Edit a release attachment
// produces:
// - application/json
// consumes:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: id
// in: path
// description: id of the release
// type: integer
// format: int64
2018-03-06 02:22:16 +01:00
// required: true
// - name: attachment_id
// in: path
// description: id of the attachment to edit
// type: integer
// format: int64
2018-03-06 02:22:16 +01:00
// required: true
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/EditAttachmentOptions"
// responses:
// "201":
// "$ref": "#/responses/Attachment"
// "422":
// "$ref": "#/responses/validationError"
// "404":
// "$ref": "#/responses/notFound"
2018-03-06 02:22:16 +01:00
2021-01-26 23:36:53 +08:00
form := web.GetForm(ctx).(*api.EditAttachmentOptions)
2018-03-06 02:22:16 +01:00
// Check if release exists an load release
2024-12-24 21:47:45 +08:00
releaseID := ctx.PathParamInt64("id")
2023-11-26 01:21:21 +08:00
if !checkReleaseMatchRepo(ctx, releaseID) {
return
}
2024-12-24 21:47:45 +08:00
attachID := ctx.PathParamInt64("attachment_id")
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
2018-03-06 02:22:16 +01:00
if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
return
}
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2018-03-06 02:22:16 +01:00
return
}
if attach.ReleaseID != releaseID {
log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2018-03-06 02:22:16 +01:00
return
}
// FIXME Should prove the existence of the given repo, but results in unnecessary database requests
if form.Name != "" {
attach.Name = form.Name
}
if err := attachment_service.UpdateAttachment(ctx, setting.Repository.Release.AllowedTypes, attach); err != nil {
if upload.IsErrFileTypeForbidden(err) {
2025-02-17 14:13:17 +08:00
ctx.APIError(http.StatusUnprocessableEntity, err)
return
}
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
return
2018-03-06 02:22:16 +01:00
}
2023-07-10 17:31:19 +08:00
ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
2018-03-06 02:22:16 +01:00
}
// DeleteReleaseAttachment delete a given attachment
func DeleteReleaseAttachment(ctx *context.APIContext) {
// swagger:operation DELETE /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id} repository repoDeleteReleaseAttachment
// ---
// summary: Delete a release attachment
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: id
// in: path
// description: id of the release
// type: integer
// format: int64
2018-03-06 02:22:16 +01:00
// required: true
// - name: attachment_id
// in: path
// description: id of the attachment to delete
// type: integer
// format: int64
2018-03-06 02:22:16 +01:00
// required: true
// responses:
// "204":
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"
2018-03-06 02:22:16 +01:00
// Check if release exists an load release
2024-12-24 21:47:45 +08:00
releaseID := ctx.PathParamInt64("id")
2023-11-26 01:21:21 +08:00
if !checkReleaseMatchRepo(ctx, releaseID) {
return
}
2024-12-24 21:47:45 +08:00
attachID := ctx.PathParamInt64("attachment_id")
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
2018-03-06 02:22:16 +01:00
if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
return
}
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2018-03-06 02:22:16 +01:00
return
}
if attach.ReleaseID != releaseID {
log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2018-03-06 02:22:16 +01:00
return
}
if err := repo_model.DeleteAttachment(ctx, attach, true); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2018-03-06 02:22:16 +01:00
return
}
2019-12-20 18:07:12 +01:00
ctx.Status(http.StatusNoContent)
2018-03-06 02:22:16 +01:00
}