Fix product performance grouped JSON build
This commit is contained in:
@@ -17,15 +17,16 @@ import (
|
||||
)
|
||||
|
||||
type ProductImageItem struct {
|
||||
ID int64 `json:"id"`
|
||||
FileName string `json:"file_name"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
Storage string `json:"storage_path"`
|
||||
ContentURL string `json:"content_url"`
|
||||
UUID string `json:"uuid,omitempty"`
|
||||
ThumbURL string `json:"thumb_url,omitempty"`
|
||||
FullURL string `json:"full_url,omitempty"`
|
||||
StoredInDB bool `json:"stored_in_db,omitempty"`
|
||||
ID int64 `json:"id"`
|
||||
FileName string `json:"file_name"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
Storage string `json:"storage_path"`
|
||||
ContentURL string `json:"content_url"`
|
||||
UUID string `json:"uuid,omitempty"`
|
||||
ThumbURL string `json:"thumb_url,omitempty"`
|
||||
FullURL string `json:"full_url,omitempty"`
|
||||
StoredInDB bool `json:"stored_in_db,omitempty"`
|
||||
HasDBContent bool `json:"-"`
|
||||
}
|
||||
|
||||
type ProductImageBatchItem struct {
|
||||
@@ -141,14 +142,68 @@ func enrichProductImageItem(it *ProductImageItem) {
|
||||
if it == nil {
|
||||
return
|
||||
}
|
||||
if it.ID > 0 {
|
||||
it.ContentURL = fmt.Sprintf("/api/product-images/%d/content", it.ID)
|
||||
}
|
||||
if u := extractImageUUID(it.Storage, it.FileName); u != "" {
|
||||
it.UUID = u
|
||||
it.ThumbURL = "/uploads/image/t300/" + u + ".jpg"
|
||||
it.FullURL = "/uploads/image/" + u + ".jpg"
|
||||
it.ThumbURL = productImagePublicURLIfExists("uploads/image/t300/" + u + ".jpg")
|
||||
it.FullURL = productImagePublicURLIfExists("uploads/image/" + u + ".jpg")
|
||||
}
|
||||
if it.FullURL == "" {
|
||||
it.FullURL = productImagePublicURLIfExists(it.Storage)
|
||||
}
|
||||
if it.FullURL == "" {
|
||||
it.FullURL = productImagePublicURLIfExists(it.FileName)
|
||||
}
|
||||
if it.ID > 0 && (it.HasDBContent || productImageStoredFileExists(it.Storage, it.FileName)) {
|
||||
it.ContentURL = fmt.Sprintf("/api/product-images/%d/content", it.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func productImageItemHasURL(it *ProductImageItem) bool {
|
||||
return it != nil && (it.ThumbURL != "" || it.FullURL != "" || it.ContentURL != "")
|
||||
}
|
||||
|
||||
func productImageStoredFileExists(storagePath, fileName string) bool {
|
||||
if resolved, _ := resolveStoragePath(storagePath); resolved != "" {
|
||||
return true
|
||||
}
|
||||
if resolved, _ := resolveStoragePath(fileName); resolved != "" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func productImagePublicURLIfExists(storagePath string) string {
|
||||
raw := strings.TrimSpace(storagePath)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
resolved, _ := resolveStoragePath(raw)
|
||||
if resolved == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
normalized := strings.ReplaceAll(raw, "\\", "/")
|
||||
lower := strings.ToLower(normalized)
|
||||
if idx := strings.Index(lower, "/uploads/"); idx >= 0 {
|
||||
return normalized[idx:]
|
||||
}
|
||||
if strings.HasPrefix(lower, "uploads/") {
|
||||
return "/" + normalized
|
||||
}
|
||||
|
||||
root := strings.TrimSpace(os.Getenv("BLOB_ROOT"))
|
||||
if root == "" {
|
||||
return ""
|
||||
}
|
||||
rel, err := filepath.Rel(root, resolved)
|
||||
if err != nil || rel == "." || strings.HasPrefix(rel, "..") || filepath.IsAbs(rel) {
|
||||
return ""
|
||||
}
|
||||
rel = filepath.ToSlash(rel)
|
||||
if strings.HasPrefix(strings.ToLower(rel), "uploads/") {
|
||||
return "/" + rel
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// POST /api/product-images/batch
|
||||
@@ -224,6 +279,7 @@ ranked AS (
|
||||
COALESCE(b.file_size,0) AS file_size,
|
||||
COALESCE(b.storage_path,'') AS storage_path,
|
||||
COALESCE(b.stored_in_db,false) AS stored_in_db,
|
||||
(COALESCE(octet_length(b.bin),0) > 0) AS has_db_content,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY mi.key
|
||||
ORDER BY
|
||||
@@ -241,9 +297,9 @@ ranked AS (
|
||||
AND b.typ='img'
|
||||
AND b.src_id=mi.mmitem_id
|
||||
)
|
||||
SELECT key, id, file_name, file_size, storage_path, stored_in_db
|
||||
SELECT key, id, file_name, file_size, storage_path, stored_in_db, has_db_content
|
||||
FROM ranked
|
||||
WHERE rn <= 1
|
||||
WHERE rn <= 5
|
||||
ORDER BY key, rn
|
||||
`, string(payload))
|
||||
if err != nil {
|
||||
@@ -257,10 +313,13 @@ ORDER BY key, rn
|
||||
for rows.Next() {
|
||||
var key string
|
||||
var it ProductImageItem
|
||||
if err := rows.Scan(&key, &it.ID, &it.FileName, &it.FileSize, &it.Storage, &it.StoredInDB); err != nil {
|
||||
if err := rows.Scan(&key, &it.ID, &it.FileName, &it.FileSize, &it.Storage, &it.StoredInDB, &it.HasDBContent); err != nil {
|
||||
continue
|
||||
}
|
||||
enrichProductImageItem(&it)
|
||||
if !productImageItemHasURL(&it) {
|
||||
continue
|
||||
}
|
||||
grouped[key] = append(grouped[key], it)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
@@ -351,7 +410,8 @@ SELECT
|
||||
COALESCE(file_name,'') AS file_name,
|
||||
COALESCE(file_size,0) AS file_size,
|
||||
COALESCE(storage_path,'') AS storage_path,
|
||||
COALESCE(stored_in_db,false) AS stored_in_db
|
||||
COALESCE(stored_in_db,false) AS stored_in_db,
|
||||
(COALESCE(octet_length(bin),0) > 0) AS has_db_content
|
||||
FROM dfblob
|
||||
WHERE typ='img'
|
||||
AND src_table='mmitem'
|
||||
@@ -382,11 +442,13 @@ ORDER BY
|
||||
items := make([]ProductImageItem, 0, 16)
|
||||
for rows.Next() {
|
||||
var it ProductImageItem
|
||||
if err := rows.Scan(&it.ID, &it.FileName, &it.FileSize, &it.Storage, &it.StoredInDB); err != nil {
|
||||
if err := rows.Scan(&it.ID, &it.FileName, &it.FileSize, &it.Storage, &it.StoredInDB, &it.HasDBContent); err != nil {
|
||||
continue
|
||||
}
|
||||
enrichProductImageItem(&it)
|
||||
items = append(items, it)
|
||||
if productImageItemHasURL(&it) {
|
||||
items = append(items, it)
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user