Code: Select all
const fs = require('fs');
const path = require('path');
async function downloadFolder(folderName, folderStructure, destinationPath) {
const folderPath = path.join(destinationPath, folderName);
if (!fs.existsSync(folderPath)) {
try {
fs.mkdirSync(folderPath, { recursive: true });
} catch (err) {
throw new Error(`Failed to create folder at ${folderPath}: ${err.message}`);
}
}
try {
const downloadPromises = folderStructure.map((item) => {
const itemPath = path.join(folderPath, item.name);
console.log(`Starting download for: ${itemPath}`);
if (item.type === 'file') {
return fs.promises.writeFile(itemPath, item.content)
.then(() => console.log(`Downloaded: ${itemPath}`))
.catch((err) => {
throw new Error(`Failed to write file at ${itemPath}: ${err.message}`);
});
} else if (item.type === 'folder') {
return downloadFolder(item.name, item.children, folderPath); // Recurse for subfolder
}
});
// Wait for all download promises to resolve concurrently
await Promise.all(downloadPromises);
console.log(`All items in ${folderName} downloaded successfully!`);
} catch (err) {
throw new Error(`Error processing folder ${folderName}: ${err.message}`);
}
}
Code: Select all
folderStructure = [
{ name: '100-mb-example-jpg.jpg', type: 'file', content: /* file data */ },
{ name: '50mb.jpg', type: 'file', content: /* file data */ },
{ name: '70mb.iso', type: 'file', content: /* file data */ }
];