admin.go 3.9 KB

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