save.go 2.9 KB

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