Load product images from Presta image path
This commit is contained in:
+17
-12
@@ -94,11 +94,8 @@ export function productToDoByManufacturerSql({ manufacturerId, position = 1 }) {
|
|||||||
p.id_product,
|
p.id_product,
|
||||||
p.price,
|
p.price,
|
||||||
(
|
(
|
||||||
SELECT CONCAT('https://www.9b-plus.com/', i.id_image, '-home_default/', pl.link_rewrite, '.jpg')
|
SELECT ${prestaImageUrl("i.id_image", "home_default")}
|
||||||
FROM ps_image i
|
FROM ps_image i
|
||||||
JOIN ps_product_lang pl
|
|
||||||
ON pl.id_product = i.id_product
|
|
||||||
AND pl.id_lang = 2
|
|
||||||
WHERE i.id_product = p.id_product
|
WHERE i.id_product = p.id_product
|
||||||
ORDER BY i.cover DESC, i.position ASC
|
ORDER BY i.cover DESC, i.position ASC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
@@ -155,11 +152,8 @@ export function productByEanSql({ manufacturerId, ean }) {
|
|||||||
p.id_product,
|
p.id_product,
|
||||||
p.price,
|
p.price,
|
||||||
(
|
(
|
||||||
SELECT CONCAT('https://www.9b-plus.com/', i.id_image, '-home_default/', pl.link_rewrite, '.jpg')
|
SELECT ${prestaImageUrl("i.id_image", "home_default")}
|
||||||
FROM ps_image i
|
FROM ps_image i
|
||||||
JOIN ps_product_lang pl
|
|
||||||
ON pl.id_product = i.id_product
|
|
||||||
AND pl.id_lang = 2
|
|
||||||
WHERE i.id_product = p.id_product
|
WHERE i.id_product = p.id_product
|
||||||
ORDER BY i.cover DESC, i.position ASC
|
ORDER BY i.cover DESC, i.position ASC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
@@ -345,11 +339,8 @@ export function suggestedProductsSql({ manufacturerId, supplierReference, produc
|
|||||||
p.supplier_reference,
|
p.supplier_reference,
|
||||||
p.active,
|
p.active,
|
||||||
(
|
(
|
||||||
SELECT CONCAT('https://www.9b-plus.com/', i.id_image, '-cart_default/', pli.link_rewrite, '.jpg')
|
SELECT ${prestaImageUrl("i.id_image", "cart_default")}
|
||||||
FROM ps_image i
|
FROM ps_image i
|
||||||
JOIN ps_product_lang pli
|
|
||||||
ON pli.id_product = i.id_product
|
|
||||||
AND pli.id_lang = 2
|
|
||||||
WHERE i.id_product = p.id_product
|
WHERE i.id_product = p.id_product
|
||||||
ORDER BY i.cover DESC, i.position ASC
|
ORDER BY i.cover DESC, i.position ASC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
@@ -429,6 +420,20 @@ function escapeSqlIdentifier(value) {
|
|||||||
return `\`${text}\``;
|
return `\`${text}\``;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function prestaImageUrl(idExpression, size) {
|
||||||
|
const safeSize = String(size ?? "").trim();
|
||||||
|
if (!/^[a-zA-Z0-9_]+$/.test(safeSize)) {
|
||||||
|
throw new Error("PrestaShop image size contains unsupported characters.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const idText = `CAST(${idExpression} AS CHAR)`;
|
||||||
|
const pathParts = Array.from({ length: 10 }, (_, index) =>
|
||||||
|
`IF(CHAR_LENGTH(${idText}) > ${index}, CONCAT(SUBSTRING(${idText}, ${index + 1}, 1), '/'), '')`,
|
||||||
|
).join(", ");
|
||||||
|
|
||||||
|
return `CONCAT('https://www.9b-plus.com/img/p/', ${pathParts}, ${idExpression}, '-${safeSize}.jpg')`;
|
||||||
|
}
|
||||||
|
|
||||||
function escapeSqlString(value) {
|
function escapeSqlString(value) {
|
||||||
return String(value ?? "").replaceAll("\\", "\\\\").replaceAll("'", "''").trim();
|
return String(value ?? "").replaceAll("\\", "\\\\").replaceAll("'", "''").trim();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user