Protect Studio catalog images and expose canonical workspace bootstrap
This commit is contained in:
+20
-4
@@ -42,7 +42,7 @@ function readJson(request, maxBytes) {
|
||||
});
|
||||
}
|
||||
|
||||
function createApiHandler({ resolveWorkspace, store, scrape, publicOrigin, maxBodyBytes = 32768, maxConcurrentImports = 1, maxImportsPerMinute = 5 }) {
|
||||
function createApiHandler({ resolveWorkspace, store, scrape, imageHandler, publicOrigin, maxBodyBytes = 32768, maxConcurrentImports = 1, maxImportsPerMinute = 5 }) {
|
||||
const configured = new URL(publicOrigin);
|
||||
if (!['http:', 'https:'].includes(configured.protocol) || configured.pathname !== '/' || configured.search || configured.hash || configured.username || configured.password) {
|
||||
throw new Error('A public Studio origin without path or credentials is required');
|
||||
@@ -51,6 +51,15 @@ function createApiHandler({ resolveWorkspace, store, scrape, publicOrigin, maxBo
|
||||
const origin = configured.origin, attempts = new Map();
|
||||
let runningImports = 0;
|
||||
|
||||
function browserStore(record, workspace) {
|
||||
return { ...record, products: record.products.map(product => ({
|
||||
...product,
|
||||
image: product.image ? '/api/catalog-image?' + new URLSearchParams({
|
||||
storeId: record.id, productId: String(product.id), tenant: workspace.tenant
|
||||
}).toString() : ''
|
||||
})) };
|
||||
}
|
||||
|
||||
function writeOrigin(request) {
|
||||
if (request.headers.origin !== origin || (request.headers['sec-fetch-site'] && request.headers['sec-fetch-site'] !== 'same-origin')) {
|
||||
throw fail(403, 'ORIGIN_REJECTED', 'এই request Studio page থেকে পাঠান।');
|
||||
@@ -69,20 +78,27 @@ function createApiHandler({ resolveWorkspace, store, scrape, publicOrigin, maxBo
|
||||
try {
|
||||
const pathname = new URL(request.url, origin).pathname;
|
||||
const routes = {
|
||||
'/api/workspace': 'GET', '/api/catalog-image': 'GET',
|
||||
'/api/get-synced-stores': 'GET', '/api/sync-store': 'POST', '/api/delete-synced-store': 'POST'
|
||||
};
|
||||
if (!routes[pathname]) return sendJson(response, 404, { success: false, code: 'NOT_FOUND', error: 'API পাওয়া যায়নি।' });
|
||||
if (request.method !== routes[pathname]) return sendJson(response, 405, { success: false, code: 'METHOD_NOT_ALLOWED', error: 'এই method সমর্থিত নয়।' });
|
||||
if (request.method === 'POST') writeOrigin(request);
|
||||
const workspace = await resolveWorkspace(request);
|
||||
const publicWorkspace = { clientId: workspace.clientId, name: workspace.displayName, tenant: workspace.tenant, canWriteProducts: workspace.canWriteProducts };
|
||||
if (pathname === '/api/workspace') return sendJson(response, 200, { success: true, workspace: publicWorkspace });
|
||||
if (pathname === '/api/catalog-image') {
|
||||
if (!imageHandler) throw fail(503, 'IMAGE_UNAVAILABLE', 'Product image এখন পাওয়া যাচ্ছে না।');
|
||||
return await imageHandler(request, response, workspace);
|
||||
}
|
||||
if (request.method === 'GET') {
|
||||
return sendJson(response, 200, { success: true, stores: await store.read(workspace), workspace: { name: workspace.displayName, tenant: workspace.tenant, canWriteProducts: workspace.canWriteProducts } });
|
||||
return sendJson(response, 200, { success: true, stores: (await store.read(workspace)).map(record => browserStore(record, workspace)), workspace: publicWorkspace });
|
||||
}
|
||||
requireProductWrite(workspace);
|
||||
const body = await readJson(request, maxBodyBytes);
|
||||
if (pathname === '/api/delete-synced-store') {
|
||||
if (typeof body.storeId !== 'string' || !body.storeId || body.storeId.length > 160) throw fail(400, 'INVALID_STORE_ID', 'সঠিক store নির্বাচন করুন।');
|
||||
return sendJson(response, 200, { success: true, stores: await store.remove(workspace, body.storeId) });
|
||||
return sendJson(response, 200, { success: true, stores: (await store.remove(workspace, body.storeId)).map(record => browserStore(record, workspace)) });
|
||||
}
|
||||
if (runningImports >= maxConcurrentImports) throw fail(503, 'IMPORT_BUSY', 'আরেকটি import চলছে; অল্প পরে আবার চেষ্টা করুন।');
|
||||
rateLimit(workspace);
|
||||
@@ -94,7 +110,7 @@ function createApiHandler({ resolveWorkspace, store, scrape, publicOrigin, maxBo
|
||||
const imported = await scrape({ url: body.url, storeName: body.storeName, category: body.category }, { signal: controller.signal });
|
||||
if (controller.signal.aborted) throw fail(499, 'REQUEST_CANCELLED', 'Request বাতিল হয়েছে।');
|
||||
await store.upsert(workspace, imported);
|
||||
return sendJson(response, 200, { success: true, store: imported });
|
||||
return sendJson(response, 200, { success: true, store: browserStore(imported, workspace) });
|
||||
} finally {
|
||||
runningImports--; response.off('close', disconnected);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user