Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

admin.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. type NoticeType int
  17. const (
  18. NoticeRepository NoticeType = iota + 1
  19. )
  20. // Notice represents a system notice for admin.
  21. type Notice struct {
  22. ID int64 `xorm:"pk autoincr"`
  23. Type NoticeType
  24. Description string `xorm:"TEXT"`
  25. Created time.Time `xorm:"-"`
  26. CreatedUnix int64
  27. }
  28. func (n *Notice) BeforeInsert() {
  29. n.CreatedUnix = time.Now().Unix()
  30. }
  31. func (n *Notice) AfterSet(colName string, _ xorm.Cell) {
  32. switch colName {
  33. case "created_unix":
  34. n.Created = time.Unix(n.CreatedUnix, 0).Local()
  35. }
  36. }
  37. // TrStr returns a translation format string.
  38. func (n *Notice) TrStr() string {
  39. return "admin.notices.type_" + com.ToStr(n.Type)
  40. }
  41. // CreateNotice creates new system notice.
  42. func CreateNotice(tp NoticeType, desc string) error {
  43. // prevent panic if database connection is not available at this point
  44. if x == nil {
  45. return fmt.Errorf("Could not save notice due database connection not being available: %d %s", tp, desc)
  46. }
  47. n := &Notice{
  48. Type: tp,
  49. Description: desc,
  50. }
  51. _, err := x.Insert(n)
  52. return err
  53. }
  54. // CreateRepositoryNotice creates new system notice with type NoticeRepository.
  55. func CreateRepositoryNotice(desc string) error {
  56. return CreateNotice(NoticeRepository, desc)
  57. }
  58. // RemoveAllWithNotice removes all directories in given path and
  59. // creates a system notice when error occurs.
  60. func RemoveAllWithNotice(title, path string) {
  61. var err error
  62. // workaround for Go not being able to remove read-only files/folders: https://github.com/golang/go/issues/9606
  63. // this bug should be fixed on Go 1.7, so the workaround should be removed when Gogs don't support Go 1.6 anymore:
  64. // https://github.com/golang/go/commit/2ffb3e5d905b5622204d199128dec06cefd57790
  65. if setting.IsWindows {
  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. log.Error(4, "CreateRepositoryNotice: %v", err)
  77. }
  78. }
  79. }
  80. // CountNotices returns number of notices.
  81. func CountNotices() int64 {
  82. count, _ := x.Count(new(Notice))
  83. return count
  84. }
  85. // Notices returns number of notices in given page.
  86. func Notices(page, pageSize int) ([]*Notice, error) {
  87. notices := make([]*Notice, 0, pageSize)
  88. return notices, x.
  89. Limit(pageSize, (page-1)*pageSize).
  90. Desc("id").
  91. Find(&notices)
  92. }
  93. // DeleteNotice deletes a system notice by given ID.
  94. func DeleteNotice(id int64) error {
  95. _, err := x.Id(id).Delete(new(Notice))
  96. return err
  97. }
  98. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  99. func DeleteNotices(start, end int64) error {
  100. sess := x.Where("id >= ?", start)
  101. if end > 0 {
  102. sess.And("id <= ?", end)
  103. }
  104. _, err := sess.Delete(new(Notice))
  105. return err
  106. }
  107. // DeleteNoticesByIDs deletes notices by given IDs.
  108. func DeleteNoticesByIDs(ids []int64) error {
  109. if len(ids) == 0 {
  110. return nil
  111. }
  112. _, err := x.
  113. In("id", ids).
  114. Delete(new(Notice))
  115. return err
  116. }