diff options
author | Jonas Franz <info@jonasfranz.software> | 2018-03-31 03:10:44 +0200 |
---|---|---|
committer | Bo-Yi Wu <appleboy.tw@gmail.com> | 2018-03-31 09:10:44 +0800 |
commit | 3e06490d38ee631029b020ccd408231841e5d1da (patch) | |
tree | 65235171481ac2a9536f5dd7336ae9a870d7c083 /models/attachment.go | |
parent | d877bf7e15ebd5d9a74e58f3999e3276e76fa15e (diff) | |
download | gitea-3e06490d38ee631029b020ccd408231841e5d1da.tar.gz gitea-3e06490d38ee631029b020ccd408231841e5d1da.zip |
Add Size column to attachment (#3734)
* Add size column to attachment
Migrate attachments by calculating file sizes
Signed-off-by: Jonas Franz <info@jonasfranz.software>
* Calculate attachment size on creation
Signed-off-by: Jonas Franz <info@jonasfranz.software>
* Log error instead of returning error
Signed-off-by: Jonas Franz <info@jonasfranz.software>
Diffstat (limited to 'models/attachment.go')
-rw-r--r-- | models/attachment.go | 31 |
1 files changed, 9 insertions, 22 deletions
diff --git a/models/attachment.go b/models/attachment.go index 47c76990bc..4a5101b385 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -11,7 +11,6 @@ import ( "os" "path" - "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" api "code.gitea.io/sdk/gitea" @@ -29,6 +28,7 @@ type Attachment struct { CommentID int64 Name string DownloadCount int64 `xorm:"DEFAULT 0"` + Size int64 `xorm:"DEFAULT 0"` CreatedUnix util.TimeStamp `xorm:"created"` } @@ -44,13 +44,12 @@ func (a *Attachment) IncreaseDownloadCount() error { // 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, + Size: a.Size, UUID: a.UUID, DownloadURL: a.DownloadURL(), } @@ -67,25 +66,6 @@ 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 -} - -// MustSize returns the result of a.Size() by ignoring errors -func (a *Attachment) MustSize() int64 { - size, err := a.Size() - if err != nil { - log.Error(4, "size: %v", err) - return 0 - } - return size -} - // DownloadURL returns the download url of the attached file func (a *Attachment) DownloadURL() string { return fmt.Sprintf("%sattachments/%s", setting.AppURL, a.UUID) @@ -115,6 +95,13 @@ func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, return nil, fmt.Errorf("Copy: %v", err) } + // Update file size + var fi os.FileInfo + if fi, err = fw.Stat(); err != nil { + return nil, fmt.Errorf("file size: %v", err) + } + attach.Size = fi.Size() + if _, err := x.Insert(attach); err != nil { return nil, err } |