Browse Source

Optimize images generation

Vova Tkach 5 years ago
parent
commit
352d98c5fb
3 changed files with 38 additions and 21 deletions
  1. 11 1
      engine/wrapper/wrapper.go
  2. 25 20
      image.go
  3. 2 0
      modules/module_shop_act_upload_image.go

+ 11 - 1
engine/wrapper/wrapper.go

@@ -284,6 +284,16 @@ func (this *Wrapper) RecreateProductXmlFile() error {
 	return nil
 }
 
+func (this *Wrapper) RecreateProductImgFiles() error {
+	trigger := strings.Join([]string{this.DTmp, "trigger.img.run"}, string(os.PathSeparator))
+	if !utils.IsFileExists(trigger) {
+		if _, err := os.Create(trigger); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
 func (this *Wrapper) RemoveProductImageThumbnails(product_id, filename string) error {
 	pattern := this.DHtdocs + string(os.PathSeparator) + strings.Join([]string{"products", "images", product_id, filename}, string(os.PathSeparator))
 	if files, err := filepath.Glob(pattern); err != nil {
@@ -295,5 +305,5 @@ func (this *Wrapper) RemoveProductImageThumbnails(product_id, filename string) e
 			}
 		}
 	}
-	return nil
+	return this.RecreateProductImgFiles()
 }

+ 25 - 20
image.go

@@ -62,7 +62,7 @@ func image_create(www, src, dst, typ string, conf *config.Config) {
 	image_generate(width, height, resize, src, dst)
 }
 
-func image_detect(www, file string, conf *config.Config) {
+func image_detect(www, file string, conf *config.Config) bool {
 	index := strings.LastIndex(file, string(os.PathSeparator))
 	if index != -1 {
 		file_name := file[index+1:]
@@ -74,45 +74,50 @@ func image_detect(www, file string, conf *config.Config) {
 			file_thumb_full := file[:index+1] + "thumb-full-" + file_name
 			if !utils.IsFileExists(file_thumb_0) {
 				image_create(www, file, file_thumb_0, "thumb-0", conf)
-				return
 			}
 			if !utils.IsFileExists(file_thumb_1) {
 				image_create(www, file, file_thumb_1, "thumb-1", conf)
-				return
 			}
 			if !utils.IsFileExists(file_thumb_2) {
 				image_create(www, file, file_thumb_2, "thumb-2", conf)
-				return
 			}
 			if !utils.IsFileExists(file_thumb_3) {
 				image_create(www, file, file_thumb_3, "thumb-3", conf)
-				return
 			}
 			if !utils.IsFileExists(file_thumb_full) {
 				image_create(www, file, file_thumb_full, "thumb-full", conf)
-				return
 			}
+			return false
 		}
 	}
+	return true
 }
 
 func image_loop(www_dir string, stop chan bool) {
-	dirs, err := ioutil.ReadDir(www_dir)
-	if err == nil {
+	if dirs, err := ioutil.ReadDir(www_dir); err == nil {
 		for _, dir := range dirs {
-			target_dir := strings.Join([]string{www_dir, dir.Name(), "htdocs", "products", "images"}, string(os.PathSeparator))
-			if utils.IsDirExists(target_dir) {
-				cfile := strings.Join([]string{www_dir, dir.Name(), "config", "config.json"}, string(os.PathSeparator))
+			trigger := strings.Join([]string{www_dir, dir.Name(), "tmp", "trigger.img.run"}, string(os.PathSeparator))
+			if utils.IsFileExists(trigger) {
 				conf := config.ConfigNew()
-				if err := conf.ConfigRead(cfile); err == nil {
-					pattern := target_dir + string(os.PathSeparator) + "*" + string(os.PathSeparator) + "*.*"
-					if files, err := filepath.Glob(pattern); err == nil {
-						for _, file := range files {
-							select {
-							case <-stop:
-								break
-							default:
-								image_detect(www_dir, file, conf)
+				if err := conf.ConfigRead(strings.Join([]string{www_dir, dir.Name(), "config", "config.json"}, string(os.PathSeparator))); err == nil {
+					target_dir := strings.Join([]string{www_dir, dir.Name(), "htdocs", "products", "images"}, string(os.PathSeparator))
+					if utils.IsDirExists(target_dir) {
+						pattern := target_dir + string(os.PathSeparator) + "*" + string(os.PathSeparator) + "*.*"
+						if files, err := filepath.Glob(pattern); err == nil {
+							if len(files) > 0 {
+								for _, file := range files {
+									select {
+									case <-stop:
+										break
+									default:
+										if image_detect(www_dir, file, conf) {
+											os.Remove(trigger)
+											return
+										}
+									}
+								}
+							} else {
+								os.Remove(trigger)
 							}
 						}
 					}

+ 2 - 0
modules/module_shop_act_upload_image.go

@@ -84,6 +84,8 @@ func (this *Modules) RegisterAction_ShopUploadImage() *Action {
 			}
 		}
 
+		wrap.RecreateProductImgFiles()
+
 		wrap.RecreateProductXmlFile()
 
 		wrap.ResetCacheBlocks()