- newsletter: prende la lista utenti (flag news_on)
- Abilita a Tutti la Newsletter news_on - isCommerciale - JobsInProgress - PCB: Corretto Totali che era a zero
This commit is contained in:
@@ -537,6 +537,11 @@ class ImageDownloader {
|
||||
fs.unlinkSync(filepath);
|
||||
}
|
||||
|
||||
// se in error.message c'è '404' allora esci e ritorna code: 404
|
||||
if (error.message.includes('404')) {
|
||||
return { ris: false, code: 404 };
|
||||
}
|
||||
|
||||
if (attempt === maxRetries) {
|
||||
console.error(`Download fallito dopo ${maxRetries} tentativi: ${error.message}`);
|
||||
return { ris: false };
|
||||
@@ -6109,28 +6114,30 @@ module.exports = {
|
||||
|
||||
async downloadImgIfMissing(productInfo) {
|
||||
|
||||
const ProductInfo = require('../models/productInfo');
|
||||
|
||||
try {
|
||||
if (this.sulServer()) {
|
||||
dirmain = '';
|
||||
} else {
|
||||
dirmain = server_constants.DIR_PUBLIC_LOCALE;
|
||||
}
|
||||
|
||||
|
||||
const vecchiomodo = false;
|
||||
|
||||
|
||||
if (productInfo.image_link && vecchiomodo) {
|
||||
|
||||
|
||||
const relativeimg = productInfo.image_link.split('/').pop();
|
||||
const img = this.getdirByIdApp(productInfo.idapp) + dirmain +
|
||||
server_constants.DIR_UPLOAD + '/products/' + productInfo.image_link.split('/').pop();
|
||||
const savePath = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
|
||||
|
||||
|
||||
let scaricaimg = !productInfo.imagefile || !fs.existsSync(savePath);
|
||||
|
||||
|
||||
if (!productInfo.imagefile && fs.existsSync(savePath)) {
|
||||
// esiste il file, ma sul DB non è corretto
|
||||
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
|
||||
|
||||
|
||||
if (stats.size > 0) { // Controlla se la dimensione del file è maggiore di zero
|
||||
// Esiste il file ed è non vuoto, ma sul DB non è corretto
|
||||
productInfo.imagefile = relativeimg;
|
||||
@@ -6139,23 +6146,24 @@ module.exports = {
|
||||
scaricaimg = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (productInfo.imagefile && fs.existsSync(savePath)) {
|
||||
// esiste il file, ma sul DB non è corretto
|
||||
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
|
||||
|
||||
|
||||
if (stats.size <= 0) { // Controlla se la dimensione del file è maggiore di zero
|
||||
scaricaimg = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (scaricaimg && vecchiomodo) {
|
||||
// Download image from the URL productInfo.image_link
|
||||
productInfo.imagefile = relativeimg;
|
||||
|
||||
|
||||
const downloader = new ImageDownloader();
|
||||
|
||||
|
||||
const aggiornatoimg = await downloader.downloadImage(productInfo.image_link, savePath,
|
||||
{
|
||||
maxRetries: 1,
|
||||
@@ -6167,80 +6175,82 @@ module.exports = {
|
||||
} else {
|
||||
console.log('Download non riuscito.');
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
|
||||
|
||||
});
|
||||
return { prodInfo: productInfo, aggiornatoimg: aggiornatoimg.ris };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let fileesistente = false;
|
||||
if (productInfo.imagefile) {
|
||||
// controlla se esiste il file
|
||||
const img = this.getdirByIdApp(productInfo.idapp) + dirmain +
|
||||
server_constants.DIR_UPLOAD + '/products/' + productInfo.imagefile.split('/').pop();
|
||||
const filecompleto = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
|
||||
|
||||
|
||||
// Se non esiste lo scarico !
|
||||
fileesistente = fs.existsSync(filecompleto);
|
||||
}
|
||||
|
||||
|
||||
if (!vecchiomodo && (!productInfo.image_link || !fileesistente)) {
|
||||
|
||||
|
||||
let scarica_da_sito = !productInfo.imagefile;
|
||||
|
||||
|
||||
if (!scarica_da_sito && productInfo.imagefile) {
|
||||
scarica_da_sito = !fileesistente; // Se non esiste lo scarico !
|
||||
}
|
||||
|
||||
if (scarica_da_sito) {
|
||||
|
||||
if (scarica_da_sito && !productInfo.image_not_found) {
|
||||
// date and time
|
||||
productInfo.imagefile = 'img_' + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
||||
const img = this.getdirByIdApp(productInfo.idapp) + dirmain +
|
||||
server_constants.DIR_UPLOAD + '/products/' + productInfo.imagefile.split('/').pop();
|
||||
let savePath = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
|
||||
|
||||
|
||||
|
||||
|
||||
let link = 'https://www.gruppomacro.com/copertine.php?id_gm=' + productInfo.sku
|
||||
|
||||
|
||||
const downloader = new ImageDownloader();
|
||||
|
||||
|
||||
const aggiornatoimg = await downloader.downloadImage(link, savePath,
|
||||
{
|
||||
maxRetries: 1,
|
||||
initialDelay: 300,
|
||||
timeout: 15000,
|
||||
nomefileoriginale: true,
|
||||
}).then(result => {
|
||||
if (result) {
|
||||
// console.log('Download completato con successo!');
|
||||
} else {
|
||||
console.log('Download non riuscito.');
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
});
|
||||
if (aggiornatoimg.filepath) {
|
||||
|
||||
|
||||
let aggiornatoimg;
|
||||
try {
|
||||
aggiornatoimg = await downloader.downloadImage(link, savePath,
|
||||
{
|
||||
maxRetries: 1,
|
||||
initialDelay: 300,
|
||||
timeout: 15000,
|
||||
nomefileoriginale: true,
|
||||
});
|
||||
} catch (e) {
|
||||
aggiornatoimg = { ris: false };
|
||||
}
|
||||
if (aggiornatoimg?.code === 404) {
|
||||
// non trovato quindi la prossima volta non richiederlo
|
||||
await ProductInfo.setImgNotFound(productInfo._id);
|
||||
}
|
||||
|
||||
|
||||
if (aggiornatoimg?.filepath) {
|
||||
const filenamebase = path.basename(aggiornatoimg.filepath);
|
||||
// const img = '/upload/products/' + filenamebase;
|
||||
productInfo.imagefile = filenamebase;
|
||||
}
|
||||
|
||||
|
||||
return { prodInfo: productInfo, aggiornatoimg: aggiornatoimg.ris };
|
||||
} else {
|
||||
return { prodInfo: null, aggiornatoimg: false };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch (e) {
|
||||
console.error('downloadImgIfMissing', e.message);
|
||||
}
|
||||
|
||||
|
||||
return { prodInfo: null, aggiornatoimg: false };
|
||||
|
||||
|
||||
},
|
||||
|
||||
removeAccents(mystr) {
|
||||
@@ -6257,7 +6267,7 @@ module.exports = {
|
||||
|
||||
return Array.from(mystr).map(char => accentsMap.get(char) || char).join('');
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user