|
-
-
-
-
- package repo
-
- import (
- "strings"
-
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/context"
- "code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/setting"
- api "code.gitea.io/gitea/modules/structs"
- "code.gitea.io/gitea/modules/upload"
- )
-
-
- func GetReleaseAttachment(ctx *context.APIContext) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- releaseID := ctx.ParamsInt64(":id")
- attachID := ctx.ParamsInt64(":asset")
- attach, err := models.GetAttachmentByID(attachID)
- if err != nil {
- ctx.Error(500, "GetAttachmentByID", err)
- return
- }
- if attach.ReleaseID != releaseID {
- log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
- ctx.NotFound()
- return
- }
-
- ctx.JSON(200, attach.APIFormat())
- }
-
-
- func ListReleaseAttachments(ctx *context.APIContext) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- releaseID := ctx.ParamsInt64(":id")
- release, err := models.GetReleaseByID(releaseID)
- if err != nil {
- ctx.Error(500, "GetReleaseByID", err)
- return
- }
- if release.RepoID != ctx.Repo.Repository.ID {
- ctx.NotFound()
- return
- }
- if err := release.LoadAttributes(); err != nil {
- ctx.Error(500, "LoadAttributes", err)
- return
- }
- ctx.JSON(200, release.APIFormat().Attachments)
- }
-
-
- func CreateReleaseAttachment(ctx *context.APIContext) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if !setting.AttachmentEnabled {
- ctx.NotFound("Attachment is not enabled")
- return
- }
-
-
- releaseID := ctx.ParamsInt64(":id")
- release, err := models.GetReleaseByID(releaseID)
- if err != nil {
- ctx.Error(500, "GetReleaseByID", err)
- return
- }
-
-
- file, header, err := ctx.GetFile("attachment")
- if err != nil {
- ctx.Error(500, "GetFile", err)
- return
- }
- defer file.Close()
-
- buf := make([]byte, 1024)
- n, _ := file.Read(buf)
- if n > 0 {
- buf = buf[:n]
- }
-
-
- err = upload.VerifyAllowedContentType(buf, strings.Split(setting.AttachmentAllowedTypes, ","))
- if err != nil {
- ctx.Error(400, "DetectContentType", err)
- return
- }
-
- var filename = header.Filename
- if query := ctx.Query("name"); query != "" {
- filename = query
- }
-
-
- attach, err := models.NewAttachment(&models.Attachment{
- UploaderID: ctx.User.ID,
- Name: filename,
- ReleaseID: release.ID,
- }, buf, file)
- if err != nil {
- ctx.Error(500, "NewAttachment", err)
- return
- }
-
- ctx.JSON(201, attach.APIFormat())
- }
-
-
- func EditReleaseAttachment(ctx *context.APIContext, form api.EditAttachmentOptions) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- releaseID := ctx.ParamsInt64(":id")
- attachID := ctx.ParamsInt64(":asset")
- attach, err := models.GetAttachmentByID(attachID)
- if err != nil {
- ctx.Error(500, "GetAttachmentByID", err)
- return
- }
- if attach.ReleaseID != releaseID {
- log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
- ctx.NotFound()
- return
- }
-
- if form.Name != "" {
- attach.Name = form.Name
- }
-
- if err := models.UpdateAttachment(attach); err != nil {
- ctx.Error(500, "UpdateAttachment", attach)
- }
- ctx.JSON(201, attach.APIFormat())
- }
-
-
- func DeleteReleaseAttachment(ctx *context.APIContext) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- releaseID := ctx.ParamsInt64(":id")
- attachID := ctx.ParamsInt64(":asset")
- attach, err := models.GetAttachmentByID(attachID)
- if err != nil {
- ctx.Error(500, "GetAttachmentByID", err)
- return
- }
- if attach.ReleaseID != releaseID {
- log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
- ctx.NotFound()
- return
- }
-
-
- if err := models.DeleteAttachment(attach, true); err != nil {
- ctx.Error(500, "DeleteAttachment", err)
- return
- }
- ctx.Status(204)
- }
|