Browse Source

Better products images cache, allow Nginx cache images by real path

Vova Tkach 5 years ago
parent
commit
99dac76da7

+ 5 - 5
engine/fetdata/shop_product_image.go

@@ -39,35 +39,35 @@ func (this *ShopProductImage) Thumbnail0() string {
 	if this == nil {
 	if this == nil {
 		return ""
 		return ""
 	}
 	}
-	return "/api/product-image/thumb-0/" + utils.IntToStr(this.object.A_product_id) + "/" + this.object.A_filename
+	return "/products/images/" + utils.IntToStr(this.object.A_product_id) + "/thumb-0-" + this.object.A_filename
 }
 }
 
 
 func (this *ShopProductImage) Thumbnail1() string {
 func (this *ShopProductImage) Thumbnail1() string {
 	if this == nil {
 	if this == nil {
 		return ""
 		return ""
 	}
 	}
-	return "/api/product-image/thumb-1/" + utils.IntToStr(this.object.A_product_id) + "/" + this.object.A_filename
+	return "/products/images/" + utils.IntToStr(this.object.A_product_id) + "/thumb-1-" + this.object.A_filename
 }
 }
 
 
 func (this *ShopProductImage) Thumbnail2() string {
 func (this *ShopProductImage) Thumbnail2() string {
 	if this == nil {
 	if this == nil {
 		return ""
 		return ""
 	}
 	}
-	return "/api/product-image/thumb-2/" + utils.IntToStr(this.object.A_product_id) + "/" + this.object.A_filename
+	return "/products/images/" + utils.IntToStr(this.object.A_product_id) + "/thumb-2-" + this.object.A_filename
 }
 }
 
 
 func (this *ShopProductImage) Thumbnail3() string {
 func (this *ShopProductImage) Thumbnail3() string {
 	if this == nil {
 	if this == nil {
 		return ""
 		return ""
 	}
 	}
-	return "/api/product-image/thumb-3/" + utils.IntToStr(this.object.A_product_id) + "/" + this.object.A_filename
+	return "/products/images/" + utils.IntToStr(this.object.A_product_id) + "/thumb-3-" + this.object.A_filename
 }
 }
 
 
 func (this *ShopProductImage) ThumbnailFull() string {
 func (this *ShopProductImage) ThumbnailFull() string {
 	if this == nil {
 	if this == nil {
 		return ""
 		return ""
 	}
 	}
-	return "/api/product-image/thumb-full/" + utils.IntToStr(this.object.A_product_id) + "/" + this.object.A_filename
+	return "/products/images/" + utils.IntToStr(this.object.A_product_id) + "/thumb-full-" + this.object.A_filename
 }
 }
 
 
 func (this *ShopProductImage) ThumbnailSize0() [2]int {
 func (this *ShopProductImage) ThumbnailSize0() [2]int {

+ 113 - 69
modules/module_api.go

@@ -3,6 +3,7 @@ package modules
 import (
 import (
 	"net/http"
 	"net/http"
 	"os"
 	"os"
+	"strings"
 
 
 	"golang-fave/assets"
 	"golang-fave/assets"
 	"golang-fave/engine/fetdata"
 	"golang-fave/engine/fetdata"
@@ -20,75 +21,7 @@ func (this *Modules) RegisterModule_Api() *Module {
 		Icon:   assets.SysSvgIconPage,
 		Icon:   assets.SysSvgIconPage,
 		Sub:    &[]MISub{},
 		Sub:    &[]MISub{},
 	}, func(wrap *wrapper.Wrapper) {
 	}, func(wrap *wrapper.Wrapper) {
-		if len(wrap.UrlArgs) == 5 && wrap.UrlArgs[0] == "api" && wrap.UrlArgs[1] == "product-image" && (wrap.UrlArgs[2] == "thumb-0" || wrap.UrlArgs[2] == "thumb-1" || wrap.UrlArgs[2] == "thumb-2" || wrap.UrlArgs[2] == "thumb-3" || wrap.UrlArgs[2] == "thumb-full") {
-			thumb_type := wrap.UrlArgs[2]
-			product_id := wrap.UrlArgs[3]
-			file_name := wrap.UrlArgs[4]
-
-			original_file := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + product_id + string(os.PathSeparator) + file_name
-			if !utils.IsFileExists(original_file) {
-				// User error 404 page
-				wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
-				return
-			}
-
-			width := (*wrap.Config).Shop.Thumbnails.Thumbnail0[0]
-			height := (*wrap.Config).Shop.Thumbnails.Thumbnail0[1]
-			resize := false
-
-			if thumb_type == "thumb-1" {
-				width = (*wrap.Config).Shop.Thumbnails.Thumbnail1[0]
-				height = (*wrap.Config).Shop.Thumbnails.Thumbnail1[1]
-				if (*wrap.Config).Shop.Thumbnails.Thumbnail1[2] == 1 {
-					resize = true
-				}
-			} else if thumb_type == "thumb-2" {
-				width = (*wrap.Config).Shop.Thumbnails.Thumbnail2[0]
-				height = (*wrap.Config).Shop.Thumbnails.Thumbnail2[1]
-				if (*wrap.Config).Shop.Thumbnails.Thumbnail2[2] == 1 {
-					resize = true
-				}
-			} else if thumb_type == "thumb-3" {
-				width = (*wrap.Config).Shop.Thumbnails.Thumbnail3[0]
-				height = (*wrap.Config).Shop.Thumbnails.Thumbnail3[1]
-				if (*wrap.Config).Shop.Thumbnails.Thumbnail3[2] == 1 {
-					resize = true
-				}
-			} else if thumb_type == "thumb-full" {
-				width = (*wrap.Config).Shop.Thumbnails.ThumbnailFull[0]
-				height = (*wrap.Config).Shop.Thumbnails.ThumbnailFull[1]
-				if (*wrap.Config).Shop.Thumbnails.ThumbnailFull[2] == 1 {
-					resize = true
-				}
-			}
-
-			target_file := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + product_id + string(os.PathSeparator) + thumb_type + "-" + file_name
-			if !utils.IsFileExists(target_file) {
-				data, ok, ext, err := this.api_GenerateImage(wrap, width, height, resize, original_file)
-				if err != nil {
-					// System error 500
-					utils.SystemErrorPageEngine(wrap.W, err)
-					return
-				}
-
-				if !ok {
-					// User error 404 page
-					wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
-					return
-				}
-
-				// Save file
-				if file, err := os.Create(target_file); err == nil {
-					file.Write(data)
-				}
-
-				wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-				wrap.W.Header().Set("Content-Type", ext)
-				wrap.W.Write(data)
-			} else {
-				http.ServeFile(wrap.W, wrap.R, target_file)
-			}
-		} else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "api" && wrap.UrlArgs[1] == "products" {
+		if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "api" && wrap.UrlArgs[1] == "products" {
 			if (*wrap.Config).API.XML.Enabled == 1 {
 			if (*wrap.Config).API.XML.Enabled == 1 {
 				// Fix url
 				// Fix url
 				if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
 				if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
@@ -127,3 +60,114 @@ func (this *Modules) RegisterModule_Api() *Module {
 		return "", "", ""
 		return "", "", ""
 	})
 	})
 }
 }
+
+func (this *Modules) RegisterModule_ApiProducts() *Module {
+	return this.newModule(MInfo{
+		WantDB: true,
+		Mount:  "products",
+		Name:   "Api Products",
+		Order:  804,
+		System: true,
+		Icon:   assets.SysSvgIconPage,
+		Sub:    &[]MISub{},
+	}, func(wrap *wrapper.Wrapper) {
+		if len(wrap.UrlArgs) == 4 && wrap.UrlArgs[0] == "products" && wrap.UrlArgs[1] == "images" && utils.IsNumeric(wrap.UrlArgs[2]) && wrap.UrlArgs[3] != "" {
+			thumb_type := ""
+			file_name := ""
+
+			if strings.HasPrefix(wrap.UrlArgs[3], "thumb-0-") {
+				thumb_type = "thumb-0"
+				file_name = wrap.UrlArgs[3][len(thumb_type)+1:]
+			} else if strings.HasPrefix(wrap.UrlArgs[3], "thumb-1-") {
+				thumb_type = "thumb-1"
+				file_name = wrap.UrlArgs[3][len(thumb_type)+1:]
+			} else if strings.HasPrefix(wrap.UrlArgs[3], "thumb-2-") {
+				thumb_type = "thumb-2"
+				file_name = wrap.UrlArgs[3][len(thumb_type)+1:]
+			} else if strings.HasPrefix(wrap.UrlArgs[3], "thumb-3-") {
+				thumb_type = "thumb-3"
+				file_name = wrap.UrlArgs[3][len(thumb_type)+1:]
+			} else if strings.HasPrefix(wrap.UrlArgs[3], "thumb-full-") {
+				thumb_type = "thumb-full"
+				file_name = wrap.UrlArgs[3][len(thumb_type)+1:]
+			}
+
+			if !(thumb_type == "" && file_name == "") {
+				original_file := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + wrap.UrlArgs[2] + string(os.PathSeparator) + file_name
+				if !utils.IsFileExists(original_file) {
+					// User error 404 page
+					wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
+					return
+				}
+
+				width := (*wrap.Config).Shop.Thumbnails.Thumbnail0[0]
+				height := (*wrap.Config).Shop.Thumbnails.Thumbnail0[1]
+				resize := false
+
+				if thumb_type == "thumb-1" {
+					width = (*wrap.Config).Shop.Thumbnails.Thumbnail1[0]
+					height = (*wrap.Config).Shop.Thumbnails.Thumbnail1[1]
+					if (*wrap.Config).Shop.Thumbnails.Thumbnail1[2] == 1 {
+						resize = true
+					}
+				} else if thumb_type == "thumb-2" {
+					width = (*wrap.Config).Shop.Thumbnails.Thumbnail2[0]
+					height = (*wrap.Config).Shop.Thumbnails.Thumbnail2[1]
+					if (*wrap.Config).Shop.Thumbnails.Thumbnail2[2] == 1 {
+						resize = true
+					}
+				} else if thumb_type == "thumb-3" {
+					width = (*wrap.Config).Shop.Thumbnails.Thumbnail3[0]
+					height = (*wrap.Config).Shop.Thumbnails.Thumbnail3[1]
+					if (*wrap.Config).Shop.Thumbnails.Thumbnail3[2] == 1 {
+						resize = true
+					}
+				} else if thumb_type == "thumb-full" {
+					width = (*wrap.Config).Shop.Thumbnails.ThumbnailFull[0]
+					height = (*wrap.Config).Shop.Thumbnails.ThumbnailFull[1]
+					if (*wrap.Config).Shop.Thumbnails.ThumbnailFull[2] == 1 {
+						resize = true
+					}
+				}
+
+				target_file := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + wrap.UrlArgs[2] + string(os.PathSeparator) + thumb_type + "-" + file_name
+				if !utils.IsFileExists(target_file) {
+					data, ok, ext, err := this.api_GenerateImage(wrap, width, height, resize, original_file)
+					if err != nil {
+						// System error 500
+						utils.SystemErrorPageEngine(wrap.W, err)
+						return
+					}
+
+					if !ok {
+						// User error 404 page
+						wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
+						return
+					}
+
+					// Save file
+					if file, err := os.Create(target_file); err == nil {
+						file.Write(data)
+					}
+
+					wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+					wrap.W.Header().Set("Content-Type", ext)
+					wrap.W.Write(data)
+				} else {
+					http.ServeFile(wrap.W, wrap.R, target_file)
+				}
+			} else {
+				// User error 404 page
+				wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
+				return
+			}
+		} else {
+			// User error 404 page
+			wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
+			return
+		}
+	}, func(wrap *wrapper.Wrapper) (string, string, string) {
+		// No any page for back-end
+		return "", "", ""
+	})
+}

+ 1 - 1
modules/module_shop.go

@@ -241,7 +241,7 @@ func (this *Modules) shop_GetAllProductImages(wrap *wrapper.Wrapper, product_id
 		for rows.Next() {
 		for rows.Next() {
 			err = rows.Scan(scan...)
 			err = rows.Scan(scan...)
 			if err == nil {
 			if err == nil {
-				result += `<div class="attached-img"><a href="/products/images/` + html.EscapeString(string(values[0])) + `/` + html.EscapeString(string(values[1])) + `" title="` + html.EscapeString(string(values[1])) + `" target="_blank"><img src="/api/product-image/thumb-0/` + string(values[0]) + `/` + string(values[1]) + `" /></a>, <a href="javascript:fave.ShopProductsDeleteImage(this, ` + html.EscapeString(string(values[0])) + `, '` + html.EscapeString(string(values[1])) + `');">Delete</a></div>`
+				result += `<div class="attached-img"><a href="/products/images/` + html.EscapeString(string(values[0])) + `/` + html.EscapeString(string(values[1])) + `" title="` + html.EscapeString(string(values[1])) + `" target="_blank"><img src="/products/images/` + string(values[0]) + `/thumb-0-` + string(values[1]) + `" /></a>, <a href="javascript:fave.ShopProductsDeleteImage(this, ` + html.EscapeString(string(values[0])) + `, '` + html.EscapeString(string(values[1])) + `');">Delete</a></div>`
 			}
 			}
 		}
 		}
 	}
 	}

+ 1 - 1
modules/module_shop_act_upload_image.go

@@ -92,6 +92,6 @@ func (this *Modules) RegisterAction_ShopUploadImage() *Action {
 			return
 			return
 		}
 		}
 
 
-		wrap.Write(`$('#list-images').append('<div class="attached-img"><a href="/products/images/` + pf_id + `/` + target_file_name + `" title="` + target_file_name + `" target="_blank"><img src="/api/product-image/thumb-0/` + pf_id + `/` + target_file_name + `" /></a>, <a href="javascript:fave.ShopProductsDeleteImage(this, ` + pf_id + `, \'` + target_file_name + `\');">Delete</a></div>');`)
+		wrap.Write(`$('#list-images').append('<div class="attached-img"><a href="/products/images/` + pf_id + `/` + target_file_name + `" title="` + target_file_name + `" target="_blank"><img src="/products/images/` + pf_id + `/thumb-0-` + target_file_name + `" /></a>, <a href="javascript:fave.ShopProductsDeleteImage(this, ` + pf_id + `, \'` + target_file_name + `\');">Delete</a></div>');`)
 	})
 	})
 }
 }

+ 2 - 2
modules/modules.go

@@ -203,7 +203,7 @@ func (this *Modules) getNavMenuModules(wrap *wrapper.Wrapper, sys bool) string {
 		if mod.Mount == "index" {
 		if mod.Mount == "index" {
 			href = `/cp/`
 			href = `/cp/`
 		}
 		}
-		if !(sys && mod.Mount == "api") {
+		if !(sys && (mod.Mount == "api" || mod.Mount == "products")) {
 			html += `<a class="dropdown-item` + class + `" href="` + href + `">` + mod.Name + `</a>`
 			html += `<a class="dropdown-item` + class + `" href="` + href + `">` + mod.Name + `</a>`
 		}
 		}
 	}
 	}
@@ -232,7 +232,7 @@ func (this *Modules) getSidebarModules(wrap *wrapper.Wrapper) string {
 		if !mod.System {
 		if !mod.System {
 			html_def += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
 			html_def += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
 		} else {
 		} else {
-			if mod.Mount != "api" {
+			if !(mod.Mount == "api" || mod.Mount == "products") {
 				html_sys += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
 				html_sys += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
 			}
 			}
 		}
 		}