admin.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package models
  2. import (
  3. "fmt"
  4. "gitote/gitote/pkg/setting"
  5. "gitote/gitote/pkg/tool"
  6. "os"
  7. "os/exec"
  8. "strings"
  9. "time"
  10. "github.com/Unknwon/com"
  11. raven "github.com/getsentry/raven-go"
  12. "github.com/go-xorm/xorm"
  13. log "gopkg.in/clog.v1"
  14. )
  15. type NoticeType int
  16. const (
  17. NOTICE_REPOSITORY NoticeType = iota + 1
  18. )
  19. // Notice represents a system notice for admin.
  20. type Notice struct {
  21. ID int64
  22. Type NoticeType
  23. Description string `xorm:"TEXT"`
  24. Created time.Time `xorm:"-" json:"-"`
  25. CreatedUnix int64
  26. }
  27. func (n *Notice) BeforeInsert() {
  28. n.CreatedUnix = time.Now().Unix()
  29. }
  30. func (n *Notice) AfterSet(colName string, _ xorm.Cell) {
  31. switch colName {
  32. case "created_unix":
  33. n.Created = time.Unix(n.CreatedUnix, 0).Local()
  34. }
  35. }
  36. // TrStr returns a translation format string.
  37. func (n *Notice) TrStr() string {
  38. return "admin.notices.type_" + com.ToStr(n.Type)
  39. }
  40. // CreateNotice creates new system notice.
  41. func CreateNotice(tp NoticeType, desc string) error {
  42. // prevent panic if database connection is not available at this point
  43. if x == nil {
  44. return fmt.Errorf("Could not save notice due database connection not being available: %d %s", tp, desc)
  45. }
  46. n := &Notice{
  47. Type: tp,
  48. Description: desc,
  49. }
  50. _, err := x.Insert(n)
  51. return err
  52. }
  53. // CreateRepositoryNotice creates new system notice with type NOTICE_REPOSITORY.
  54. func CreateRepositoryNotice(desc string) error {
  55. return CreateNotice(NOTICE_REPOSITORY, desc)
  56. }
  57. // RemoveAllWithNotice removes all directories in given path and
  58. // creates a system notice when error occurs.
  59. func RemoveAllWithNotice(title, path string) {
  60. var err error
  61. // LEGACY [Go 1.7, 0.12]: workaround for Go not being able to remove read-only files/folders: https://github.com/golang/go/issues/9606
  62. // this bug should be fixed on Go 1.7, so the workaround should be removed when Gitote don't support Go 1.6 anymore:
  63. // https://github.com/golang/go/commit/2ffb3e5d905b5622204d199128dec06cefd57790
  64. // Note: Windows complains when delete target does not exist, therefore we can skip deletion in such cases.
  65. if setting.IsWindows && com.IsExist(path) {
  66. // converting "/" to "\" in path on Windows
  67. path = strings.Replace(path, "/", "\\", -1)
  68. err = exec.Command("cmd", "/C", "rmdir", "/S", "/Q", path).Run()
  69. } else {
  70. err = os.RemoveAll(path)
  71. }
  72. if err != nil {
  73. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  74. log.Warn(desc)
  75. if err = CreateRepositoryNotice(desc); err != nil {
  76. raven.CaptureErrorAndWait(err, nil)
  77. log.Error(4, "CreateRepositoryNotice: %v", err)
  78. }
  79. }
  80. }
  81. // CountNotices returns number of notices.
  82. func CountNotices() int64 {
  83. count, _ := x.Count(new(Notice))
  84. return count
  85. }
  86. // Notices returns number of notices in given page.
  87. func Notices(page, pageSize int) ([]*Notice, error) {
  88. notices := make([]*Notice, 0, pageSize)
  89. return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices)
  90. }
  91. // DeleteNotice deletes a system notice by given ID.
  92. func DeleteNotice(id int64) error {
  93. _, err := x.Id(id).Delete(new(Notice))
  94. return err
  95. }
  96. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  97. func DeleteNotices(start, end int64) error {
  98. sess := x.Where("id >= ?", start)
  99. if end > 0 {
  100. sess.And("id <= ?", end)
  101. }
  102. _, err := sess.Delete(new(Notice))
  103. return err
  104. }
  105. // DeleteNoticesByIDs deletes notices by given IDs.
  106. func DeleteNoticesByIDs(ids []int64) error {
  107. if len(ids) == 0 {
  108. return nil
  109. }
  110. _, err := x.Where("id IN (" + strings.Join(tool.Int64sToStrings(ids), ",") + ")").Delete(new(Notice))
  111. return err
  112. }