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

Add Attachment API #3478

Merged
merged 35 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
25615b1
Add Attachment API
jonasfranz Feb 8, 2018
b2a780d
Add unit tests for new attachment functions
jonasfranz Feb 8, 2018
1407c1c
fix lint
Feb 9, 2018
80c6555
Update vendor.json
jonasfranz Feb 9, 2018
ea14f48
Merge remote-tracking branch 'origin/711-releases-api' into 711-relea…
jonasfranz Feb 9, 2018
95965d8
remove version of sdk
jonasfranz Feb 9, 2018
640ef5a
Fix unit tests
jonasfranz Feb 9, 2018
ad41258
Add CreateReleaseAttachment
jonasfranz Feb 9, 2018
ca954a7
Add filename query parameter for choosing another name for an attachment
jonasfranz Feb 9, 2018
151f594
Fix order of imports
jonasfranz Feb 10, 2018
030d4e8
Restricting updatable attachment columns
jonasfranz Feb 10, 2018
69539dc
Merge branch 'master' into 711-releases-api
jonasfranz Feb 10, 2018
da162ad
gofmt
jonasfranz Feb 10, 2018
e3aedec
Merge remote-tracking branch 'origin/711-releases-api' into 711-relea…
jonasfranz Feb 10, 2018
8c6ad76
Merge branch 'master' into 711-releases-api
jonasfranz Feb 11, 2018
f55249d
Merge branch 'master' into 711-releases-api
jonasfranz Feb 11, 2018
97ef292
Merge branch 'master' into 711-releases-api
jonasfranz Feb 15, 2018
14f27bb
Merge branch 'master' into 711-releases-api
jonasfranz Feb 16, 2018
ffed1a5
Merge branch 'master' into 711-releases-api
jonasfranz Feb 16, 2018
b4698a8
Merge branch 'master' into 711-releases-api
jonasfranz Feb 24, 2018
322c7a6
Update go-sdk
jonasfranz Feb 27, 2018
96acb4e
Update go-sdk
jonasfranz Feb 27, 2018
b696a63
Merge branch 'master' into 711-releases-api
jonasfranz Feb 27, 2018
e5c1b82
Updating go-sdk and regenerating swagger
jonasfranz Mar 2, 2018
62e93ab
Merge remote-tracking branch 'origin/711-releases-api' into 711-relea…
jonasfranz Mar 2, 2018
30258dc
Merge branch 'master' into 711-releases-api
jonasfranz Mar 2, 2018
08c778d
Add missing file of go-sdk
jonasfranz Mar 2, 2018
73b705e
Change origin of code.gitea.io/sdk to code.gitea.io/sdk
jonasfranz Mar 2, 2018
b2bdd60
Merge branch 'master' into 711-releases-api
jonasfranz Mar 2, 2018
d0d80aa
Merge branch 'master' into 711-releases-api
jonasfranz Mar 3, 2018
555521c
Merge branch 'master' into 711-releases-api
jonasfranz Mar 4, 2018
e317350
Update swagger
jonasfranz Mar 4, 2018
410e801
Merge branch 'master' into 711-releases-api
jonasfranz Mar 5, 2018
7a6c7dd
Update updateAttachment
jonasfranz Mar 5, 2018
3812137
Merge branch 'master' into 711-releases-api
jonasfranz Mar 5, 2018
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
67 changes: 65 additions & 2 deletions models/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated since it is used to generade the download link and not for API usage.

}

// NewAttachment creates a new attachment object.
func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
attach := &Attachment{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
29 changes: 29 additions & 0 deletions models/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is required because assets is only used at the release API. And the attachment API uses attachment and a change would be breaking.

}

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"})
}
7 changes: 6 additions & 1 deletion models/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -92,6 +96,7 @@ func (r *Release) APIFormat() *api.Release {
CreatedAt: r.CreatedUnix.AsTime(),
PublishedAt: r.CreatedUnix.AsTime(),
Publisher: r.Publisher.APIFormat(),
Attachments: assets,
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"less": "^2.7.2",
"less-plugin-clean-css": "^1.5.1"
}
}
}
Loading