Skip to content

Commit 09fb036

Browse files
lunnytechknowlogick
authored andcommitted
fix upload attachments (#6481)
* fix upload attachments * add migration for new column uploader_id on table attachment * fix imports sequence
1 parent 0a8e63c commit 09fb036

File tree

7 files changed

+76
-14
lines changed

7 files changed

+76
-14
lines changed

models/attachment.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package models
77
import (
88
"fmt"
99
"io"
10-
"mime/multipart"
1110
"os"
1211
"path"
1312

@@ -25,6 +24,7 @@ type Attachment struct {
2524
UUID string `xorm:"uuid UNIQUE"`
2625
IssueID int64 `xorm:"INDEX"`
2726
ReleaseID int64 `xorm:"INDEX"`
27+
UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added
2828
CommentID int64
2929
Name string
3030
DownloadCount int64 `xorm:"DEFAULT 0"`
@@ -72,11 +72,8 @@ func (a *Attachment) DownloadURL() string {
7272
}
7373

7474
// NewAttachment creates a new attachment object.
75-
func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
76-
attach := &Attachment{
77-
UUID: gouuid.NewV4().String(),
78-
Name: name,
79-
}
75+
func NewAttachment(attach *Attachment, buf []byte, file io.Reader) (_ *Attachment, err error) {
76+
attach.UUID = gouuid.NewV4().String()
8077

8178
localPath := attach.LocalPath()
8279
if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {

models/attachment_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,40 @@
55
package models
66

77
import (
8+
"os"
9+
"path/filepath"
810
"testing"
911

1012
"github.com/stretchr/testify/assert"
1113
)
1214

15+
func TestUploadAttachment(t *testing.T) {
16+
assert.NoError(t, PrepareTestDatabase())
17+
18+
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
19+
20+
var fPath = "./attachment_test.go"
21+
f, err := os.Open(fPath)
22+
assert.NoError(t, err)
23+
defer f.Close()
24+
25+
var buf = make([]byte, 1024)
26+
n, err := f.Read(buf)
27+
assert.NoError(t, err)
28+
buf = buf[:n]
29+
30+
attach, err := NewAttachment(&Attachment{
31+
UploaderID: user.ID,
32+
Name: filepath.Base(fPath),
33+
}, buf, f)
34+
assert.NoError(t, err)
35+
36+
attachment, err := GetAttachmentByUUID(attach.UUID)
37+
assert.NoError(t, err)
38+
assert.EqualValues(t, user.ID, attachment.UploaderID)
39+
assert.Equal(t, int64(0), attachment.DownloadCount)
40+
}
41+
1342
func TestIncreaseDownloadCount(t *testing.T) {
1443
assert.NoError(t, PrepareTestDatabase())
1544

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ var migrations = []Migration{
219219
NewMigration("update U2F counter type", changeU2FCounterType),
220220
// v82 -> v83
221221
NewMigration("hot fix for wrong release sha1 on release table", fixReleaseSha1OnReleaseTable),
222+
// v83 -> v84
223+
NewMigration("add uploader id for table attachment", addUploaderIDForAttachment),
222224
}
223225

224226
// Migrate database to current version

models/migrations/v83.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2019 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+
5+
package migrations
6+
7+
import (
8+
"code.gitea.io/gitea/modules/util"
9+
10+
"github.com/go-xorm/xorm"
11+
)
12+
13+
func addUploaderIDForAttachment(x *xorm.Engine) error {
14+
type Attachment struct {
15+
ID int64 `xorm:"pk autoincr"`
16+
UUID string `xorm:"uuid UNIQUE"`
17+
IssueID int64 `xorm:"INDEX"`
18+
ReleaseID int64 `xorm:"INDEX"`
19+
UploaderID int64 `xorm:"INDEX DEFAULT 0"`
20+
CommentID int64
21+
Name string
22+
DownloadCount int64 `xorm:"DEFAULT 0"`
23+
Size int64 `xorm:"DEFAULT 0"`
24+
CreatedUnix util.TimeStamp `xorm:"created"`
25+
}
26+
27+
return x.Sync2(new(Attachment))
28+
}

routers/api/v1/repo/release_attachment.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,16 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
200200
}
201201

202202
// Create a new attachment and save the file
203-
attach, err := models.NewAttachment(filename, buf, file)
203+
attach, err := models.NewAttachment(&models.Attachment{
204+
UploaderID: ctx.User.ID,
205+
Name: filename,
206+
ReleaseID: release.ID,
207+
}, buf, file)
204208
if err != nil {
205209
ctx.Error(500, "NewAttachment", err)
206210
return
207211
}
208-
attach.ReleaseID = release.ID
209-
if err := models.UpdateAttachment(attach); err != nil {
210-
ctx.Error(500, "UpdateAttachment", err)
211-
return
212-
}
212+
213213
ctx.JSON(201, attach.APIFormat())
214214
}
215215

routers/repo/attachment.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ func UploadAttachment(ctx *context.Context) {
6060
return
6161
}
6262

63-
attach, err := models.NewAttachment(header.Filename, buf, file)
63+
attach, err := models.NewAttachment(&models.Attachment{
64+
UploaderID: ctx.User.ID,
65+
Name: header.Filename,
66+
}, buf, file)
6467
if err != nil {
6568
ctx.Error(500, fmt.Sprintf("NewAttachment: %v", err))
6669
return

routers/routes/routes.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,12 @@ func RegisterRoutes(m *macaron.Macaron) {
480480
return
481481
}
482482
})
483-
m.Post("/attachments", repo.UploadAttachment)
484483
}, ignSignIn)
485484

485+
m.Group("", func() {
486+
m.Post("/attachments", repo.UploadAttachment)
487+
}, reqSignIn)
488+
486489
m.Group("/:username", func() {
487490
m.Get("/action/:action", user.Action)
488491
}, reqSignIn)

0 commit comments

Comments
 (0)