Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix upload attachments #6481

Merged
merged 5 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions models/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package models
import (
"fmt"
"io"
"mime/multipart"
"os"
"path"

Expand All @@ -25,6 +24,7 @@ type Attachment struct {
UUID string `xorm:"uuid UNIQUE"`
IssueID int64 `xorm:"INDEX"`
ReleaseID int64 `xorm:"INDEX"`
UploaderID int64 `xorm:"INDEX"`
CommentID int64
Name string
DownloadCount int64 `xorm:"DEFAULT 0"`
Expand Down Expand Up @@ -72,11 +72,8 @@ func (a *Attachment) DownloadURL() string {
}

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

localPath := attach.LocalPath()
if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
Expand Down
29 changes: 29 additions & 0 deletions models/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,40 @@
package models

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUploadAttachment(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)

var fPath = "./attachment_test.go"
f, err := os.Open(fPath)
assert.NoError(t, err)
defer f.Close()

var buf = make([]byte, 1024)
n, err := f.Read(buf)
assert.NoError(t, err)
buf = buf[:n]

attach, err := NewAttachment(&Attachment{
UploaderID: user.ID,
Name: filepath.Base(fPath),
}, buf, f)
assert.NoError(t, err)

attachment, err := GetAttachmentByUUID(attach.UUID)
assert.NoError(t, err)
assert.EqualValues(t, user.ID, attachment.UploaderID)
assert.Equal(t, int64(0), attachment.DownloadCount)
}

func TestIncreaseDownloadCount(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

Expand Down
12 changes: 6 additions & 6 deletions routers/api/v1/repo/release_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,16 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
}

// Create a new attachment and save the file
attach, err := models.NewAttachment(filename, buf, file)
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
}
attach.ReleaseID = release.ID
if err := models.UpdateAttachment(attach); err != nil {
ctx.Error(500, "UpdateAttachment", err)
return
}

ctx.JSON(201, attach.APIFormat())
}

Expand Down
5 changes: 4 additions & 1 deletion routers/repo/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ func UploadAttachment(ctx *context.Context) {
return
}

attach, err := models.NewAttachment(header.Filename, buf, file)
attach, err := models.NewAttachment(&models.Attachment{
UploaderID: ctx.User.ID,
Name: header.Filename,
}, buf, file)
if err != nil {
ctx.Error(500, fmt.Sprintf("NewAttachment: %v", err))
return
Expand Down
5 changes: 4 additions & 1 deletion routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,12 @@ func RegisterRoutes(m *macaron.Macaron) {
return
}
})
m.Post("/attachments", repo.UploadAttachment)
}, ignSignIn)

m.Group("", func() {
m.Post("/attachments", repo.UploadAttachment)
}, reqSignIn)

m.Group("/:username", func() {
m.Get("/action/:action", user.Action)
}, reqSignIn)
Expand Down