INNER CODE UNIT · JavaScript
handleDownload
sebastiancarlos/beachpatrol · beachpatrol.js:322
const handleDownload = async (download) => {
let filename = download.suggestedFilename();
console.log(`Got download event for: ${filename}`);
let savePath = path.join(DOWNLOAD_DIR, filename);
let counter = 0;
// Iterate increasing the counter in the filename until there is no name clash.
while (fs.existsSync(savePath)) {
const ext = path.extname(filename);
const base = path.basename(filename, ext);
counter++;
filename = `${base} (${counter})${ext}`;
savePath = path.join(DOWNLOAD_DIR, filename);
}
console.log(
`Attempting to download: ${filename} (as ${filename}) to ${DOWNLOAD_DIR}`,