image.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. return
  70. }
  71. if !utils.IsFileExists(file_thumb_1) {
  72. image_create(www, file, file_thumb_1, "thumb-1", conf)
  73. return
  74. }
  75. if !utils.IsFileExists(file_thumb_2) {
  76. image_create(www, file, file_thumb_2, "thumb-2", conf)
  77. return
  78. }
  79. if !utils.IsFileExists(file_thumb_3) {
  80. image_create(www, file, file_thumb_3, "thumb-3", conf)
  81. return
  82. }
  83. if !utils.IsFileExists(file_thumb_full) {
  84. image_create(www, file, file_thumb_full, "thumb-full", conf)
  85. return
  86. }
  87. }
  88. }
  89. }
  90. func image_loop(www_dir string, stop chan bool) {
  91. dirs, err := ioutil.ReadDir(www_dir)
  92. if err == nil {
  93. for _, dir := range dirs {
  94. target_dir := strings.Join([]string{www_dir, dir.Name(), "htdocs", "products", "images"}, string(os.PathSeparator))
  95. if utils.IsDirExists(target_dir) {
  96. cfile := strings.Join([]string{www_dir, dir.Name(), "config", "config.json"}, string(os.PathSeparator))
  97. conf := config.ConfigNew()
  98. if err := conf.ConfigRead(cfile); err == nil {
  99. pattern := target_dir + string(os.PathSeparator) + "*" + string(os.PathSeparator) + "*.*"
  100. if files, err := filepath.Glob(pattern); err == nil {
  101. for _, file := range files {
  102. select {
  103. case <-stop:
  104. break
  105. default:
  106. image_detect(www_dir, file, conf)
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. func image_start(www_dir string) (chan bool, chan bool) {
  116. ch := make(chan bool)
  117. stop := make(chan bool)
  118. // Run at start
  119. image_loop(www_dir, stop)
  120. go func() {
  121. for {
  122. select {
  123. case <-time.After(2 * time.Second):
  124. // Run every 2 seconds
  125. image_loop(www_dir, stop)
  126. case <-ch:
  127. ch <- true
  128. return
  129. }
  130. }
  131. }()
  132. return ch, stop
  133. }
  134. func image_stop(ch, stop chan bool) {
  135. for {
  136. select {
  137. case stop <- true:
  138. case ch <- true:
  139. <-ch
  140. return
  141. case <-time.After(3 * time.Second):
  142. fmt.Println("Image error: force exit by timeout after 3 seconds")
  143. return
  144. }
  145. }
  146. }