- Aggiornamento dal DB di GM al DB Locale
This commit is contained in:
@@ -6100,7 +6100,149 @@ module.exports = {
|
||||
date: dateObj,
|
||||
timestamp: dateObj.getTime()
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
getDateFromISOString(mydate) {
|
||||
const mydate2 = new Date(mydate)
|
||||
return mydate2
|
||||
},
|
||||
|
||||
async downloadImgIfMissing(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;
|
||||
return { prodInfo: productInfo, aggiornatoimg: true };
|
||||
} else {
|
||||
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: 3,
|
||||
initialDelay: 300,
|
||||
timeout: 15000,
|
||||
}).then(result => {
|
||||
if (result) {
|
||||
// console.log('Download completato con successo!');
|
||||
} 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) {
|
||||
// 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: 3,
|
||||
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) {
|
||||
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 };
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user