diff --git a/back/models/image.js b/back/models/image.js index fb1a887e8..079acd293 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -3,7 +3,11 @@ const sharp = require('sharp'); const path = require('path'); module.exports = Self => { - Self.register = async(collectionName, file, srcFilePath) => { + Self.getPath = function() { + return '/var/lib/salix/image'; + }; + + Self.registerImage = async(collectionName, file, srcFilePath) => { const models = Self.app.models; const tx = await Self.beginTransaction({}); const myOptions = {transaction: tx}; @@ -25,11 +29,10 @@ module.exports = Self => { fields: ['width', 'height', 'crop'] } } - }); + }, myOptions); const fileName = file.split('.')[0]; - // const rootPath = 'C:\\Users\\jsanc\\Desktop\\images'; - const rootPath = '/var/lib/salix/image'; + const rootPath = Self.getPath(); const data = { name: fileName, collectionFk: collectionName diff --git a/modules/item/back/methods/item-image-queue/download.js b/modules/item/back/methods/item-image-queue/download.js index c9e68d719..dd1af52c9 100644 --- a/modules/item/back/methods/item-image-queue/download.js +++ b/modules/item/back/methods/item-image-queue/download.js @@ -19,14 +19,9 @@ module.exports = Self => { Self.download = async() => { const models = Self.app.models; - let filePath; try { - const imageQueue = await Self.find({ - limit: 3 - }); - - // const rootPath = 'C:\\Users\\jsanc\\Desktop\\images'; - const rootPath = '/var/lib/salix/image'; + const imageQueue = await Self.find({limit: 25}); + const rootPath = models.Image.getPath(); const tempPath = path.join(rootPath, 'temp'); // Create temporary path @@ -34,21 +29,27 @@ module.exports = Self => { for (let image of imageQueue) { const fileName = `${image.itemFk}.png`; - filePath = path.join(tempPath, fileName); + const filePath = path.join(tempPath, fileName); + const file = fs.createWriteStream(filePath); https.get(image.url, async response => { - // Upload file to temporary path - const file = fs.createWriteStream(filePath); response.pipe(file); + }); - await models.Image.register('catalog', fileName, filePath); + file.on('finish', async function() { + await models.Image.registerImage('catalog', fileName, filePath); await image.destroy(); }); + + file.on('error', err => { + fs.unlink(filePath); + + throw err; + }); } return imageQueue; } catch (e) { - fs.unlink(filePath); throw e; } };