mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 15:15:24 +02:00
fix(fetch): add proxy fallbacks and improved error logging for dropbox
This commit is contained in:
+54
-32
@@ -24,40 +24,62 @@ export default function App() {
|
||||
|
||||
useEffect(() => {
|
||||
const loadDefaultData = async () => {
|
||||
try {
|
||||
setIsLoadingDefault(true);
|
||||
setDefaultLoadError(null);
|
||||
// Using allorigins proxy to avoid Dropbox CORS issues and improve reliability
|
||||
const dropboxUrl = 'https://dl.dropboxusercontent.com/scl/fi/nkyabkq2jdwfudof2ovrl/Data-Matrix.xlsx?rlkey=1cz5fuabwipqqivihii7sybw0&st=io0g4wvb&dl=1';
|
||||
const proxyUrl = `https://api.allorigins.win/raw?url=${encodeURIComponent(dropboxUrl)}`;
|
||||
|
||||
const response = await fetch(proxyUrl);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch file: ${response.statusText}`);
|
||||
// Use different proxies as fallbacks
|
||||
const proxies = [
|
||||
(url: string) => `https://api.allorigins.win/raw?url=${encodeURIComponent(url)}`,
|
||||
(url: string) => `https://cors-anywhere.herokuapp.com/${url}`, // Often needs manual activation but we'll try
|
||||
(url: string) => `https://thingproxy.freeboard.io/fetch/${url}`
|
||||
];
|
||||
|
||||
const dropboxUrl = 'https://dl.dropboxusercontent.com/scl/fi/nkyabkq2jdwfudof2ovrl/Data-Matrix.xlsx?rlkey=1cz5fuabwipqqivihii7sybw0&st=io0g4wvb&dl=1';
|
||||
|
||||
setIsLoadingDefault(true);
|
||||
setDefaultLoadError(null);
|
||||
|
||||
let lastError = null;
|
||||
|
||||
for (const getProxyUrl of proxies) {
|
||||
try {
|
||||
const proxyUrl = getProxyUrl(dropboxUrl);
|
||||
console.log(`Attempting to fetch via: ${proxyUrl}`);
|
||||
|
||||
const response = await fetch(proxyUrl);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Proxy returned status ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
if (arrayBuffer.byteLength < 100) {
|
||||
throw new Error("File too small, possibly empty or error page");
|
||||
}
|
||||
|
||||
const wb = XLSX.read(arrayBuffer, { type: 'array' });
|
||||
const wsname = wb.SheetNames[0];
|
||||
const ws = wb.Sheets[wsname];
|
||||
const data = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1 });
|
||||
|
||||
if (data.length > 0) {
|
||||
setAppState({
|
||||
headers: data[0],
|
||||
data: data.slice(1),
|
||||
fileName: 'Data-Matrix.xlsx (Cloud Sync)',
|
||||
fileDate: new Date(),
|
||||
hasUnsavedChanges: false
|
||||
});
|
||||
setActiveModule('descriptions');
|
||||
setIsLoadingDefault(false);
|
||||
return; // Success!
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Failed with proxy:`, err);
|
||||
lastError = err;
|
||||
}
|
||||
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
const wb = XLSX.read(arrayBuffer, { type: 'array' });
|
||||
const wsname = wb.SheetNames[0];
|
||||
const ws = wb.Sheets[wsname];
|
||||
const data = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1 });
|
||||
|
||||
if (data.length > 0) {
|
||||
setAppState({
|
||||
headers: data[0],
|
||||
data: data.slice(1),
|
||||
fileName: 'Data-Matrix.xlsx (Auto-loaded Google Drive/Dropbox)',
|
||||
fileDate: new Date(),
|
||||
hasUnsavedChanges: false
|
||||
});
|
||||
setActiveModule('descriptions');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error loading default data:', err);
|
||||
setDefaultLoadError(err instanceof Error ? err.message : 'Unknown error');
|
||||
} finally {
|
||||
setIsLoadingDefault(false);
|
||||
}
|
||||
|
||||
// If we reach here, all proxies failed
|
||||
setDefaultLoadError(lastError instanceof Error ? lastError.message : 'All connection attempts failed');
|
||||
setIsLoadingDefault(false);
|
||||
};
|
||||
|
||||
loadDefaultData();
|
||||
|
||||
Reference in New Issue
Block a user