Skip to content

Commit 9a5e628

Browse files
jonasfranzappleboy
authored andcommitted
Add Attachment API (#3478)
* Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <[email protected]> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <[email protected]> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <[email protected]> * remove version of sdk Signed-off-by: Jonas Franz <[email protected]> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <[email protected]> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <[email protected]> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <[email protected]> * Fix order of imports Signed-off-by: Jonas Franz <[email protected]> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <[email protected]> * gofmt Signed-off-by: Jonas Franz <[email protected]> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <[email protected]> * Update go-sdk Signed-off-by: Jonas Franz <[email protected]> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <[email protected]> * Add missing file of go-sdk Signed-off-by: Jonas Franz <[email protected]> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <[email protected]> * Update swagger Signed-off-by: Jonas Franz <[email protected]> * Update updateAttachment
1 parent 69ea5e4 commit 9a5e628

30 files changed

+1043
-122
lines changed

models/attachment.go

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import (
1111
"os"
1212
"path"
1313

14-
gouuid "github.com/satori/go.uuid"
15-
1614
"code.gitea.io/gitea/modules/setting"
1715
"code.gitea.io/gitea/modules/util"
16+
api "code.gitea.io/sdk/gitea"
17+
18+
"github.com/go-xorm/xorm"
19+
gouuid "github.com/satori/go.uuid"
1820
)
1921

2022
// Attachment represent a attachment of issue/comment/release.
@@ -39,6 +41,20 @@ func (a *Attachment) IncreaseDownloadCount() error {
3941
return nil
4042
}
4143

44+
// APIFormat converts models.Attachment to api.Attachment
45+
func (a *Attachment) APIFormat() *api.Attachment {
46+
size, _ := a.Size()
47+
return &api.Attachment{
48+
ID: a.ID,
49+
Name: a.Name,
50+
Created: a.CreatedUnix.AsTime(),
51+
DownloadCount: a.DownloadCount,
52+
Size: size,
53+
UUID: a.UUID,
54+
DownloadURL: a.DownloadURL(),
55+
}
56+
}
57+
4258
// AttachmentLocalPath returns where attachment is stored in local file
4359
// system based on given UUID.
4460
func AttachmentLocalPath(uuid string) string {
@@ -50,6 +66,20 @@ func (a *Attachment) LocalPath() string {
5066
return AttachmentLocalPath(a.UUID)
5167
}
5268

69+
// Size returns the file's size of the attachment
70+
func (a *Attachment) Size() (int64, error) {
71+
fi, err := os.Stat(a.LocalPath())
72+
if err != nil {
73+
return 0, err
74+
}
75+
return fi.Size(), nil
76+
}
77+
78+
// DownloadURL returns the download url of the attached file
79+
func (a *Attachment) DownloadURL() string {
80+
return fmt.Sprintf("%sattachments/%s", setting.AppURL, a.UUID)
81+
}
82+
5383
// NewAttachment creates a new attachment object.
5484
func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
5585
attach := &Attachment{
@@ -81,6 +111,22 @@ func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment,
81111
return attach, nil
82112
}
83113

114+
// GetAttachmentByID returns attachment by given id
115+
func GetAttachmentByID(id int64) (*Attachment, error) {
116+
return getAttachmentByID(x, id)
117+
}
118+
119+
func getAttachmentByID(e Engine, id int64) (*Attachment, error) {
120+
attach := &Attachment{ID: id}
121+
122+
if has, err := e.Get(attach); err != nil {
123+
return nil, err
124+
} else if !has {
125+
return nil, ErrAttachmentNotExist{ID: id, UUID: ""}
126+
}
127+
return attach, nil
128+
}
129+
84130
func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
85131
attach := &Attachment{UUID: uuid}
86132
has, err := e.Get(attach)
@@ -180,3 +226,20 @@ func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
180226

181227
return DeleteAttachments(attachments, remove)
182228
}
229+
230+
// UpdateAttachment updates the given attachment in database
231+
func UpdateAttachment(atta *Attachment) error {
232+
return updateAttachment(x, atta)
233+
}
234+
235+
func updateAttachment(e Engine, atta *Attachment) error {
236+
var sess *xorm.Session
237+
if atta.ID != 0 && atta.UUID == "" {
238+
sess = e.ID(atta.ID)
239+
} else {
240+
// Use uuid only if id is not set and uuid is set
241+
sess = e.Where("uuid = ?", atta.UUID)
242+
}
243+
_, err := sess.Cols("name", "issue_id", "release_id", "comment_id", "download_count").Update(atta)
244+
return err
245+
}

models/attachment_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,32 @@ func TestDeleteAttachments(t *testing.T) {
5858
assert.True(t, IsErrAttachmentNotExist(err))
5959
assert.Nil(t, attachment)
6060
}
61+
62+
func TestGetAttachmentByID(t *testing.T) {
63+
assert.NoError(t, PrepareTestDatabase())
64+
65+
attach, err := GetAttachmentByID(1)
66+
assert.NoError(t, err)
67+
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
68+
}
69+
70+
func TestAttachment_DownloadURL(t *testing.T) {
71+
attach := &Attachment{
72+
UUID: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
73+
ID: 1,
74+
}
75+
assert.Equal(t, "https://try.gitea.io/attachments/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.DownloadURL())
76+
}
77+
78+
func TestUpdateAttachment(t *testing.T) {
79+
assert.NoError(t, PrepareTestDatabase())
80+
81+
attach, err := GetAttachmentByID(1)
82+
assert.NoError(t, err)
83+
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
84+
85+
attach.Name = "new_name"
86+
assert.NoError(t, UpdateAttachment(attach))
87+
88+
AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"})
89+
}

models/release.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (r *Release) loadAttributes(e Engine) error {
5353
return err
5454
}
5555
}
56-
return nil
56+
return GetReleaseAttachments(r)
5757
}
5858

5959
// LoadAttributes load repo and publisher attributes for a release
@@ -79,6 +79,10 @@ func (r *Release) TarURL() string {
7979

8080
// APIFormat convert a Release to api.Release
8181
func (r *Release) APIFormat() *api.Release {
82+
assets := make([]*api.Attachment, 0)
83+
for _, att := range r.Attachments {
84+
assets = append(assets, att.APIFormat())
85+
}
8286
return &api.Release{
8387
ID: r.ID,
8488
TagName: r.TagName,
@@ -92,6 +96,7 @@ func (r *Release) APIFormat() *api.Release {
9296
CreatedAt: r.CreatedUnix.AsTime(),
9397
PublishedAt: r.CreatedUnix.AsTime(),
9498
Publisher: r.Publisher.APIFormat(),
99+
Attachments: assets,
95100
}
96101
}
97102

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
"less": "^2.7.2",
55
"less-plugin-clean-css": "^1.5.1"
66
}
7-
}
7+
}

0 commit comments

Comments
 (0)