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.

admin.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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
  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. // 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 NoticeRepository.
  59. func CreateRepositoryNotice(desc string) error {
  60. return CreateNotice(NoticeRepository, 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. // 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 Gogs don't support Go 1.6 anymore:
  68. // https://github.com/golang/go/commit/2ffb3e5d905b5622204d199128dec06cefd57790
  69. if setting.IsWindows {
  70. // converting "/" to "\" in path on Windows
  71. path = strings.Replace(path, "/", "\\", -1)
  72. err = exec.Command("cmd", "/C", "rmdir", "/S", "/Q", path).Run()
  73. } else {
  74. err = os.RemoveAll(path)
  75. }
  76. if err != nil {
  77. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  78. log.Warn(desc)
  79. if err = CreateRepositoryNotice(desc); err != nil {
  80. log.Error(4, "CreateRepositoryNotice: %v", err)
  81. }
  82. }
  83. }
  84. // CountNotices returns number of notices.
  85. func CountNotices() int64 {
  86. count, _ := x.Count(new(Notice))
  87. return count
  88. }
  89. // Notices returns number of notices in given page.
  90. func Notices(page, pageSize int) ([]*Notice, error) {
  91. notices := make([]*Notice, 0, pageSize)
  92. return notices, x.
  93. Limit(pageSize, (page-1)*pageSize).
  94. Desc("id").
  95. Find(&notices)
  96. }
  97. // DeleteNotice deletes a system notice by given ID.
  98. func DeleteNotice(id int64) error {
  99. _, err := x.Id(id).Delete(new(Notice))
  100. return err
  101. }
  102. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  103. func DeleteNotices(start, end int64) error {
  104. sess := x.Where("id >= ?", start)
  105. if end > 0 {
  106. sess.And("id <= ?", end)
  107. }
  108. _, err := sess.Delete(new(Notice))
  109. return err
  110. }
  111. // DeleteNoticesByIDs deletes notices by given IDs.
  112. func DeleteNoticesByIDs(ids []int64) error {
  113. if len(ids) == 0 {
  114. return nil
  115. }
  116. _, err := x.
  117. In("id", ids).
  118. Delete(new(Notice))
  119. return err
  120. }