- Ricerca Titolo per nome o autore o ISBN o codice articolo

This commit is contained in:
Surya Paolo
2025-03-31 23:56:01 +02:00
parent 789dc1dcae
commit 5431fe118e
9 changed files with 345 additions and 114 deletions

View File

@@ -433,7 +433,8 @@ class ImageDownloader {
initialDelay = 1000, // Ritardo iniziale
maxDelay = 10000, // Ritardo massimo
timeout = 30000, // Timeout della richiesta
validateContentType = true // Verifica del tipo di contenuto
validateContentType = true, // Verifica del tipo di contenuto
nomefileoriginale = true,
} = options;
// Funzione per il backoff esponenziale
@@ -448,7 +449,6 @@ class ImageDownloader {
fs.unlinkSync(filepath);
}
const writer = fs.createWriteStream(filepath);
if (attempt > 1)
console.log(`📥 Tentativo ${attempt}/${maxRetries} - Scaricamento: ${url}`);
@@ -487,6 +487,28 @@ class ImageDownloader {
downloadedBytes += chunk.length;
});
let writer = null;
if (nomefileoriginale) {
// Estrai il nome del file dall'URL o da Content-Disposition
//let fileName = this.extractFileNameFromUrl(url) || this.extractFileNameFromHeaders(response.headers);
let fileName = path.basename(response.data.responseUrl);
// Se il nome del file non è specificato, genera un nome predefinito
if (!fileName) {
fileName = `image_${Date.now()}.jpg`;
}
// Genera il percorso completo del file
const fullPath = path.join(path.dirname(filepath), fileName);
filepath = fullPath;
}
// Scrivi il file sul disco
writer = fs.createWriteStream(filepath);
response.data.pipe(writer);
await new Promise((resolve, reject) => {
@@ -501,8 +523,11 @@ class ImageDownloader {
});
});
console.info(`✅ Immagine scaricata con successo in ${filepath}`);
return true;
return { ris: true, filepath };
} catch (error) {
console.error(`❌ Errore nel tentativo ${attempt}/${maxRetries}:`, error.message);
@@ -514,7 +539,7 @@ class ImageDownloader {
if (attempt === maxRetries) {
console.error(`Download fallito dopo ${maxRetries} tentativi: ${error.message}`);
return false;
return { ris: false };
}
const delay = getDelay(attempt);
@@ -523,6 +548,24 @@ class ImageDownloader {
}
}
}
// Funzione per estrarre il nome del file dall'URL
extractFileNameFromUrl(url) {
const match = url.match(/\/([^/?#]+)(?:[?#]|$)/);
return match ? decodeURIComponent(match[1]) : null;
}
// Funzione per estrarre il nome del file da Content-Disposition
extractFileNameFromHeaders(headers) {
const contentDisposition = headers['content-disposition'];
if (contentDisposition) {
const match = contentDisposition.match(/filename="([^"]+)"/);
if (match) {
return decodeURIComponent(match[1]);
}
}
return null;
}
}
@@ -3380,7 +3423,7 @@ module.exports = {
try {
console.time(name);
} catch (e) {
}
}
},