Browse Source

Shop product images remove optimization

Vova Tkach 5 years ago
parent
commit
383e08fc2b

+ 16 - 0
engine/wrapper/wrapper.go

@@ -7,7 +7,9 @@ import (
 	"html/template"
 	"html/template"
 	"net/http"
 	"net/http"
 	"os"
 	"os"
+	"path/filepath"
 	"strconv"
 	"strconv"
+	"strings"
 	"time"
 	"time"
 
 
 	"golang-fave/consts"
 	"golang-fave/consts"
@@ -288,3 +290,17 @@ func (this *Wrapper) GetCurrentPage(max int) int {
 func (this *Wrapper) ConfigSave() error {
 func (this *Wrapper) ConfigSave() error {
 	return this.Config.configWrite(this.DConfig + string(os.PathSeparator) + "config.json")
 	return this.Config.configWrite(this.DConfig + string(os.PathSeparator) + "config.json")
 }
 }
+
+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 {
+		return err
+	} else {
+		for _, file := range files {
+			if err := os.Remove(file); err != nil {
+				return errors.New(fmt.Sprintf("[upload delete] Thumbnail file (%s) delete error: %s", file, err.Error()))
+			}
+		}
+	}
+	return nil
+}

+ 3 - 9
modules/module_settings_act_thumbnails.go

@@ -1,8 +1,6 @@
 package modules
 package modules
 
 
 import (
 import (
-	"os"
-	"path/filepath"
 	"strconv"
 	"strconv"
 
 
 	"golang-fave/engine/wrapper"
 	"golang-fave/engine/wrapper"
@@ -174,13 +172,9 @@ func (this *Modules) RegisterAction_SettingsThumbnails() *Action {
 		}
 		}
 
 
 		// Reset products images cache
 		// Reset products images cache
-		pattern := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + "*" + string(os.PathSeparator) + "thumb-*"
-		if files, err := filepath.Glob(pattern); err == nil {
-			for _, file := range files {
-				if err := os.Remove(file); err != nil {
-					wrap.LogError("[settings save] Thumbnail file (%s) delete error: %s", file, err.Error())
-				}
-			}
+		if err := wrap.RemoveProductImageThumbnails("*", "thumb-*"); err != nil {
+			wrap.MsgError(err.Error())
+			return
 		}
 		}
 
 
 		// Reload current page
 		// Reload current page

+ 1 - 28
modules/module_shop_act_delete.go

@@ -1,8 +1,6 @@
 package modules
 package modules
 
 
 import (
 import (
-	"os"
-
 	"golang-fave/engine/wrapper"
 	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 	"golang-fave/utils"
 )
 )
@@ -36,35 +34,10 @@ func (this *Modules) RegisterAction_ShopDelete() *Action {
 			}
 			}
 
 
 			// Delete product attached images
 			// Delete product attached images
-			rows, err := wrap.DB.Query(
-				`SELECT
-					product_id,
-					filename
-				FROM
-					shop_product_images
-				WHERE
-					product_id = ?
-				;`,
-				pf_id,
-			)
-			if err == nil {
-				defer rows.Close()
-				values := make([]string, 2)
-				scan := make([]interface{}, len(values))
-				for i := range values {
-					scan[i] = &values[i]
-				}
-				for rows.Next() {
-					err = rows.Scan(scan...)
-					if err == nil {
-						target_file_full := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + string(values[0]) + string(os.PathSeparator) + string(values[1])
-						os.Remove(target_file_full)
-					}
-				}
-			}
 			if _, err := tx.Exec("DELETE FROM shop_product_images WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
 			if _, err := tx.Exec("DELETE FROM shop_product_images WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
 				return err
 				return err
 			}
 			}
+			wrap.RemoveProductImageThumbnails(pf_id, "*")
 
 
 			// Delete target product with category connection data
 			// Delete target product with category connection data
 			if _, err := tx.Exec("DELETE FROM shop_filter_product_values WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
 			if _, err := tx.Exec("DELETE FROM shop_filter_product_values WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {

+ 7 - 11
modules/module_shop_act_upload_delete.go

@@ -2,7 +2,6 @@ package modules
 
 
 import (
 import (
 	"os"
 	"os"
-	"path/filepath"
 
 
 	"golang-fave/engine/wrapper"
 	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 	"golang-fave/utils"
@@ -41,18 +40,15 @@ func (this *Modules) RegisterAction_ShopUploadDelete() *Action {
 				return err
 				return err
 			}
 			}
 
 
+			// Delete thumbnails
+			if err := wrap.RemoveProductImageThumbnails(pf_id, "thumb-*-"+pf_file); err != nil {
+				return err
+			}
+
 			// Delete file
 			// Delete file
 			target_file_full := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + pf_id + string(os.PathSeparator) + pf_file
 			target_file_full := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + pf_id + string(os.PathSeparator) + pf_file
-			os.Remove(target_file_full)
-
-			// Delete thumbnails
-			pattern := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + pf_id + string(os.PathSeparator) + "thumb-*-" + pf_file
-			if files, err := filepath.Glob(pattern); err == nil {
-				for _, file := range files {
-					if err := os.Remove(file); err != nil {
-						wrap.LogError("[upload delete] Thumbnail file (%s) delete error: %s", file, err.Error())
-					}
-				}
+			if err := os.Remove(target_file_full); err != nil {
+				return err
 			}
 			}
 
 
 			return nil
 			return nil