feat: sync shop icons and continue imports in v1.1.4
This commit is contained in:
@@ -145,10 +145,14 @@ async function importOrdersForShop(config, api, dependencies = {}) {
|
||||
cursor = Number(order.id);
|
||||
processed += 1;
|
||||
} catch (error) {
|
||||
Log.error('Failed to import PS order; cursor remains before this order', {
|
||||
orderId: order.id, cursor, error: error.message,
|
||||
const previousCursor = cursor;
|
||||
cursor = Number(order.id);
|
||||
Log.error('Failed to import PS order; skipping order and continuing', {
|
||||
orderId: order.id,
|
||||
previousCursor,
|
||||
cursor,
|
||||
error: error.message,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ const PrestashopApi = require('./PrestaApi');
|
||||
const { isLegacyApi } = require('./utils');
|
||||
const { extractPsStateName } = require('./status-mapper');
|
||||
const { setPsStates, loadPsStates } = require('./module-config');
|
||||
const { ensureShopIcon } = require('./shop-icon');
|
||||
|
||||
const ORBITO_STATUS_OPTIONS = {
|
||||
module: 'core/orders',
|
||||
@@ -95,6 +96,11 @@ async function registerPrestashopConfigBlocks() {
|
||||
try {
|
||||
const psStates = await fetchPsStates(apiUrl, apiKey, params.versionCode);
|
||||
await setPsStates(psStates);
|
||||
await ensureShopIcon({
|
||||
apiUrl,
|
||||
shopName: params.shopName,
|
||||
force: true,
|
||||
});
|
||||
Log.info('PrestaShop config: połączenie OK, statusy pobrane', { count: psStates.length });
|
||||
} catch (err) {
|
||||
Log.warn('PrestaShop config validate failed', { error: err?.message });
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
const { Config, Core, Log } = require('@orbito/api-lib');
|
||||
|
||||
const CACHE_KEY = 'runtime.shopIcon';
|
||||
const MAX_ICON_BYTES = 45 * 1024;
|
||||
const MAX_HTML_BYTES = 512 * 1024;
|
||||
const FETCH_TIMEOUT_MS = 10_000;
|
||||
const MAX_REDIRECTS = 3;
|
||||
|
||||
const pendingEnsures = new Map();
|
||||
|
||||
function storefrontUrl(apiUrl) {
|
||||
const url = new URL(String(apiUrl || '').trim());
|
||||
if (!['http:', 'https:'].includes(url.protocol)) throw new Error('INVALID_SHOP_URL');
|
||||
|
||||
url.hash = '';
|
||||
url.search = '';
|
||||
const segments = url.pathname.split('/').filter(Boolean);
|
||||
if (segments[segments.length - 1]?.toLowerCase() === 'api') segments.pop();
|
||||
url.pathname = `/${segments.join('/')}${segments.length ? '/' : ''}`;
|
||||
return url.href;
|
||||
}
|
||||
|
||||
function headerValue(headers, name) {
|
||||
if (!headers) return '';
|
||||
if (typeof headers.get === 'function') return headers.get(name) || '';
|
||||
const key = Object.keys(headers).find(item => item.toLowerCase() === name.toLowerCase());
|
||||
return key ? headers[key] : '';
|
||||
}
|
||||
|
||||
async function responseBuffer(response) {
|
||||
if (Buffer.isBuffer(response?.data)) return response.data;
|
||||
if (response?.data != null) return Buffer.from(response.data);
|
||||
if (typeof response?.arrayBuffer === 'function') {
|
||||
return Buffer.from(await response.arrayBuffer());
|
||||
}
|
||||
if (typeof response?.text === 'function') {
|
||||
return Buffer.from(await response.text());
|
||||
}
|
||||
return Buffer.alloc(0);
|
||||
}
|
||||
|
||||
async function fetchSameHost(url, rootUrl, maxBytes) {
|
||||
let current = new URL(url);
|
||||
const root = new URL(rootUrl);
|
||||
|
||||
for (let redirect = 0; redirect <= MAX_REDIRECTS; redirect += 1) {
|
||||
if (!['http:', 'https:'].includes(current.protocol)
|
||||
|| current.host !== root.host
|
||||
|| current.username
|
||||
|| current.password) {
|
||||
throw new Error('SHOP_ICON_REDIRECT_REJECTED');
|
||||
}
|
||||
|
||||
const response = await Core.fetch(current.href, {
|
||||
method: 'GET',
|
||||
timeout: FETCH_TIMEOUT_MS,
|
||||
redirect: 'manual',
|
||||
headers: { Accept: 'image/*,text/html;q=0.8,*/*;q=0.1' },
|
||||
});
|
||||
const status = Number(response?.status || 0);
|
||||
if (status >= 300 && status < 400) {
|
||||
const location = headerValue(response.headers, 'location');
|
||||
if (!location || redirect === MAX_REDIRECTS) throw new Error('SHOP_ICON_REDIRECT_REJECTED');
|
||||
current = new URL(location, current);
|
||||
continue;
|
||||
}
|
||||
if (status < 200 || status >= 300) throw new Error(`SHOP_ICON_HTTP_${status || 'ERROR'}`);
|
||||
|
||||
const declaredLength = Number(headerValue(response.headers, 'content-length'));
|
||||
if (Number.isFinite(declaredLength) && declaredLength > maxBytes) {
|
||||
throw new Error('SHOP_ICON_TOO_LARGE');
|
||||
}
|
||||
const buffer = await responseBuffer(response);
|
||||
if (!buffer.length || buffer.length > maxBytes) throw new Error('SHOP_ICON_TOO_LARGE');
|
||||
|
||||
return {
|
||||
buffer,
|
||||
contentType: headerValue(response.headers, 'content-type'),
|
||||
url: current.href,
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error('SHOP_ICON_REDIRECT_REJECTED');
|
||||
}
|
||||
|
||||
function tagAttribute(tag, attribute) {
|
||||
const escaped = attribute.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const quoted = new RegExp(`\\b${escaped}\\s*=\\s*(["'])(.*?)\\1`, 'i').exec(tag);
|
||||
if (quoted) return quoted[2];
|
||||
const bare = new RegExp(`\\b${escaped}\\s*=\\s*([^\\s>]+)`, 'i').exec(tag);
|
||||
return bare ? bare[1] : '';
|
||||
}
|
||||
|
||||
function faviconCandidates(html, documentUrl) {
|
||||
const candidates = [];
|
||||
const seen = new Set();
|
||||
const tags = String(html || '').match(/<link\b[^>]*>/gi) || [];
|
||||
|
||||
for (const tag of tags) {
|
||||
const rel = tagAttribute(tag, 'rel').toLowerCase();
|
||||
if (!rel.split(/\s+/).some(token => token === 'icon' || token === 'apple-touch-icon')) continue;
|
||||
const href = tagAttribute(tag, 'href').replace(/&/gi, '&').trim();
|
||||
if (!href || href.startsWith('data:')) continue;
|
||||
try {
|
||||
const candidate = new URL(href, documentUrl);
|
||||
if (!['http:', 'https:'].includes(candidate.protocol) || seen.has(candidate.href)) continue;
|
||||
seen.add(candidate.href);
|
||||
candidates.push(candidate.href);
|
||||
} catch {
|
||||
// Pomijamy nieprawidłowy href i przechodzimy do standardowego favicon.ico.
|
||||
}
|
||||
}
|
||||
|
||||
const fallback = new URL('favicon.ico', documentUrl).href;
|
||||
if (!seen.has(fallback)) candidates.push(fallback);
|
||||
return candidates.slice(0, 5);
|
||||
}
|
||||
|
||||
function detectIconMime(buffer) {
|
||||
if (!Buffer.isBuffer(buffer)) return '';
|
||||
if (buffer.length >= 8
|
||||
&& buffer.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))) {
|
||||
return 'image/png';
|
||||
}
|
||||
if (buffer.length >= 3 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) {
|
||||
return 'image/jpeg';
|
||||
}
|
||||
if (buffer.length >= 12
|
||||
&& buffer.subarray(0, 4).toString('ascii') === 'RIFF'
|
||||
&& buffer.subarray(8, 12).toString('ascii') === 'WEBP') {
|
||||
return 'image/webp';
|
||||
}
|
||||
if (buffer.length >= 6 && ['GIF87a', 'GIF89a'].includes(buffer.subarray(0, 6).toString('ascii'))) {
|
||||
return 'image/gif';
|
||||
}
|
||||
if (buffer.length >= 4
|
||||
&& buffer[0] === 0x00 && buffer[1] === 0x00
|
||||
&& buffer[2] === 0x01 && buffer[3] === 0x00) {
|
||||
return 'image/vnd.microsoft.icon';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
async function downloadShopIcon(shopPageUrl) {
|
||||
let documentUrl = shopPageUrl;
|
||||
let candidates = [new URL('favicon.ico', shopPageUrl).href];
|
||||
|
||||
try {
|
||||
const page = await fetchSameHost(shopPageUrl, shopPageUrl, MAX_HTML_BYTES);
|
||||
documentUrl = page.url;
|
||||
candidates = faviconCandidates(page.buffer.toString('utf8'), documentUrl);
|
||||
} catch (error) {
|
||||
Log.warn('PrestaShop favicon: storefront discovery failed', { error: error?.message });
|
||||
}
|
||||
|
||||
for (const candidate of candidates) {
|
||||
try {
|
||||
const image = await fetchSameHost(candidate, documentUrl, MAX_ICON_BYTES);
|
||||
const mime = detectIconMime(image.buffer);
|
||||
if (!mime) continue;
|
||||
return {
|
||||
dataUrl: `data:${mime};base64,${image.buffer.toString('base64')}`,
|
||||
mime,
|
||||
sourceUrl: image.url,
|
||||
};
|
||||
} catch (error) {
|
||||
Log.warn('PrestaShop favicon: candidate rejected', {
|
||||
candidate,
|
||||
error: error?.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function registerSourceProfile(cache, shopName) {
|
||||
try {
|
||||
await Core.callModule('core/orders', 'source.register', {
|
||||
name: String(shopName || '').trim() || new URL(cache.shopUrl).hostname,
|
||||
iconDataUrl: cache.dataUrl || '',
|
||||
iconSourceUrl: cache.sourceUrl || '',
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
Log.warn('PrestaShop favicon: source profile registration failed', { error: error?.message });
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureShopIconOnce({ apiUrl, shopName, force = false }) {
|
||||
const shopUrl = storefrontUrl(apiUrl);
|
||||
const existing = await Config.get(CACHE_KEY, null);
|
||||
const sameShop = existing && existing.shopUrl === shopUrl;
|
||||
|
||||
if (!force && sameShop && existing.fetchedAt) {
|
||||
await registerSourceProfile(existing, shopName);
|
||||
return { ...existing, cacheHit: true };
|
||||
}
|
||||
|
||||
const downloaded = await downloadShopIcon(shopUrl);
|
||||
const retained = !downloaded && sameShop && existing?.dataUrl ? existing : null;
|
||||
const cache = {
|
||||
shopUrl,
|
||||
dataUrl: downloaded?.dataUrl || retained?.dataUrl || '',
|
||||
mime: downloaded?.mime || retained?.mime || '',
|
||||
sourceUrl: downloaded?.sourceUrl || retained?.sourceUrl || '',
|
||||
fetchedAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
await Config.set(CACHE_KEY, cache);
|
||||
await registerSourceProfile(cache, shopName);
|
||||
return { ...cache, cacheHit: false };
|
||||
}
|
||||
|
||||
function ensureShopIcon(options) {
|
||||
const key = `${storefrontUrl(options?.apiUrl)}|${options?.force === true ? 'refresh' : 'cached'}`;
|
||||
if (pendingEnsures.has(key)) return pendingEnsures.get(key);
|
||||
|
||||
const promise = ensureShopIconOnce(options).finally(() => {
|
||||
pendingEnsures.delete(key);
|
||||
});
|
||||
pendingEnsures.set(key, promise);
|
||||
return promise;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ensureShopIcon,
|
||||
downloadShopIcon,
|
||||
storefrontUrl,
|
||||
faviconCandidates,
|
||||
detectIconMime,
|
||||
_private: {
|
||||
CACHE_KEY,
|
||||
MAX_ICON_BYTES,
|
||||
MAX_HTML_BYTES,
|
||||
FETCH_TIMEOUT_MS,
|
||||
fetchSameHost,
|
||||
responseBuffer,
|
||||
tagAttribute,
|
||||
registerSourceProfile,
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user