save.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package web
  2. import (
  3. "backup-x/client"
  4. "backup-x/entity"
  5. "backup-x/util"
  6. "log"
  7. "net/http"
  8. "strconv"
  9. "strings"
  10. )
  11. // Save 保存
  12. func Save(writer http.ResponseWriter, request *http.Request) {
  13. oldConf, _ := entity.GetConfigCache()
  14. conf := &entity.Config{}
  15. conf.EncryptKey = oldConf.EncryptKey
  16. if conf.EncryptKey == "" {
  17. encryptKey, err := util.GenerateEncryptKey()
  18. if err != nil {
  19. writer.Write([]byte("生成Key失败"))
  20. return
  21. }
  22. conf.EncryptKey = encryptKey
  23. }
  24. // 覆盖以前的配置
  25. conf.Username = strings.TrimSpace(request.FormValue("Username"))
  26. conf.Password = request.FormValue("Password")
  27. if conf.Username == "" || conf.Password == "" {
  28. writer.Write([]byte("请输入登录用户名/密码"))
  29. return
  30. }
  31. if conf.Password != oldConf.Password {
  32. encryptPasswd, err := util.EncryptByEncryptKey(conf.EncryptKey, conf.Password)
  33. if err != nil {
  34. writer.Write([]byte("加密失败"))
  35. return
  36. }
  37. conf.Password = encryptPasswd
  38. }
  39. forms := request.PostForm
  40. for index, projectName := range forms["ProjectName"] {
  41. saveDays, _ := strconv.Atoi(forms["SaveDays"][index])
  42. saveDaysS3, _ := strconv.Atoi(forms["SaveDaysS3"][index])
  43. startTime, _ := strconv.Atoi(forms["StartTime"][index])
  44. period, _ := strconv.Atoi(forms["Period"][index])
  45. backupType, _ := strconv.Atoi(forms["BackupType"][index])
  46. enabled, _ := strconv.Atoi(forms["Enabled"][index])
  47. conf.BackupConfig = append(
  48. conf.BackupConfig,
  49. entity.BackupConfig{
  50. ProjectName: projectName,
  51. Command: forms["Command"][index],
  52. SaveDays: saveDays,
  53. SaveDaysS3: saveDaysS3,
  54. StartTime: startTime,
  55. Period: period,
  56. Pwd: forms["Pwd"][index],
  57. BackupType: backupType,
  58. Enabled: enabled,
  59. },
  60. )
  61. }
  62. for i := 0; i < len(conf.BackupConfig); i++ {
  63. if conf.BackupConfig[i].Pwd != "" &&
  64. (len(oldConf.BackupConfig) == 0 || conf.BackupConfig[i].Pwd != oldConf.BackupConfig[i].Pwd) {
  65. encryptPwd, err := util.EncryptByEncryptKey(conf.EncryptKey, conf.BackupConfig[i].Pwd)
  66. if err != nil {
  67. writer.Write([]byte("加密失败"))
  68. return
  69. }
  70. conf.BackupConfig[i].Pwd = encryptPwd
  71. }
  72. }
  73. // Webhook
  74. conf.WebhookURL = strings.TrimSpace(request.FormValue("WebhookURL"))
  75. conf.WebhookRequestBody = strings.TrimSpace(request.FormValue("WebhookRequestBody"))
  76. // S3
  77. conf.Endpoint = strings.TrimSpace(request.FormValue("Endpoint"))
  78. conf.AccessKey = strings.TrimSpace(request.FormValue("AccessKey"))
  79. conf.SecretKey = strings.TrimSpace(request.FormValue("SecretKey"))
  80. conf.BucketName = strings.TrimSpace(request.FormValue("BucketName"))
  81. if conf.SecretKey != "" && conf.SecretKey != oldConf.SecretKey {
  82. secretKey, err := util.EncryptByEncryptKey(conf.EncryptKey, conf.SecretKey)
  83. if err != nil {
  84. writer.Write([]byte("加密失败"))
  85. return
  86. }
  87. conf.SecretKey = secretKey
  88. }
  89. // 保存到用户目录
  90. err := conf.SaveConfig()
  91. // 没有错误
  92. if err == nil {
  93. conf.CreateBucketIfNotExist()
  94. if request.URL.Query().Get("backupAll") == "true" {
  95. go client.RunOnce()
  96. }
  97. if request.URL.Query().Get("backupIdx") != "" {
  98. idx, err := strconv.Atoi(request.URL.Query().Get("backupIdx"))
  99. if err == nil {
  100. go client.RunByIdx(idx)
  101. } else {
  102. log.Println("索引号不正确" + request.URL.Query().Get("backupIdx"))
  103. }
  104. }
  105. // 重新进行循环
  106. client.StopRunLoop()
  107. go client.RunLoop()
  108. }
  109. // 回写错误信息
  110. if err == nil {
  111. writer.Write([]byte("ok"))
  112. } else {
  113. writer.Write([]byte(err.Error()))
  114. }
  115. }