diff --git a/back/models/image.js b/back/models/image.js index 079acd293..340b2e5a6 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -55,8 +55,9 @@ module.exports = Self => { }; await fs.mkdir(dstDir, {recursive: true}); - await sharp(srcFilePath) + await sharp(srcFilePath, {failOnError: false}) .resize(collection.maxWidth, collection.maxHeight, resizeOpts) + .png() .toFile(dstFile); const sizes = collection.sizes(); @@ -69,8 +70,9 @@ module.exports = Self => { }; await fs.mkdir(dstDir, {recursive: true}); - await sharp(srcFilePath) + await sharp(srcFilePath, {failOnError: false}) .resize(size.width, size.height, resizeOpts) + .png() .toFile(dstFile); } diff --git a/modules/item/back/methods/item-image-queue/downloadImages.js b/modules/item/back/methods/item-image-queue/downloadImages.js index e27b4516a..bd22bc281 100644 --- a/modules/item/back/methods/item-image-queue/downloadImages.js +++ b/modules/item/back/methods/item-image-queue/downloadImages.js @@ -20,45 +20,48 @@ module.exports = Self => { const models = Self.app.models; try { - const imageQueue = await Self.find({where: {error: null}, limit: 25}); - /* const tempPath = path.join('/tmp/salix-image'); */ - const rootPath = models.Image.getPath(); - const tempPath = path.join(rootPath, 'temp'); + const tempPath = path.join('/tmp/salix-image'); // Create temporary path await fs.mkdir(tempPath, {recursive: true}); - for (let image of imageQueue) { + const timer = setInterval(async() => { + const image = await Self.findOne({where: {error: null}}); + + // Exit loop + if (!image) return clearInterval(timer); + const fileName = `${image.itemFk}.png`; const filePath = path.join(tempPath, fileName); - const file = fs.createWriteStream(filePath); - https.get(image.url, async response => { - if (response.statusCode != 200) { - const error = new Error(`Could not download the image. Status code ${response.statusCode}`); + const writeStream = fs.createWriteStream(filePath); + writeStream.on('open', () => { + https.get(image.url, async response => { + if (response.statusCode != 200) { + const error = new Error(`Could not download the image. Status code ${response.statusCode}`); - file.close(); - await errorHandler(image.itemFk, error, filePath); - } - - response.pipe(file); - - file.on('error', async error => { - await errorHandler(image.itemFk, error, filePath); - }); - - file.on('finish', async function() { - try { - await models.Image.registerImage('catalog', fileName, filePath); - await image.destroy(); - } catch (error) { - await errorHandler(image.itemFk, error, filePath); + return await errorHandler(image.itemFk, error, filePath); } + + response.pipe(writeStream); + }).on('error', async error => { + await errorHandler(image.itemFk, error, filePath); }); - }).on('error', async error => { + }); + + writeStream.on('error', async error => { await errorHandler(image.itemFk, error, filePath); }); - } + + writeStream.on('finish', async function() { + try { + await models.Image.registerImage('catalog', fileName, filePath); + await image.destroy(); + } catch (error) { + await errorHandler(image.itemFk, error, filePath); + } + }); + }, 1000); } catch (error) { await errorHandler(image.itemFk, error); }