save.go 3.1 KB

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