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.

143 lines
3.6 KiB

  1. // Copyright 2014 The Gogs 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 models
  5. import (
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. "time"
  11. "github.com/Unknwon/com"
  12. "github.com/go-xorm/xorm"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. )
  16. //NoticeType describes the notice type
  17. type NoticeType int
  18. const (
  19. //NoticeRepository type
  20. NoticeRepository NoticeType = iota + 1
  21. )
  22. // Notice represents a system notice for admin.
  23. type Notice struct {
  24. ID int64 `xorm:"pk autoincr"`
  25. Type NoticeType
  26. Description string `xorm:"TEXT"`
  27. Created time.Time `xorm:"-"`
  28. CreatedUnix int64 `xorm:"INDEX"`
  29. }
  30. // BeforeInsert is invoked from XORM before inserting an object of this type.
  31. func (n *Notice) BeforeInsert() {
  32. n.CreatedUnix = time.Now().Unix()
  33. }
  34. // AfterSet is invoked from XORM after setting the value of a field of this object.
  35. func (n *Notice) AfterSet(colName string, _ xorm.Cell) {
  36. switch colName {
  37. case "created_unix":
  38. n.Created = time.Unix(n.CreatedUnix, 0).Local()
  39. }
  40. }
  41. // TrStr returns a translation format string.
  42. func (n *Notice) TrStr() string {
  43. return "admin.notices.type_" + com.ToStr(n.Type)
  44. }
  45. // CreateNotice creates new system notice.
  46. func CreateNotice(tp NoticeType, desc string) error {
  47. return createNotice(x, tp, desc)
  48. }
  49. func createNotice(e Engine, tp NoticeType, desc string) error {
  50. n := &Notice{
  51. Type: tp,
  52. Description: desc,
  53. }
  54. _, err := e.Insert(n)
  55. return err
  56. }
  57. // CreateRepositoryNotice creates new system notice with type NoticeRepository.
  58. func CreateRepositoryNotice(desc string) error {
  59. return createNotice(x, NoticeRepository, desc)
  60. }
  61. // RemoveAllWithNotice removes all directories in given path and
  62. // creates a system notice when error occurs.
  63. func RemoveAllWithNotice(title, path string) {
  64. removeAllWithNotice(x, title, path)
  65. }
  66. func removeAllWithNotice(e Engine, title, path string) {
  67. var err error
  68. // workaround for Go not being able to remove read-only files/folders: https://github.com/golang/go/issues/9606
  69. // this bug should be fixed on Go 1.7, so the workaround should be removed when Gogs don't support Go 1.6 anymore:
  70. // https://github.com/golang/go/commit/2ffb3e5d905b5622204d199128dec06cefd57790
  71. if setting.IsWindows {
  72. // converting "/" to "\" in path on Windows
  73. path = strings.Replace(path, "/", "\\", -1)
  74. err = exec.Command("cmd", "/C", "rmdir", "/S", "/Q", path).Run()
  75. } else {
  76. err = os.RemoveAll(path)
  77. }
  78. if err != nil {
  79. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  80. log.Warn(desc)
  81. if err = createNotice(e, NoticeRepository, desc); err != nil {
  82. log.Error(4, "CreateRepositoryNotice: %v", err)
  83. }
  84. }
  85. }
  86. // CountNotices returns number of notices.
  87. func CountNotices() int64 {
  88. count, _ := x.Count(new(Notice))
  89. return count
  90. }
  91. // Notices returns notices in given page.
  92. func Notices(page, pageSize int) ([]*Notice, error) {
  93. notices := make([]*Notice, 0, pageSize)
  94. return notices, x.
  95. Limit(pageSize, (page-1)*pageSize).
  96. Desc("id").
  97. Find(&notices)
  98. }
  99. // DeleteNotice deletes a system notice by given ID.
  100. func DeleteNotice(id int64) error {
  101. _, err := x.Id(id).Delete(new(Notice))
  102. return err
  103. }
  104. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  105. func DeleteNotices(start, end int64) error {
  106. sess := x.Where("id >= ?", start)
  107. if end > 0 {
  108. sess.And("id <= ?", end)
  109. }
  110. _, err := sess.Delete(new(Notice))
  111. return err
  112. }
  113. // DeleteNoticesByIDs deletes notices by given IDs.
  114. func DeleteNoticesByIDs(ids []int64) error {
  115. if len(ids) == 0 {
  116. return nil
  117. }
  118. _, err := x.
  119. In("id", ids).
  120. Delete(new(Notice))
  121. return err
  122. }