- MultiDominio: api.riso.app, api.gruppomacro.app

This commit is contained in:
Surya Paolo
2024-07-23 12:25:10 +02:00
parent 017ac82d8b
commit 21862f87a1
17 changed files with 484 additions and 97 deletions

View File

@@ -399,6 +399,70 @@ const textlang = {
},
};
/**
* Scarica un'immagine da una URL e la salva in una directory locale
* @param {string} url - L'URL dell'immagine da scaricare
* @param {string} filepath - Il percorso dove salvare l'immagine scaricata
*/
class ImageDownloader {
/**
* Scarica un'immagine da una URL e la salva in una directory locale.
* Tenta di scaricare fino a 3 volte in caso di errore, con un ritardo tra i tentativi.
*
* @param {string} url - L'URL dell'immagine da scaricare
* @param {string} filepath - Il percorso dove salvare l'immagine scaricata
* @param {number} maxRetries - Numero massimo di tentativi in caso di fallimento (default: 3)
* @param {number} delay - Ritardo in millisecondi tra i tentativi (default: 1000)
* @returns {Promise<boolean>}
*/
async downloadImage(url, filepath, maxRetries = 1, delay = 1000) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const writer = fs.createWriteStream(filepath);
console.log('url da scaricare:', url);
const response = await axios({
url,
method: 'GET',
responseType: 'stream',
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
}
});
response.data.pipe(writer);
await new Promise((resolve, reject) => {
writer.on('finish', () => {
console.info('✅ Immagine scaricata ' + url + ' in ' + filepath);
resolve(true);
});
writer.on('error', reject);
});
return true;
} catch (error) {
console.error(`❌ Tentativo ${attempt} fallito per l'URL ${url}. Errore:`, error.message);
if (attempt === maxRetries) {
console.error('❌ Tutti i tentativi sono falliti. Scaricamento interrotto.');
return false;
} else {
console.info(`🔁 Ritentando... (${attempt + 1} di ${maxRetries})`);
await sleep(delay);
}
}
}
}
}
// Funzione per implementare il ritardo tra i tentativi
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports = {
MYAPPS: [],
INITDB_FIRSTIME: true,
@@ -1125,10 +1189,10 @@ module.exports = {
typeid = shared_consts.TypeNotifs.ID_CIRCUIT_COINS_ACCEPTED;
} else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT_SENT) {
typeid = shared_consts.TypeNotifs.ID_CIRCUIT_COINS_ACCEPTED_SENT;
// } else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REFUSE) {
// typeid = shared_consts.TypeNotifs.ID_CIRCUIT_COINS_REFUSED;
//} else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REFUSE_SENT) {
// typeid = shared_consts.TypeNotifs.ID_CIRCUIT_COINS_REFUSED_SENT;
// } else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REFUSE) {
// typeid = shared_consts.TypeNotifs.ID_CIRCUIT_COINS_REFUSED;
//} else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REFUSE_SENT) {
// typeid = shared_consts.TypeNotifs.ID_CIRCUIT_COINS_REFUSED_SENT;
}
}
@@ -5195,5 +5259,7 @@ module.exports = {
}
}
},
ImageDownloader,
};