You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

  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. package migrations
  5. import (
  6. "fmt"
  7. "os"
  8. "path"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/go-xorm/xorm"
  12. )
  13. func addSizeToAttachment(x *xorm.Engine) error {
  14. type Attachment struct {
  15. ID int64 `xorm:"pk autoincr"`
  16. UUID string `xorm:"uuid UNIQUE"`
  17. Size int64 `xorm:"DEFAULT 0"`
  18. }
  19. if err := x.Sync2(new(Attachment)); err != nil {
  20. return fmt.Errorf("Sync2: %v", err)
  21. }
  22. attachments := make([]Attachment, 0, 100)
  23. if err := x.Find(&attachments); err != nil {
  24. return fmt.Errorf("query attachments: %v", err)
  25. }
  26. for _, attach := range attachments {
  27. localPath := path.Join(setting.AttachmentPath, attach.UUID[0:1], attach.UUID[1:2], attach.UUID)
  28. fi, err := os.Stat(localPath)
  29. if err != nil {
  30. log.Error(4, "calculate file size of attachment[UUID: %s]: %v", attach.UUID, err)
  31. continue
  32. }
  33. attach.Size = fi.Size()
  34. if _, err := x.ID(attach.ID).Cols("size").Update(attach); err != nil {
  35. return fmt.Errorf("update size column: %v", err)
  36. }
  37. }
  38. return nil
  39. }