From 6f3fb83cd75b121f703e979888b0eb05068fabbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Thu, 27 Aug 2020 13:53:11 +0200 Subject: [PATCH] Changes --- back/models/image.js | 4 +++- .../back/methods/item-image-queue/downloadImages.js | 11 +++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/back/models/image.js b/back/models/image.js index 340b2e5a6..313e4492b 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -90,7 +90,9 @@ module.exports = Self => { ); } - await fs.unlink(srcFilePath); + if (fs.existsSync(filePath)) + await fs.unlink(srcFilePath); + await tx.commit(); return newImage; } catch (e) { diff --git a/modules/item/back/methods/item-image-queue/downloadImages.js b/modules/item/back/methods/item-image-queue/downloadImages.js index c49cb0702..d3fd739cc 100644 --- a/modules/item/back/methods/item-image-queue/downloadImages.js +++ b/modules/item/back/methods/item-image-queue/downloadImages.js @@ -19,7 +19,6 @@ module.exports = Self => { Self.downloadImages = async() => { const models = Self.app.models; - let image; try { const tempPath = path.join('/tmp/salix-image'); @@ -27,7 +26,7 @@ module.exports = Self => { await fs.mkdir(tempPath, {recursive: true}); const timer = setInterval(async() => { - image = await Self.findOne({where: {error: null}}); + const image = await Self.findOne({where: {error: null}}); // Exit loop if (!image) return clearInterval(timer); @@ -62,9 +61,9 @@ module.exports = Self => { await errorHandler(image.itemFk, error, filePath); } }); - }, 1500); + }, 2500); } catch (error) { - await errorHandler(image.itemFk, error); + throw new Error('Try-catch error: ', error); } async function errorHandler(rowId, error, filePath) { @@ -76,10 +75,10 @@ module.exports = Self => { await row.updateAttribute('error', error); - if (filePath) + if (filePath && fs.existsSync(filePath)) await fs.unlink(filePath); } catch (error) { - throw error; + throw new Error('ErrorHandler error: ', error); } } };