Skip to content

Commit 0d9b44c

Browse files
GiteaBotprskrKN4CK3Rtechknowlogick
authored
Preserve file size when creating attachments (#23406) (#23426)
Backport #23406 by @baez90 When creating attachments (issue, release, repo) the file size (being part of the multipart file header) is passed through the chain of creating an attachment to ensure the MinIO client can stream the file directly instead of having to read it to memory completely at first. Fixes #23393 Co-authored-by: Peter <[email protected]> Co-authored-by: KN4CK3R <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent e87f36e commit 0d9b44c

File tree

8 files changed

+15
-12
lines changed

8 files changed

+15
-12
lines changed

routers/api/v1/repo/issue_attachment.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func CreateIssueAttachment(ctx *context.APIContext) {
176176
filename = query
177177
}
178178

179-
attachment, err := attachment.UploadAttachment(file, setting.Attachment.AllowedTypes, &repo_model.Attachment{
179+
attachment, err := attachment.UploadAttachment(file, setting.Attachment.AllowedTypes, header.Size, &repo_model.Attachment{
180180
Name: filename,
181181
UploaderID: ctx.Doer.ID,
182182
RepoID: ctx.Repo.Repository.ID,

routers/api/v1/repo/issue_comment_attachment.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func CreateIssueCommentAttachment(ctx *context.APIContext) {
180180
filename = query
181181
}
182182

183-
attachment, err := attachment.UploadAttachment(file, setting.Attachment.AllowedTypes, &repo_model.Attachment{
183+
attachment, err := attachment.UploadAttachment(file, setting.Attachment.AllowedTypes, header.Size, &repo_model.Attachment{
184184
Name: filename,
185185
UploaderID: ctx.Doer.ID,
186186
RepoID: ctx.Repo.Repository.ID,

routers/api/v1/repo/release_attachment.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
194194
}
195195

196196
// Create a new attachment and save the file
197-
attach, err := attachment.UploadAttachment(file, setting.Repository.Release.AllowedTypes, &repo_model.Attachment{
197+
attach, err := attachment.UploadAttachment(file, setting.Repository.Release.AllowedTypes, header.Size, &repo_model.Attachment{
198198
Name: filename,
199199
UploaderID: ctx.Doer.ID,
200200
RepoID: release.RepoID,

routers/web/repo/attachment.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func uploadAttachment(ctx *context.Context, repoID int64, allowedTypes string) {
4444
}
4545
defer file.Close()
4646

47-
attach, err := attachment.UploadAttachment(file, allowedTypes, &repo_model.Attachment{
47+
attach, err := attachment.UploadAttachment(file, allowedTypes, header.Size, &repo_model.Attachment{
4848
Name: header.Filename,
4949
UploaderID: ctx.Doer.ID,
5050
RepoID: repoID,

services/attachment/attachment.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import (
1919
)
2020

2121
// NewAttachment creates a new attachment object, but do not verify.
22-
func NewAttachment(attach *repo_model.Attachment, file io.Reader) (*repo_model.Attachment, error) {
22+
func NewAttachment(attach *repo_model.Attachment, file io.Reader, size int64) (*repo_model.Attachment, error) {
2323
if attach.RepoID == 0 {
2424
return nil, fmt.Errorf("attachment %s should belong to a repository", attach.Name)
2525
}
2626

2727
err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
2828
attach.UUID = uuid.New().String()
29-
size, err := storage.Attachments.Save(attach.RelativePath(), file, -1)
29+
size, err := storage.Attachments.Save(attach.RelativePath(), file, size)
3030
if err != nil {
3131
return fmt.Errorf("Create: %w", err)
3232
}
@@ -39,7 +39,7 @@ func NewAttachment(attach *repo_model.Attachment, file io.Reader) (*repo_model.A
3939
}
4040

4141
// UploadAttachment upload new attachment into storage and update database
42-
func UploadAttachment(file io.Reader, allowedTypes string, opts *repo_model.Attachment) (*repo_model.Attachment, error) {
42+
func UploadAttachment(file io.Reader, allowedTypes string, fileSize int64, opts *repo_model.Attachment) (*repo_model.Attachment, error) {
4343
buf := make([]byte, 1024)
4444
n, _ := util.ReadAtMost(file, buf)
4545
buf = buf[:n]
@@ -48,5 +48,5 @@ func UploadAttachment(file io.Reader, allowedTypes string, opts *repo_model.Atta
4848
return nil, err
4949
}
5050

51-
return NewAttachment(opts, io.MultiReader(bytes.NewReader(buf), file))
51+
return NewAttachment(opts, io.MultiReader(bytes.NewReader(buf), file), fileSize)
5252
}

services/attachment/attachment_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestUploadAttachment(t *testing.T) {
3636
RepoID: 1,
3737
UploaderID: user.ID,
3838
Name: filepath.Base(fPath),
39-
}, f)
39+
}, f, -1)
4040
assert.NoError(t, err)
4141

4242
attachment, err := repo_model.GetAttachmentByUUID(db.DefaultContext, attach.UUID)

services/mailer/incoming/incoming_handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (h *ReplyHandler) Handle(ctx context.Context, content *MailContent, doer *u
8787
attachmentIDs := make([]string, 0, len(content.Attachments))
8888
if setting.Attachment.Enabled {
8989
for _, attachment := range content.Attachments {
90-
a, err := attachment_service.UploadAttachment(bytes.NewReader(attachment.Content), setting.Attachment.AllowedTypes, &repo_model.Attachment{
90+
a, err := attachment_service.UploadAttachment(bytes.NewReader(attachment.Content), setting.Attachment.AllowedTypes, int64(len(attachment.Content)), &repo_model.Attachment{
9191
Name: attachment.Name,
9292
UploaderID: doer.ID,
9393
RepoID: issue.Repo.ID,

services/release/release_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@ func TestRelease_Create(t *testing.T) {
106106
IsTag: false,
107107
}, nil, ""))
108108

109+
testPlayload := "testtest"
110+
109111
attach, err := attachment.NewAttachment(&repo_model.Attachment{
110112
RepoID: repo.ID,
111113
UploaderID: user.ID,
112114
Name: "test.txt",
113-
}, strings.NewReader("testtest"))
115+
}, strings.NewReader(testPlayload), int64(len([]byte(testPlayload))))
114116
assert.NoError(t, err)
115117

116118
release := repo_model.Release{
@@ -239,11 +241,12 @@ func TestRelease_Update(t *testing.T) {
239241
assert.Equal(t, tagName, release.TagName)
240242

241243
// Add new attachments
244+
samplePayload := "testtest"
242245
attach, err := attachment.NewAttachment(&repo_model.Attachment{
243246
RepoID: repo.ID,
244247
UploaderID: user.ID,
245248
Name: "test.txt",
246-
}, strings.NewReader("testtest"))
249+
}, strings.NewReader(samplePayload), int64(len([]byte(samplePayload))))
247250
assert.NoError(t, err)
248251

249252
assert.NoError(t, UpdateRelease(user, gitRepo, release, []string{attach.UUID}, nil, nil))

0 commit comments

Comments
 (0)