diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-09-08 23:19:30 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-08 17:19:30 +0200 |
commit | ddc709ff7f94bd627ac05209a16ea5a5e24b7413 (patch) | |
tree | 1420d827e00769c1a8330c6b1a80e7cbbc343332 /routers/api | |
parent | f55cd033ed8dbee04e90faede5cca2dcf697e102 (diff) | |
download | gitea-ddc709ff7f94bd627ac05209a16ea5a5e24b7413.tar.gz gitea-ddc709ff7f94bd627ac05209a16ea5a5e24b7413.zip |
Add repo_id for attachment (#16958)
When create a new issue or comment and paste/upload an attachment/image, it will not assign an issue id before submit. So if user give up the creating, the attachments will lost key feature and become dirty content. We don't know if we need to delete the attachment even if the repository deleted.
This PR add a repo_id in attachment table so that even if a new upload attachment with no issue_id or release_id but should have repo_id. When deleting a repository, they could also be deleted.
Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/repo/release_attachment.go | 24 |
1 files changed, 6 insertions, 18 deletions
diff --git a/routers/api/v1/repo/release_attachment.go b/routers/api/v1/repo/release_attachment.go index 0834667657..d1533e2b5a 100644 --- a/routers/api/v1/repo/release_attachment.go +++ b/routers/api/v1/repo/release_attachment.go @@ -15,6 +15,7 @@ import ( api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/upload" "code.gitea.io/gitea/modules/web" + "code.gitea.io/gitea/services/attachment" ) // GetReleaseAttachment gets a single attachment of the release @@ -176,31 +177,18 @@ func CreateReleaseAttachment(ctx *context.APIContext) { } defer file.Close() - buf := make([]byte, 1024) - n, _ := file.Read(buf) - if n > 0 { - buf = buf[:n] - } - - // Check if the filetype is allowed by the settings - err = upload.Verify(buf, header.Filename, setting.Repository.Release.AllowedTypes) - if err != nil { - ctx.Error(http.StatusBadRequest, "DetectContentType", err) - return - } - var filename = header.Filename if query := ctx.FormString("name"); query != "" { filename = query } // Create a new attachment and save the file - attach, err := models.NewAttachment(&models.Attachment{ - UploaderID: ctx.User.ID, - Name: filename, - ReleaseID: release.ID, - }, buf, file) + attach, err := attachment.UploadAttachment(file, ctx.User.ID, release.RepoID, releaseID, filename, setting.Repository.Release.AllowedTypes) if err != nil { + if upload.IsErrFileTypeForbidden(err) { + ctx.Error(http.StatusBadRequest, "DetectContentType", err) + return + } ctx.Error(http.StatusInternalServerError, "NewAttachment", err) return } |