save.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. enabled, _ := strconv.Atoi(forms["Enabled"][index])
  46. conf.BackupConfig = append(
  47. conf.BackupConfig,
  48. entity.BackupConfig{
  49. ProjectName: projectName,
  50. Command: forms["Command"][index],
  51. SaveDays: saveDays,
  52. SaveDaysS3: saveDaysS3,
  53. StartTime: startTime,
  54. Period: period,
  55. Pwd: forms["Pwd"][index],
  56. BackupType: backupType,
  57. Enabled: enabled,
  58. },
  59. )
  60. }
  61. for i := 0; i < len(conf.BackupConfig); i++ {
  62. if conf.BackupConfig[i].Pwd != "" &&
  63. (len(oldConf.BackupConfig) == 0 || conf.BackupConfig[i].Pwd != oldConf.BackupConfig[i].Pwd) {
  64. encryptPwd, err := util.EncryptByEncryptKey(conf.EncryptKey, conf.BackupConfig[i].Pwd)
  65. if err != nil {
  66. writer.Write([]byte("加密失败"))
  67. return
  68. }
  69. conf.BackupConfig[i].Pwd = encryptPwd
  70. }
  71. }
  72. // Webhook
  73. conf.WebhookURL = strings.TrimSpace(request.FormValue("WebhookURL"))
  74. conf.WebhookRequestBody = strings.TrimSpace(request.FormValue("WebhookRequestBody"))
  75. // S3
  76. conf.Endpoint = strings.TrimSpace(request.FormValue("Endpoint"))
  77. conf.AccessKey = strings.TrimSpace(request.FormValue("AccessKey"))
  78. conf.SecretKey = strings.TrimSpace(request.FormValue("SecretKey"))
  79. conf.BucketName = strings.TrimSpace(request.FormValue("BucketName"))
  80. if conf.SecretKey != "" && conf.SecretKey != oldConf.SecretKey {
  81. secretKey, err := util.EncryptByEncryptKey(conf.EncryptKey, conf.SecretKey)
  82. if err != nil {
  83. writer.Write([]byte("加密失败"))
  84. return
  85. }
  86. conf.SecretKey = secretKey
  87. }
  88. // 保存到用户目录
  89. err := conf.SaveConfig()
  90. // 没有错误
  91. if err == nil {
  92. conf.CreateBucketIfNotExist()
  93. if request.URL.Query().Get("backupNow") == "true" {
  94. go client.RunOnce()
  95. }
  96. // 重新进行循环
  97. client.StopRunLoop()
  98. go client.RunLoop()
  99. }
  100. // 回写错误信息
  101. if err == nil {
  102. writer.Write([]byte("ok"))
  103. } else {
  104. writer.Write([]byte(err.Error()))
  105. }
  106. }