gitea源码

v112.go 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_11
  4. import (
  5. "path/filepath"
  6. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/modules/setting"
  8. "code.gitea.io/gitea/modules/util"
  9. "xorm.io/builder"
  10. "xorm.io/xorm"
  11. )
  12. func RemoveAttachmentMissedRepo(x *xorm.Engine) error {
  13. type Attachment struct {
  14. UUID string `xorm:"uuid"`
  15. }
  16. var start int
  17. attachments := make([]*Attachment, 0, 50)
  18. for {
  19. err := x.Select("uuid").Where(builder.NotIn("release_id", builder.Select("id").From("`release`"))).
  20. And("release_id > 0").
  21. OrderBy("id").Limit(50, start).Find(&attachments)
  22. if err != nil {
  23. return err
  24. }
  25. for i := 0; i < len(attachments); i++ {
  26. uuid := attachments[i].UUID
  27. if err = util.RemoveAll(filepath.Join(setting.Attachment.Storage.Path, uuid[0:1], uuid[1:2], uuid)); err != nil {
  28. log.Warn("Unable to remove attachment file by UUID %s: %v", uuid, err)
  29. }
  30. }
  31. if len(attachments) < 50 {
  32. break
  33. }
  34. start += 50
  35. attachments = attachments[:0]
  36. }
  37. _, err := x.Exec("DELETE FROM attachment WHERE release_id > 0 AND release_id NOT IN (SELECT id FROM `release`)")
  38. return err
  39. }