- Continuazione del Catalogo
This commit is contained in:
@@ -60,28 +60,59 @@ async function downloadImgIfMissing(productInfo) {
|
||||
if (!productInfo.image_link)
|
||||
return { prodInfo: null, aggiornatoimg: false };
|
||||
|
||||
const relativeimg = productInfo.image_link.split('/').pop();
|
||||
const img = 'upload/products/' + productInfo.image_link.split('/').pop();
|
||||
const savePath = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
|
||||
|
||||
if (!productInfo.imagefile || !fs.existsSync(savePath)) {
|
||||
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) {
|
||||
// Download image from the URL productInfo.image_link
|
||||
productInfo.imagefile = img;
|
||||
productInfo.imagefile = relativeimg;
|
||||
|
||||
const downloader = new ImageDownloader();
|
||||
|
||||
const aggiornatoimg = await downloader.downloadImage(productInfo.image_link, savePath).then(result => {
|
||||
if (result) {
|
||||
// console.log('Download completato con successo!');
|
||||
} else {
|
||||
console.log('Download non riuscito.');
|
||||
}
|
||||
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 result;
|
||||
|
||||
});
|
||||
});
|
||||
return { prodInfo: productInfo, aggiornatoimg };
|
||||
}
|
||||
|
||||
|
||||
return { prodInfo: null, aggiornatoimg: false };
|
||||
}
|
||||
|
||||
@@ -1046,7 +1077,7 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
if (productInfo.productTypes.length === 1 && productInfo.productTypes[0] === undefined) {
|
||||
productInfo.productTypes = [shared_consts.PRODUCTTYPE.PRODUCT];
|
||||
}
|
||||
|
||||
|
||||
let esisteindb = await ProductInfo.findOne({ code: productInfo.code }).lean();
|
||||
|
||||
// Update ProductInfo
|
||||
|
||||
Reference in New Issue
Block a user