You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

badge.go 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package badge
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/context"
  5. "code.gitea.io/gitea/modules/log"
  6. "code.gitea.io/gitea/modules/setting"
  7. "code.gitea.io/gitea/routers/response"
  8. "code.gitea.io/gitea/services/badge"
  9. "errors"
  10. "github.com/unknwon/com"
  11. "net/http"
  12. "strings"
  13. )
  14. func GetCustomizeBadgeList(ctx *context.Context) {
  15. page := ctx.QueryInt("page")
  16. pageSize := 50
  17. n, r, err := badge.GetBadgeList(models.GetBadgeOpts{BadgeType: models.CustomizeBadge, LO: models.ListOptions{PageSize: pageSize, Page: page}})
  18. if err != nil {
  19. log.Error("GetCustomizeBadgeList error.%v", err)
  20. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  21. return
  22. }
  23. m := make(map[string]interface{})
  24. m["List"] = r
  25. m["Total"] = n
  26. m["PageSize"] = pageSize
  27. ctx.JSON(http.StatusOK, response.SuccessWithData(m))
  28. }
  29. func OperateBadge(ctx *context.Context, req models.BadgeOperateReq) {
  30. action := ctx.Params(":action")
  31. var err error
  32. switch action {
  33. case "edit":
  34. err = badge.EditBadge(req, ctx.User)
  35. case "new":
  36. err = badge.AddBadge(req, ctx.User)
  37. case "del":
  38. err = badge.DelBadge(req.ID, ctx.User)
  39. default:
  40. err = errors.New("action type error")
  41. }
  42. if err != nil {
  43. log.Error("OperateBadge error ,%v", err)
  44. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  45. return
  46. }
  47. ctx.JSON(http.StatusOK, response.Success())
  48. }
  49. func GetBadgeUsers(ctx *context.Context) {
  50. page := ctx.QueryInt("page")
  51. badgeId := ctx.QueryInt64("badge")
  52. pageSize := 50
  53. n, r, err := badge.GetBadgeUsers(badgeId, models.ListOptions{PageSize: pageSize, Page: page})
  54. if err != nil {
  55. log.Error("GetBadgeUsers error.%v", err)
  56. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  57. return
  58. }
  59. m := make(map[string]interface{})
  60. m["List"] = r
  61. m["Total"] = n
  62. m["PageSize"] = pageSize
  63. ctx.JSON(http.StatusOK, response.SuccessWithData(m))
  64. }
  65. func AddOperateBadgeUsers(ctx *context.Context, req models.AddBadgeUsersReq) {
  66. userStr := req.Users
  67. if userStr == "" {
  68. ctx.JSON(http.StatusOK, response.Success())
  69. return
  70. }
  71. userStr = strings.ReplaceAll(userStr, " ", "")
  72. userStr = strings.ReplaceAll(userStr, "\r", "")
  73. userNames := strings.Split(userStr, "\n")
  74. n, err := badge.AddBadgeUsers(req.BadgeId, userNames)
  75. if err != nil {
  76. log.Error("AddOperateBadgeUsers error.%v", err)
  77. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  78. return
  79. }
  80. m := make(map[string]interface{})
  81. m["Total"] = len(userNames)
  82. m["Success"] = n
  83. ctx.JSON(http.StatusOK, response.SuccessWithData(m))
  84. }
  85. func DelBadgeUsers(ctx *context.Context, req models.DelBadgeUserReq) {
  86. id := req.ID
  87. if id <= 0 {
  88. ctx.JSON(http.StatusOK, response.Success())
  89. return
  90. }
  91. err := badge.DelBadgeUser(id)
  92. if err != nil {
  93. log.Error("DelBadgeUsers error.%v", err)
  94. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  95. return
  96. }
  97. ctx.JSON(http.StatusOK, response.Success())
  98. }
  99. func UploadIcon(ctx *context.Context, form badge.IconUploadForm) {
  100. uploader := badge.NewIconUploader(badge.IconUploadConfig{
  101. FileMaxSize: setting.BadgeIconMaxFileSize,
  102. FileMaxWidth: setting.BadgeIconMaxWidth,
  103. FileMaxHeight: setting.BadgeIconMaxHeight,
  104. })
  105. iconName, err := uploader.Upload(form, ctx.User)
  106. if err != nil {
  107. log.Error("UploadIcon error.%v", err)
  108. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  109. return
  110. }
  111. m := make(map[string]string, 0)
  112. m["IconName"] = iconName
  113. ctx.JSON(http.StatusOK, response.SuccessWithData(m))
  114. }
  115. func GetIcon(ctx *context.Context) {
  116. hash := ctx.Params(":hash")
  117. if !com.IsFile(models.GetCustomIconByHash(hash)) {
  118. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  119. return
  120. }
  121. ctx.Redirect(setting.AppSubURL + "/icons/" + hash)
  122. }