Fix csv update/import on settings page + lots of cors work
This commit is contained in:
@@ -38,17 +38,42 @@ function downloadFile(url, filePath) {
|
||||
|
||||
const totalSize = parseInt(response.headers['content-length'], 10);
|
||||
let downloadedSize = 0;
|
||||
let lastProgressUpdate = Date.now();
|
||||
const startTime = Date.now();
|
||||
|
||||
response.on('data', chunk => {
|
||||
downloadedSize += chunk.length;
|
||||
const progress = (downloadedSize / totalSize * 100).toFixed(2);
|
||||
process.stdout.write(`\rDownloading ${path.basename(filePath)}: ${progress}%`);
|
||||
const now = Date.now();
|
||||
// Update progress at most every 100ms to avoid console flooding
|
||||
if (now - lastProgressUpdate > 100) {
|
||||
const elapsed = (now - startTime) / 1000;
|
||||
const rate = downloadedSize / elapsed;
|
||||
const remaining = (totalSize - downloadedSize) / rate;
|
||||
|
||||
console.log(JSON.stringify({
|
||||
status: 'running',
|
||||
operation: `Downloading ${path.basename(filePath)}`,
|
||||
current: downloadedSize,
|
||||
total: totalSize,
|
||||
rate: (rate / 1024 / 1024).toFixed(2), // MB/s
|
||||
elapsed: formatDuration(elapsed),
|
||||
remaining: formatDuration(remaining),
|
||||
percentage: ((downloadedSize / totalSize) * 100).toFixed(1)
|
||||
}));
|
||||
lastProgressUpdate = now;
|
||||
}
|
||||
});
|
||||
|
||||
response.pipe(file);
|
||||
|
||||
file.on('finish', () => {
|
||||
process.stdout.write('\n');
|
||||
console.log(JSON.stringify({
|
||||
status: 'running',
|
||||
operation: `Completed ${path.basename(filePath)}`,
|
||||
current: totalSize,
|
||||
total: totalSize,
|
||||
percentage: '100'
|
||||
}));
|
||||
file.close();
|
||||
resolve();
|
||||
});
|
||||
@@ -64,34 +89,79 @@ function downloadFile(url, filePath) {
|
||||
});
|
||||
}
|
||||
|
||||
// Helper function to format duration
|
||||
function formatDuration(seconds) {
|
||||
if (seconds < 60) return `${Math.round(seconds)}s`;
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
seconds = Math.round(seconds % 60);
|
||||
return `${minutes}m ${seconds}s`;
|
||||
}
|
||||
|
||||
// Main function to update all files
|
||||
async function updateFiles() {
|
||||
console.log('Starting CSV file updates...');
|
||||
console.log(JSON.stringify({
|
||||
status: 'running',
|
||||
operation: 'Starting CSV file updates',
|
||||
total: FILES.length,
|
||||
current: 0
|
||||
}));
|
||||
|
||||
for (const file of FILES) {
|
||||
for (let i = 0; i < FILES.length; i++) {
|
||||
const file = FILES[i];
|
||||
const filePath = path.join(CSV_DIR, file.name);
|
||||
|
||||
try {
|
||||
// Delete existing file if it exists
|
||||
if (fs.existsSync(filePath)) {
|
||||
console.log(`Removing existing file: ${file.name}`);
|
||||
console.log(JSON.stringify({
|
||||
status: 'running',
|
||||
operation: `Removing existing file: ${file.name}`,
|
||||
current: i,
|
||||
total: FILES.length,
|
||||
percentage: ((i / FILES.length) * 100).toFixed(1)
|
||||
}));
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
|
||||
// Download new file
|
||||
console.log(`Downloading ${file.name}...`);
|
||||
console.log(JSON.stringify({
|
||||
status: 'running',
|
||||
operation: `Starting download: ${file.name}`,
|
||||
current: i,
|
||||
total: FILES.length,
|
||||
percentage: ((i / FILES.length) * 100).toFixed(1)
|
||||
}));
|
||||
await downloadFile(file.url, filePath);
|
||||
console.log(`Successfully updated ${file.name}`);
|
||||
console.log(JSON.stringify({
|
||||
status: 'running',
|
||||
operation: `Successfully updated ${file.name}`,
|
||||
current: i + 1,
|
||||
total: FILES.length,
|
||||
percentage: (((i + 1) / FILES.length) * 100).toFixed(1)
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error(`Error updating ${file.name}:`, error.message);
|
||||
console.error(JSON.stringify({
|
||||
status: 'error',
|
||||
operation: `Error updating ${file.name}`,
|
||||
error: error.message
|
||||
}));
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('CSV file update complete!');
|
||||
console.log(JSON.stringify({
|
||||
status: 'complete',
|
||||
operation: 'CSV file update complete',
|
||||
current: FILES.length,
|
||||
total: FILES.length,
|
||||
percentage: '100'
|
||||
}));
|
||||
}
|
||||
|
||||
// Run the update
|
||||
updateFiles().catch(error => {
|
||||
console.error('Update failed:', error);
|
||||
console.error(JSON.stringify({
|
||||
error: `Update failed: ${error.message}`
|
||||
}));
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user