51 lines
3.0 KiB
JavaScript
51 lines
3.0 KiB
JavaScript
const { fail } = require('./workspace.cjs');
|
|
|
|
// The browser receives raster bytes, never a redirect to a merchant-supplied URL.
|
|
function rasterType(body) {
|
|
if (!Buffer.isBuffer(body)) return null;
|
|
if (body.length >= 24 && body.subarray(0, 8).equals(Buffer.from('89504e470d0a1a0a', 'hex')) && body.toString('ascii', 12, 16) === 'IHDR') return 'image/png';
|
|
if (body.length >= 4 && body[0] === 255 && body[1] === 216 && body[2] === 255) return 'image/jpeg';
|
|
if (body.length >= 10 && ['GIF87a', 'GIF89a'].includes(body.toString('ascii', 0, 6))) return 'image/gif';
|
|
if (body.length >= 16 && body.toString('ascii', 0, 4) === 'RIFF' && body.toString('ascii', 8, 12) === 'WEBP') return 'image/webp';
|
|
return null;
|
|
}
|
|
|
|
function createImageHandler({ store, fetchPublic, getConnected, maxConcurrent = 4 }) {
|
|
if (!Number.isInteger(maxConcurrent) || maxConcurrent < 1) throw new Error('Invalid image concurrency');
|
|
let running = 0;
|
|
return async function image(request, response, workspace) {
|
|
const params = new URL(request.url, 'https://studio.invalid').searchParams;
|
|
// Look up the image in this workspace's saved catalog. No arbitrary URL proxy.
|
|
const storeId = params.get('storeId'), productId = params.get('productId');
|
|
if (!storeId || storeId.length > 160 || !productId || productId.length > 8192 || params.has('url')) {
|
|
throw fail(400, 'INVALID_IMAGE_REFERENCE', 'Catalog-এর product image নির্বাচন করুন।');
|
|
}
|
|
const records = storeId.startsWith('connected:') && getConnected ? [await getConnected(workspace)] : await store.read(workspace);
|
|
const product = records.find(item => item.id === storeId)?.products?.find(item => String(item.id) === productId);
|
|
if (!product || typeof product.image !== 'string' || !product.image) throw fail(404, 'IMAGE_NOT_FOUND', 'Product image পাওয়া যায়নি।');
|
|
if (running >= maxConcurrent) throw fail(503, 'IMAGE_BUSY', 'Image অল্প পরে আবার খুলুন।');
|
|
const controller = new AbortController();
|
|
const disconnected = () => { if (!response.writableEnded) controller.abort(); };
|
|
response.on('close', disconnected);
|
|
running++;
|
|
try {
|
|
const result = await fetchPublic(product.image, { signal: controller.signal });
|
|
if (controller.signal.aborted) return;
|
|
if (result.status !== 200) throw fail(502, 'IMAGE_SOURCE_FAILED', 'Website-এর image পাওয়া যায়নি।');
|
|
const mime = rasterType(result.body);
|
|
if (!mime) throw fail(415, 'UNSUPPORTED_IMAGE', 'এই image format preview সমর্থিত নয়।');
|
|
response.writeHead(200, {
|
|
'Content-Type': mime, 'Content-Length': result.body.length,
|
|
'Cache-Control': 'private, no-store', 'X-Content-Type-Options': 'nosniff',
|
|
'Content-Security-Policy': "default-src 'none'; sandbox",
|
|
'Cross-Origin-Resource-Policy': 'same-origin'
|
|
});
|
|
response.end(result.body);
|
|
} finally {
|
|
running--; response.off('close', disconnected);
|
|
}
|
|
};
|
|
}
|
|
|
|
module.exports = { createImageHandler, rasterType };
|