module_settings_act_thumbnails.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package modules
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strconv"
  6. "golang-fave/engine/wrapper"
  7. "golang-fave/utils"
  8. )
  9. func (this *Modules) RegisterAction_SettingsThumbnails() *Action {
  10. return this.newAction(AInfo{
  11. WantDB: true,
  12. Mount: "settings-thumbnails",
  13. WantAdmin: true,
  14. }, func(wrap *wrapper.Wrapper) {
  15. pf_shop_thumbnail_w_1 := wrap.R.FormValue("shop-thumbnail-w-1")
  16. pf_shop_thumbnail_h_1 := wrap.R.FormValue("shop-thumbnail-h-1")
  17. pf_shop_thumbnail_w_2 := wrap.R.FormValue("shop-thumbnail-w-2")
  18. pf_shop_thumbnail_h_2 := wrap.R.FormValue("shop-thumbnail-h-2")
  19. pf_shop_thumbnail_w_3 := wrap.R.FormValue("shop-thumbnail-w-3")
  20. pf_shop_thumbnail_h_3 := wrap.R.FormValue("shop-thumbnail-h-3")
  21. if _, err := strconv.Atoi(pf_shop_thumbnail_w_1); err != nil {
  22. wrap.MsgError(`Must be integer number`)
  23. return
  24. }
  25. if _, err := strconv.Atoi(pf_shop_thumbnail_h_1); err != nil {
  26. wrap.MsgError(`Must be integer number`)
  27. return
  28. }
  29. if _, err := strconv.Atoi(pf_shop_thumbnail_w_2); err != nil {
  30. wrap.MsgError(`Must be integer number`)
  31. return
  32. }
  33. if _, err := strconv.Atoi(pf_shop_thumbnail_h_2); err != nil {
  34. wrap.MsgError(`Must be integer number`)
  35. return
  36. }
  37. if _, err := strconv.Atoi(pf_shop_thumbnail_w_3); err != nil {
  38. wrap.MsgError(`Must be integer number`)
  39. return
  40. }
  41. if _, err := strconv.Atoi(pf_shop_thumbnail_h_3); err != nil {
  42. wrap.MsgError(`Must be integer number`)
  43. return
  44. }
  45. pfi_shop_thumbnail_w_1 := utils.StrToInt(pf_shop_thumbnail_w_1)
  46. pfi_shop_thumbnail_h_1 := utils.StrToInt(pf_shop_thumbnail_h_1)
  47. pfi_shop_thumbnail_w_2 := utils.StrToInt(pf_shop_thumbnail_w_2)
  48. pfi_shop_thumbnail_h_2 := utils.StrToInt(pf_shop_thumbnail_h_2)
  49. pfi_shop_thumbnail_w_3 := utils.StrToInt(pf_shop_thumbnail_w_3)
  50. pfi_shop_thumbnail_h_3 := utils.StrToInt(pf_shop_thumbnail_h_3)
  51. // Correct some values
  52. if pfi_shop_thumbnail_w_1 < 10 {
  53. pfi_shop_thumbnail_w_1 = 10
  54. }
  55. if pfi_shop_thumbnail_h_1 > 1000 {
  56. pfi_shop_thumbnail_h_1 = 1000
  57. }
  58. if pfi_shop_thumbnail_w_2 < 10 {
  59. pfi_shop_thumbnail_w_2 = 10
  60. }
  61. if pfi_shop_thumbnail_h_2 > 1000 {
  62. pfi_shop_thumbnail_h_2 = 1000
  63. }
  64. if pfi_shop_thumbnail_w_3 < 10 {
  65. pfi_shop_thumbnail_w_3 = 10
  66. }
  67. if pfi_shop_thumbnail_h_3 > 1000 {
  68. pfi_shop_thumbnail_h_3 = 1000
  69. }
  70. (*wrap.Config).Shop.Thumbnails.Thumbnail1[0] = pfi_shop_thumbnail_w_1
  71. (*wrap.Config).Shop.Thumbnails.Thumbnail1[1] = pfi_shop_thumbnail_h_1
  72. (*wrap.Config).Shop.Thumbnails.Thumbnail2[0] = pfi_shop_thumbnail_w_2
  73. (*wrap.Config).Shop.Thumbnails.Thumbnail2[1] = pfi_shop_thumbnail_h_2
  74. (*wrap.Config).Shop.Thumbnails.Thumbnail3[0] = pfi_shop_thumbnail_w_3
  75. (*wrap.Config).Shop.Thumbnails.Thumbnail3[1] = pfi_shop_thumbnail_h_3
  76. if err := wrap.ConfigSave(); err != nil {
  77. wrap.MsgError(err.Error())
  78. return
  79. }
  80. // Reset products images cache
  81. pattern := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + "*" + string(os.PathSeparator) + "thumb-*"
  82. if files, err := filepath.Glob(pattern); err == nil {
  83. for _, file := range files {
  84. if err := os.Remove(file); err != nil {
  85. wrap.LogError("[settings save] Thumbnail file (%s) delete error: %s", file, err.Error())
  86. }
  87. }
  88. }
  89. // Reload current page
  90. wrap.Write(`window.location.reload(false);`)
  91. })
  92. }