diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/attachment.go | 67 | ||||
-rw-r--r-- | models/attachment_test.go | 29 | ||||
-rw-r--r-- | models/release.go | 7 |
3 files changed, 100 insertions, 3 deletions
diff --git a/models/attachment.go b/models/attachment.go index acb1f0716c..ffe2eea80c 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -11,10 +11,12 @@ import ( "os" "path" - gouuid "github.com/satori/go.uuid" - "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" + api "code.gitea.io/sdk/gitea" + + "github.com/go-xorm/xorm" + gouuid "github.com/satori/go.uuid" ) // Attachment represent a attachment of issue/comment/release. @@ -39,6 +41,20 @@ func (a *Attachment) IncreaseDownloadCount() error { return nil } +// APIFormat converts models.Attachment to api.Attachment +func (a *Attachment) APIFormat() *api.Attachment { + size, _ := a.Size() + return &api.Attachment{ + ID: a.ID, + Name: a.Name, + Created: a.CreatedUnix.AsTime(), + DownloadCount: a.DownloadCount, + Size: size, + UUID: a.UUID, + DownloadURL: a.DownloadURL(), + } +} + // AttachmentLocalPath returns where attachment is stored in local file // system based on given UUID. func AttachmentLocalPath(uuid string) string { @@ -50,6 +66,20 @@ func (a *Attachment) LocalPath() string { return AttachmentLocalPath(a.UUID) } +// Size returns the file's size of the attachment +func (a *Attachment) Size() (int64, error) { + fi, err := os.Stat(a.LocalPath()) + if err != nil { + return 0, err + } + return fi.Size(), nil +} + +// DownloadURL returns the download url of the attached file +func (a *Attachment) DownloadURL() string { + return fmt.Sprintf("%sattachments/%s", setting.AppURL, a.UUID) +} + // NewAttachment creates a new attachment object. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) { attach := &Attachment{ @@ -81,6 +111,22 @@ func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, return attach, nil } +// GetAttachmentByID returns attachment by given id +func GetAttachmentByID(id int64) (*Attachment, error) { + return getAttachmentByID(x, id) +} + +func getAttachmentByID(e Engine, id int64) (*Attachment, error) { + attach := &Attachment{ID: id} + + if has, err := e.Get(attach); err != nil { + return nil, err + } else if !has { + return nil, ErrAttachmentNotExist{ID: id, UUID: ""} + } + return attach, nil +} + func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) { attach := &Attachment{UUID: uuid} has, err := e.Get(attach) @@ -180,3 +226,20 @@ func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) { return DeleteAttachments(attachments, remove) } + +// UpdateAttachment updates the given attachment in database +func UpdateAttachment(atta *Attachment) error { + return updateAttachment(x, atta) +} + +func updateAttachment(e Engine, atta *Attachment) error { + var sess *xorm.Session + if atta.ID != 0 && atta.UUID == "" { + sess = e.ID(atta.ID) + } else { + // Use uuid only if id is not set and uuid is set + sess = e.Where("uuid = ?", atta.UUID) + } + _, err := sess.Cols("name", "issue_id", "release_id", "comment_id", "download_count").Update(atta) + return err +} diff --git a/models/attachment_test.go b/models/attachment_test.go index d568e39431..be4baf3055 100644 --- a/models/attachment_test.go +++ b/models/attachment_test.go @@ -58,3 +58,32 @@ func TestDeleteAttachments(t *testing.T) { assert.True(t, IsErrAttachmentNotExist(err)) assert.Nil(t, attachment) } + +func TestGetAttachmentByID(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + attach, err := GetAttachmentByID(1) + assert.NoError(t, err) + assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID) +} + +func TestAttachment_DownloadURL(t *testing.T) { + attach := &Attachment{ + UUID: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", + ID: 1, + } + assert.Equal(t, "https://try.gitea.io/attachments/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.DownloadURL()) +} + +func TestUpdateAttachment(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + attach, err := GetAttachmentByID(1) + assert.NoError(t, err) + assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID) + + attach.Name = "new_name" + assert.NoError(t, UpdateAttachment(attach)) + + AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"}) +} diff --git a/models/release.go b/models/release.go index 66202615d2..586f494e7d 100644 --- a/models/release.go +++ b/models/release.go @@ -53,7 +53,7 @@ func (r *Release) loadAttributes(e Engine) error { return err } } - return nil + return GetReleaseAttachments(r) } // LoadAttributes load repo and publisher attributes for a release @@ -79,6 +79,10 @@ func (r *Release) TarURL() string { // APIFormat convert a Release to api.Release func (r *Release) APIFormat() *api.Release { + assets := make([]*api.Attachment, 0) + for _, att := range r.Attachments { + assets = append(assets, att.APIFormat()) + } return &api.Release{ ID: r.ID, TagName: r.TagName, @@ -92,6 +96,7 @@ func (r *Release) APIFormat() *api.Release { CreatedAt: r.CreatedUnix.AsTime(), PublishedAt: r.CreatedUnix.AsTime(), Publisher: r.Publisher.APIFormat(), + Attachments: assets, } } |