admin.go 3.9 KB

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