-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add Attachment API #3478
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
Changes from 32 commits
25615b1
b2a780d
1407c1c
80c6555
ea14f48
95965d8
640ef5a
ad41258
ca954a7
151f594
030d4e8
69539dc
da162ad
e3aedec
8c6ad76
f55249d
97ef292
14f27bb
ffed1a5
b4698a8
322c7a6
96acb4e
b696a63
e5c1b82
62e93ab
30258dc
08c778d
73b705e
b2bdd60
d0d80aa
555521c
e317350
410e801
7a6c7dd
3812137
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{ | ||
|
@@ -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 == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe change to or
|
||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
"less": "^2.7.2", | ||
"less-plugin-clean-css": "^1.5.1" | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank line needed.