image.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "golang-fave/engine/wrapper/config"
  10. "golang-fave/utils"
  11. "github.com/disintegration/imaging"
  12. )
  13. func image_generate(width, height int, resize bool, fsrc, fdst string) {
  14. src, err := imaging.Open(fsrc)
  15. if err == nil {
  16. if !resize {
  17. src = imaging.Fill(src, width, height, imaging.Center, imaging.Lanczos)
  18. } else {
  19. src = imaging.Fit(src, width, height, imaging.Lanczos)
  20. }
  21. if err := imaging.Save(src, fdst); err != nil {
  22. fmt.Printf("Image generation error: %v\n", err)
  23. }
  24. }
  25. }
  26. func image_create(www, src, dst, typ string, conf *config.Config) {
  27. width := (*conf).Shop.Thumbnails.Thumbnail0[0]
  28. height := (*conf).Shop.Thumbnails.Thumbnail0[1]
  29. resize := false
  30. if typ == "thumb-1" {
  31. width = (*conf).Shop.Thumbnails.Thumbnail1[0]
  32. height = (*conf).Shop.Thumbnails.Thumbnail1[1]
  33. if (*conf).Shop.Thumbnails.Thumbnail1[2] == 1 {
  34. resize = true
  35. }
  36. } else if typ == "thumb-2" {
  37. width = (*conf).Shop.Thumbnails.Thumbnail2[0]
  38. height = (*conf).Shop.Thumbnails.Thumbnail2[1]
  39. if (*conf).Shop.Thumbnails.Thumbnail2[2] == 1 {
  40. resize = true
  41. }
  42. } else if typ == "thumb-3" {
  43. width = (*conf).Shop.Thumbnails.Thumbnail3[0]
  44. height = (*conf).Shop.Thumbnails.Thumbnail3[1]
  45. if (*conf).Shop.Thumbnails.Thumbnail3[2] == 1 {
  46. resize = true
  47. }
  48. } else if typ == "thumb-full" {
  49. width = (*conf).Shop.Thumbnails.ThumbnailFull[0]
  50. height = (*conf).Shop.Thumbnails.ThumbnailFull[1]
  51. if (*conf).Shop.Thumbnails.ThumbnailFull[2] == 1 {
  52. resize = true
  53. }
  54. }
  55. image_generate(width, height, resize, src, dst)
  56. }
  57. func image_detect(www, file string, conf *config.Config) {
  58. index := strings.LastIndex(file, string(os.PathSeparator))
  59. if index != -1 {
  60. file_name := file[index+1:]
  61. if !strings.HasPrefix(file_name, "thumb-") {
  62. file_thumb_0 := file[:index+1] + "thumb-0-" + file_name
  63. file_thumb_1 := file[:index+1] + "thumb-1-" + file_name
  64. file_thumb_2 := file[:index+1] + "thumb-2-" + file_name
  65. file_thumb_3 := file[:index+1] + "thumb-3-" + file_name
  66. file_thumb_full := file[:index+1] + "thumb-full-" + file_name
  67. if !utils.IsFileExists(file_thumb_0) {
  68. image_create(www, file, file_thumb_0, "thumb-0", conf)
  69. }
  70. if !utils.IsFileExists(file_thumb_1) {
  71. image_create(www, file, file_thumb_1, "thumb-1", conf)
  72. }
  73. if !utils.IsFileExists(file_thumb_2) {
  74. image_create(www, file, file_thumb_2, "thumb-2", conf)
  75. }
  76. if !utils.IsFileExists(file_thumb_3) {
  77. image_create(www, file, file_thumb_3, "thumb-3", conf)
  78. }
  79. if !utils.IsFileExists(file_thumb_full) {
  80. image_create(www, file, file_thumb_full, "thumb-full", conf)
  81. }
  82. }
  83. }
  84. }
  85. func image_loop(www_dir string, stop chan bool) {
  86. if dirs, err := ioutil.ReadDir(www_dir); err == nil {
  87. for _, dir := range dirs {
  88. trigger := strings.Join([]string{www_dir, dir.Name(), "tmp", "trigger.img.run"}, string(os.PathSeparator))
  89. if utils.IsFileExists(trigger) {
  90. conf := config.ConfigNew()
  91. if err := conf.ConfigRead(strings.Join([]string{www_dir, dir.Name(), "config", "config.json"}, string(os.PathSeparator))); err == nil {
  92. target_dir := strings.Join([]string{www_dir, dir.Name(), "htdocs", "products", "images"}, string(os.PathSeparator))
  93. if utils.IsDirExists(target_dir) {
  94. pattern := target_dir + string(os.PathSeparator) + "*" + string(os.PathSeparator) + "*.*"
  95. if files, err := filepath.Glob(pattern); err == nil {
  96. for _, file := range files {
  97. select {
  98. case <-stop:
  99. break
  100. default:
  101. image_detect(www_dir, file, conf)
  102. }
  103. }
  104. }
  105. }
  106. }
  107. os.Remove(trigger)
  108. }
  109. }
  110. }
  111. }
  112. func image_start(www_dir string) (chan bool, chan bool) {
  113. ch := make(chan bool)
  114. stop := make(chan bool)
  115. // Run at start
  116. image_loop(www_dir, stop)
  117. go func() {
  118. for {
  119. select {
  120. case <-time.After(2 * time.Second):
  121. // Run every 2 seconds
  122. image_loop(www_dir, stop)
  123. case <-ch:
  124. ch <- true
  125. return
  126. }
  127. }
  128. }()
  129. return ch, stop
  130. }
  131. func image_stop(ch, stop chan bool) {
  132. for {
  133. select {
  134. case stop <- true:
  135. case ch <- true:
  136. <-ch
  137. return
  138. case <-time.After(3 * time.Second):
  139. fmt.Println("Image error: force exit by timeout after 3 seconds")
  140. return
  141. }
  142. }
  143. }