console_migrate.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // 用于迁移检测的旧键,该文件下个版本会删除
  2. package controller
  3. import (
  4. "encoding/json"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/model"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // MigrateConsoleSetting 迁移旧的控制台相关配置到 console_setting.*
  11. func MigrateConsoleSetting(c *gin.Context) {
  12. // 读取全部 option
  13. opts, err := model.AllOption()
  14. if err != nil {
  15. c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
  16. return
  17. }
  18. // 建立 map
  19. valMap := map[string]string{}
  20. for _, o := range opts {
  21. valMap[o.Key] = o.Value
  22. }
  23. // 处理 APIInfo
  24. if v := valMap["ApiInfo"]; v != "" {
  25. var arr []map[string]interface{}
  26. if err := json.Unmarshal([]byte(v), &arr); err == nil {
  27. if len(arr) > 50 {
  28. arr = arr[:50]
  29. }
  30. bytes, _ := json.Marshal(arr)
  31. model.UpdateOption("console_setting.api_info", string(bytes))
  32. }
  33. model.UpdateOption("ApiInfo", "")
  34. }
  35. // Announcements 直接搬
  36. if v := valMap["Announcements"]; v != "" {
  37. model.UpdateOption("console_setting.announcements", v)
  38. model.UpdateOption("Announcements", "")
  39. }
  40. // FAQ 转换
  41. if v := valMap["FAQ"]; v != "" {
  42. var arr []map[string]interface{}
  43. if err := json.Unmarshal([]byte(v), &arr); err == nil {
  44. out := []map[string]interface{}{}
  45. for _, item := range arr {
  46. q, _ := item["question"].(string)
  47. if q == "" {
  48. q, _ = item["title"].(string)
  49. }
  50. a, _ := item["answer"].(string)
  51. if a == "" {
  52. a, _ = item["content"].(string)
  53. }
  54. if q != "" && a != "" {
  55. out = append(out, map[string]interface{}{"question": q, "answer": a})
  56. }
  57. }
  58. if len(out) > 50 {
  59. out = out[:50]
  60. }
  61. bytes, _ := json.Marshal(out)
  62. model.UpdateOption("console_setting.faq", string(bytes))
  63. }
  64. model.UpdateOption("FAQ", "")
  65. }
  66. // Uptime Kuma 迁移到新的 groups 结构(console_setting.uptime_kuma_groups)
  67. url := valMap["UptimeKumaUrl"]
  68. slug := valMap["UptimeKumaSlug"]
  69. if url != "" && slug != "" {
  70. // 仅当同时存在 URL 与 Slug 时才进行迁移
  71. groups := []map[string]interface{}{
  72. {
  73. "id": 1,
  74. "categoryName": "old",
  75. "url": url,
  76. "slug": slug,
  77. "description": "",
  78. },
  79. }
  80. bytes, _ := json.Marshal(groups)
  81. model.UpdateOption("console_setting.uptime_kuma_groups", string(bytes))
  82. }
  83. // 清空旧键内容
  84. if url != "" {
  85. model.UpdateOption("UptimeKumaUrl", "")
  86. }
  87. if slug != "" {
  88. model.UpdateOption("UptimeKumaSlug", "")
  89. }
  90. // 删除旧键记录
  91. oldKeys := []string{"ApiInfo", "Announcements", "FAQ", "UptimeKumaUrl", "UptimeKumaSlug"}
  92. model.DB.Where("key IN ?", oldKeys).Delete(&model.Option{})
  93. // 重新加载 OptionMap
  94. model.InitOptionMap()
  95. common.SysLog("console setting migrated")
  96. c.JSON(http.StatusOK, gin.H{"success": true, "message": "migrated"})
  97. }