aboutsummaryrefslogtreecommitdiffstats
path: root/models/migrations/v61.go
diff options
context:
space:
mode:
authorJonas Franz <info@jonasfranz.software>2018-03-31 03:10:44 +0200
committerBo-Yi Wu <appleboy.tw@gmail.com>2018-03-31 09:10:44 +0800
commit3e06490d38ee631029b020ccd408231841e5d1da (patch)
tree65235171481ac2a9536f5dd7336ae9a870d7c083 /models/migrations/v61.go
parentd877bf7e15ebd5d9a74e58f3999e3276e76fa15e (diff)
downloadgitea-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/migrations/v61.go')
-rw-r--r--models/migrations/v61.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/models/migrations/v61.go b/models/migrations/v61.go
new file mode 100644
index 0000000000..bcbc7553b8
--- /dev/null
+++ b/models/migrations/v61.go
@@ -0,0 +1,45 @@
+// Copyright 2018 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package migrations
+
+import (
+ "fmt"
+ "os"
+ "path"
+
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/setting"
+
+ "github.com/go-xorm/xorm"
+)
+
+func addSizeToAttachment(x *xorm.Engine) error {
+ type Attachment struct {
+ ID int64 `xorm:"pk autoincr"`
+ UUID string `xorm:"uuid UNIQUE"`
+ Size int64 `xorm:"DEFAULT 0"`
+ }
+ if err := x.Sync2(new(Attachment)); err != nil {
+ return fmt.Errorf("Sync2: %v", err)
+ }
+
+ attachments := make([]Attachment, 0, 100)
+ if err := x.Find(&attachments); err != nil {
+ return fmt.Errorf("query attachments: %v", err)
+ }
+ for _, attach := range attachments {
+ localPath := path.Join(setting.AttachmentPath, attach.UUID[0:1], attach.UUID[1:2], attach.UUID)
+ fi, err := os.Stat(localPath)
+ if err != nil {
+ log.Error(4, "calculate file size of attachment[UUID: %s]: %v", attach.UUID, err)
+ continue
+ }
+ attach.Size = fi.Size()
+ if _, err := x.ID(attach.ID).Cols("size").Update(attach); err != nil {
+ return fmt.Errorf("update size column: %v", err)
+ }
+ }
+ return nil
+}