Skip to content

Commit 3e06490

Browse files
jonasfranzappleboy
authored andcommitted
Add Size column to attachment (#3734)
* Add size column to attachment Migrate attachments by calculating file sizes Signed-off-by: Jonas Franz <[email protected]> * Calculate attachment size on creation Signed-off-by: Jonas Franz <[email protected]> * Log error instead of returning error Signed-off-by: Jonas Franz <[email protected]>
1 parent d877bf7 commit 3e06490

File tree

4 files changed

+57
-23
lines changed

4 files changed

+57
-23
lines changed

models/attachment.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"os"
1212
"path"
1313

14-
"code.gitea.io/gitea/modules/log"
1514
"code.gitea.io/gitea/modules/setting"
1615
"code.gitea.io/gitea/modules/util"
1716
api "code.gitea.io/sdk/gitea"
@@ -29,6 +28,7 @@ type Attachment struct {
2928
CommentID int64
3029
Name string
3130
DownloadCount int64 `xorm:"DEFAULT 0"`
31+
Size int64 `xorm:"DEFAULT 0"`
3232
CreatedUnix util.TimeStamp `xorm:"created"`
3333
}
3434

@@ -44,13 +44,12 @@ func (a *Attachment) IncreaseDownloadCount() error {
4444

4545
// APIFormat converts models.Attachment to api.Attachment
4646
func (a *Attachment) APIFormat() *api.Attachment {
47-
size, _ := a.Size()
4847
return &api.Attachment{
4948
ID: a.ID,
5049
Name: a.Name,
5150
Created: a.CreatedUnix.AsTime(),
5251
DownloadCount: a.DownloadCount,
53-
Size: size,
52+
Size: a.Size,
5453
UUID: a.UUID,
5554
DownloadURL: a.DownloadURL(),
5655
}
@@ -67,25 +66,6 @@ func (a *Attachment) LocalPath() string {
6766
return AttachmentLocalPath(a.UUID)
6867
}
6968

70-
// Size returns the file's size of the attachment
71-
func (a *Attachment) Size() (int64, error) {
72-
fi, err := os.Stat(a.LocalPath())
73-
if err != nil {
74-
return 0, err
75-
}
76-
return fi.Size(), nil
77-
}
78-
79-
// MustSize returns the result of a.Size() by ignoring errors
80-
func (a *Attachment) MustSize() int64 {
81-
size, err := a.Size()
82-
if err != nil {
83-
log.Error(4, "size: %v", err)
84-
return 0
85-
}
86-
return size
87-
}
88-
8969
// DownloadURL returns the download url of the attached file
9070
func (a *Attachment) DownloadURL() string {
9171
return fmt.Sprintf("%sattachments/%s", setting.AppURL, a.UUID)
@@ -115,6 +95,13 @@ func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment,
11595
return nil, fmt.Errorf("Copy: %v", err)
11696
}
11797

98+
// Update file size
99+
var fi os.FileInfo
100+
if fi, err = fw.Stat(); err != nil {
101+
return nil, fmt.Errorf("file size: %v", err)
102+
}
103+
attach.Size = fi.Size()
104+
118105
if _, err := x.Insert(attach); err != nil {
119106
return nil, err
120107
}

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ var migrations = []Migration{
174174
NewMigration("add merge whitelist for protected branches", addProtectedBranchMergeWhitelist),
175175
// v60 -> v61
176176
NewMigration("add is_fsck_enabled column for repos", addFsckEnabledToRepo),
177+
// v61 -> v62
178+
NewMigration("add size column for attachments", addSizeToAttachment),
177179
}
178180

179181
// Migrate database to current version

models/migrations/v61.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
"os"
10+
"path"
11+
12+
"code.gitea.io/gitea/modules/log"
13+
"code.gitea.io/gitea/modules/setting"
14+
15+
"github.com/go-xorm/xorm"
16+
)
17+
18+
func addSizeToAttachment(x *xorm.Engine) error {
19+
type Attachment struct {
20+
ID int64 `xorm:"pk autoincr"`
21+
UUID string `xorm:"uuid UNIQUE"`
22+
Size int64 `xorm:"DEFAULT 0"`
23+
}
24+
if err := x.Sync2(new(Attachment)); err != nil {
25+
return fmt.Errorf("Sync2: %v", err)
26+
}
27+
28+
attachments := make([]Attachment, 0, 100)
29+
if err := x.Find(&attachments); err != nil {
30+
return fmt.Errorf("query attachments: %v", err)
31+
}
32+
for _, attach := range attachments {
33+
localPath := path.Join(setting.AttachmentPath, attach.UUID[0:1], attach.UUID[1:2], attach.UUID)
34+
fi, err := os.Stat(localPath)
35+
if err != nil {
36+
log.Error(4, "calculate file size of attachment[UUID: %s]: %v", attach.UUID, err)
37+
continue
38+
}
39+
attach.Size = fi.Size()
40+
if _, err := x.ID(attach.ID).Cols("size").Update(attach); err != nil {
41+
return fmt.Errorf("update size column: %v", err)
42+
}
43+
}
44+
return nil
45+
}

templates/repo/release/list.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
<li>
8080
<a target="_blank" rel="noopener" href="{{AppSubUrl}}/attachments/{{.UUID}}">
8181
<strong><span class="ui image octicon octicon-package" title='{{.Name}}'></span> {{.Name}}</strong>
82-
<span class="ui text grey right">{{.MustSize | FileSize}}</span>
82+
<span class="ui text grey right">{{.Size | FileSize}}</span>
8383
</a>
8484
</li>
8585
{{end}}

0 commit comments

Comments
 (0)