admin.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package models
  7. import (
  8. "fmt"
  9. "gitote/gitote/pkg/setting"
  10. "gitote/gitote/pkg/tool"
  11. "os"
  12. "os/exec"
  13. "strings"
  14. "time"
  15. "github.com/Unknwon/com"
  16. raven "github.com/getsentry/raven-go"
  17. "github.com/go-xorm/xorm"
  18. log "gopkg.in/clog.v1"
  19. )
  20. type NoticeType int
  21. const (
  22. NOTICE_REPOSITORY NoticeType = iota + 1
  23. )
  24. // Notice represents a system notice for admin.
  25. type Notice struct {
  26. ID int64
  27. Type NoticeType
  28. Description string `xorm:"TEXT"`
  29. Created time.Time `xorm:"-" json:"-"`
  30. CreatedUnix int64
  31. }
  32. func (n *Notice) BeforeInsert() {
  33. n.CreatedUnix = time.Now().Unix()
  34. }
  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. // prevent panic if database connection is not available at this point
  48. if x == nil {
  49. return fmt.Errorf("Could not save notice due database connection not being available: %d %s", tp, desc)
  50. }
  51. n := &Notice{
  52. Type: tp,
  53. Description: desc,
  54. }
  55. _, err := x.Insert(n)
  56. return err
  57. }
  58. // CreateRepositoryNotice creates new system notice with type NOTICE_REPOSITORY.
  59. func CreateRepositoryNotice(desc string) error {
  60. return CreateNotice(NOTICE_REPOSITORY, desc)
  61. }
  62. // RemoveAllWithNotice removes all directories in given path and
  63. // creates a system notice when error occurs.
  64. func RemoveAllWithNotice(title, path string) {
  65. var err error
  66. // 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
  67. // this bug should be fixed on Go 1.7, so the workaround should be removed when Gitote don't support Go 1.6 anymore:
  68. // https://github.com/golang/go/commit/2ffb3e5d905b5622204d199128dec06cefd57790
  69. // Note: Windows complains when delete target does not exist, therefore we can skip deletion in such cases.
  70. if setting.IsWindows && com.IsExist(path) {
  71. // converting "/" to "\" in path on Windows
  72. path = strings.Replace(path, "/", "\\", -1)
  73. err = exec.Command("cmd", "/C", "rmdir", "/S", "/Q", path).Run()
  74. } else {
  75. err = os.RemoveAll(path)
  76. }
  77. if err != nil {
  78. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  79. log.Warn(desc)
  80. if err = CreateRepositoryNotice(desc); err != nil {
  81. raven.CaptureErrorAndWait(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 number of notices in given page.
  92. func Notices(page, pageSize int) ([]*Notice, error) {
  93. notices := make([]*Notice, 0, pageSize)
  94. return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices)
  95. }
  96. // DeleteNotice deletes a system notice by given ID.
  97. func DeleteNotice(id int64) error {
  98. _, err := x.Id(id).Delete(new(Notice))
  99. return err
  100. }
  101. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  102. func DeleteNotices(start, end int64) error {
  103. sess := x.Where("id >= ?", start)
  104. if end > 0 {
  105. sess.And("id <= ?", end)
  106. }
  107. _, err := sess.Delete(new(Notice))
  108. return err
  109. }
  110. // DeleteNoticesByIDs deletes notices by given IDs.
  111. func DeleteNoticesByIDs(ids []int64) error {
  112. if len(ids) == 0 {
  113. return nil
  114. }
  115. _, err := x.Where("id IN (" + strings.Join(tool.Int64sToStrings(ids), ",") + ")").Delete(new(Notice))
  116. return err
  117. }