mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 12:15:23 +02:00
feat(bsr): fetch BSR.xlsx from Dropbox instead of local file
- Rewrite api/fetch-bsr.ts to proxy BSR.xlsx from the Dropbox URL, matching the same pattern as fetch-buybox.ts and other data sources - Add /api/fetch-bsr proxy in vite.config.ts for local dev Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
ce13d17eda
commit
17a3df9fb3
+29
-18
@@ -1,27 +1,38 @@
|
||||
import { promises as fs } from 'fs';
|
||||
import path from 'path';
|
||||
import type { VercelRequest, VercelResponse } from '@vercel/node';
|
||||
|
||||
export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||
try {
|
||||
const filePath = path.join(process.cwd(), 'BSR.xlsx');
|
||||
const BSR_DROPBOX_URL = "https://www.dropbox.com/scl/fi/r32r9x6r7bvgg6ckiaowk/BSR.xlsx?rlkey=vka50xyhc637riq901qo5bk16&st=y6ys2oz8&dl=1";
|
||||
|
||||
try {
|
||||
await fs.access(filePath);
|
||||
} catch {
|
||||
return res.status(404).json({ error: 'BSR data file not found' });
|
||||
export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||
// CORS headers
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
||||
|
||||
if (req.method === 'OPTIONS') {
|
||||
return res.status(200).end();
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('[fetch-bsr] Fetching BSR.xlsx from Dropbox...');
|
||||
const response = await fetch(BSR_DROPBOX_URL, {
|
||||
cache: 'no-store',
|
||||
headers: {
|
||||
'Pragma': 'no-cache',
|
||||
'Cache-Control': 'no-cache'
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Dropbox responded with ${response.status}`);
|
||||
}
|
||||
|
||||
const fileBuffer = await fs.readFile(filePath);
|
||||
const buffer = await response.arrayBuffer();
|
||||
console.log('[fetch-bsr] Successfully fetched BSR Excel, size:', buffer.byteLength);
|
||||
|
||||
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||||
res.setHeader('Content-Disposition', 'attachment; filename="BSR.xlsx"');
|
||||
res.setHeader('Cache-Control', 'public, max-age=3600');
|
||||
|
||||
// Convert Buffer to ArrayBuffer before sending for fetch compatibility on client
|
||||
res.send(fileBuffer);
|
||||
} catch (error) {
|
||||
console.error('Error serving BSR Excel file:', error);
|
||||
res.status(500).json({ error: 'Failed to serve BSR file' });
|
||||
res.status(200).send(Buffer.from(buffer));
|
||||
} catch (error: any) {
|
||||
console.error('[fetch-bsr] Error:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user