mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 11:45:24 +02:00
fix(sync): harden Dropbox loading
This commit is contained in:
+43
-56
@@ -192,68 +192,55 @@ export default function App() {
|
|||||||
setDefaultLoadError(null);
|
setDefaultLoadError(null);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const isDev = import.meta.env.DEV;
|
|
||||||
|
|
||||||
let rows: any[][];
|
let rows: any[][];
|
||||||
let allData: any[][];
|
let allData: any[][];
|
||||||
let fileMeta = { rev: '', size: 0 };
|
let fileMeta = { rev: '', size: 0 };
|
||||||
|
|
||||||
if (isDev) {
|
const loadWorkbookFromUrl = async (url: string) => {
|
||||||
const cacheBuster = `t_${Date.now()}`;
|
const response = await fetch(url, { cache: 'no-store' });
|
||||||
const fileUrl = `/dropbox-file/scl/fi/usa8me7ywgylrij2bt6hj/Data-Matrix.xlsx?rlkey=tsec8csrhye54u1fdvk15ped1&st=qbxxs4cn&dl=0&${cacheBuster}=${Date.now()}`;
|
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||||
console.log('Fetching Data-Matrix.xlsx from Dropbox (hard refresh)...');
|
|
||||||
|
const contentType = response.headers.get('content-type');
|
||||||
|
if (contentType && contentType.includes('text/html')) {
|
||||||
|
throw new Error('Dropbox returned an HTML page instead of the Excel file.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const arrayBuffer = await response.arrayBuffer();
|
||||||
|
if (arrayBuffer.byteLength < 100) throw new Error('File too small');
|
||||||
|
|
||||||
|
const wb = XLSX.read(arrayBuffer, { type: 'array' });
|
||||||
|
const wsname = wb.SheetNames[0];
|
||||||
|
const ws = wb.Sheets[wsname];
|
||||||
|
allData = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1 });
|
||||||
|
rows = allData.slice(1);
|
||||||
|
return arrayBuffer.byteLength;
|
||||||
|
};
|
||||||
|
|
||||||
|
const dropboxSources = import.meta.env.DEV
|
||||||
|
? [
|
||||||
|
'/api/dropbox-proxy',
|
||||||
|
`/dropbox-file/scl/fi/usa8me7ywgylrij2bt6hj/Data-Matrix.xlsx?rlkey=tsec8csrhye54u1fdvk15ped1&st=qbxxs4cn&dl=0&t=${Date.now()}`
|
||||||
|
]
|
||||||
|
: [
|
||||||
|
'/api/dropbox-proxy'
|
||||||
|
];
|
||||||
|
|
||||||
|
let lastError: unknown = null;
|
||||||
|
for (const source of dropboxSources) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(fileUrl, { cache: 'no-store' });
|
console.log(`Fetching Data-Matrix.xlsx from ${source.includes('/api/') ? 'proxy' : 'Dropbox'}...`);
|
||||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
const size = await loadWorkbookFromUrl(source);
|
||||||
|
fileMeta = { rev: source, size };
|
||||||
const contentType = response.headers.get('content-type');
|
lastError = null;
|
||||||
if (contentType && contentType.includes('text/html')) {
|
break;
|
||||||
throw new Error('Dropbox returned an HTML page instead of the Excel file.');
|
} catch (err) {
|
||||||
}
|
lastError = err;
|
||||||
|
console.warn(`Dropbox source failed for ${source}:`, err);
|
||||||
const arrayBuffer = await response.arrayBuffer();
|
|
||||||
if (arrayBuffer.byteLength < 100) throw new Error('File too small');
|
|
||||||
|
|
||||||
const wb = XLSX.read(arrayBuffer, { type: 'array' });
|
|
||||||
const wsname = wb.SheetNames[0];
|
|
||||||
const ws = wb.Sheets[wsname];
|
|
||||||
allData = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1 });
|
|
||||||
rows = allData.slice(1);
|
|
||||||
fileMeta = { rev: 'dev', size: arrayBuffer.byteLength };
|
|
||||||
} catch (devErr) {
|
|
||||||
console.error('Dropbox dev load failed:', devErr);
|
|
||||||
throw devErr;
|
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
console.log('Fetching file info from Dropbox...');
|
|
||||||
const infoRes = await fetch('/api/dropbox-proxy?info=1');
|
|
||||||
if (infoRes.ok) {
|
|
||||||
fileMeta = await infoRes.json();
|
|
||||||
console.log('File meta:', fileMeta);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Fetching Data-Matrix.xlsx from proxy (hard refresh)...');
|
|
||||||
try {
|
|
||||||
const response = await fetch('/api/dropbox-proxy', { cache: 'no-store' });
|
|
||||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
||||||
|
|
||||||
const contentType = response.headers.get('content-type');
|
if (lastError) {
|
||||||
if (contentType && contentType.includes('text/html')) {
|
throw lastError instanceof Error ? lastError : new Error(String(lastError));
|
||||||
throw new Error('Dropbox returned an HTML page instead of the Excel file.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const arrayBuffer = await response.arrayBuffer();
|
|
||||||
if (arrayBuffer.byteLength < 100) throw new Error('File too small');
|
|
||||||
|
|
||||||
const wb = XLSX.read(arrayBuffer, { type: 'array' });
|
|
||||||
const wsname = wb.SheetNames[0];
|
|
||||||
const ws = wb.Sheets[wsname];
|
|
||||||
allData = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1 });
|
|
||||||
rows = allData.slice(1);
|
|
||||||
} catch (prodErr) {
|
|
||||||
console.error('Dropbox prod load failed:', prodErr);
|
|
||||||
throw prodErr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rows.length > 0) {
|
if (rows.length > 0) {
|
||||||
|
|||||||
+2
-1
@@ -31,6 +31,7 @@ async function readJsonBody(req: any): Promise<any> {
|
|||||||
|
|
||||||
export default defineConfig(({mode}) => {
|
export default defineConfig(({mode}) => {
|
||||||
const env = loadEnv(mode, '.', '');
|
const env = loadEnv(mode, '.', '');
|
||||||
|
const prodEnv = loadEnv('production', '.', '');
|
||||||
return {
|
return {
|
||||||
plugins: [
|
plugins: [
|
||||||
react(),
|
react(),
|
||||||
@@ -44,7 +45,7 @@ export default defineConfig(({mode}) => {
|
|||||||
const config = getBcConfig(env);
|
const config = getBcConfig(env);
|
||||||
|
|
||||||
// Populate process.env with loaded env variables for serverless handlers
|
// Populate process.env with loaded env variables for serverless handlers
|
||||||
Object.assign(process.env, env);
|
Object.assign(process.env, prodEnv, env);
|
||||||
|
|
||||||
// Helper to adapt Node.js req/res to Vercel signature
|
// Helper to adapt Node.js req/res to Vercel signature
|
||||||
const adaptVercelHandler = async (handler: any) => {
|
const adaptVercelHandler = async (handler: any) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user