aboutsummaryrefslogtreecommitdiffstats
path: root/models/attachment.go
diff options
context:
space:
mode:
authorJonas Franz <info@jonasfranz.software>2018-03-06 02:22:16 +0100
committerBo-Yi Wu <appleboy.tw@gmail.com>2018-03-06 09:22:16 +0800
commit9a5e628a7e8bc8e7de6ed05f3621442de3388086 (patch)
tree07bd2cc6c22bae9ea976f5c23359d9b5cbf0bd8c /models/attachment.go
parent69ea5e438538863056af3bd70d182096b68f0d4a (diff)
downloadgitea-9a5e628a7e8bc8e7de6ed05f3621442de3388086.tar.gz
gitea-9a5e628a7e8bc8e7de6ed05f3621442de3388086.zip
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 <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
Diffstat (limited to 'models/attachment.go')
-rw-r--r--models/attachment.go67
1 files changed, 65 insertions, 2 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
+}