From 596a017cfdb9d675c57aff3929cb9c0097021e43 Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 16 Mar 2023 07:18:29 +0100 Subject: [PATCH] Don't throw error on file unlink --- .../back/methods/item-image-queue/download.js | 122 ++++++++++-------- 1 file changed, 66 insertions(+), 56 deletions(-) diff --git a/modules/item/back/methods/item-image-queue/download.js b/modules/item/back/methods/item-image-queue/download.js index 527f9ba65..cdc0fe049 100644 --- a/modules/item/back/methods/item-image-queue/download.js +++ b/modules/item/back/methods/item-image-queue/download.js @@ -1,7 +1,7 @@ const axios = require('axios'); const uuid = require('uuid'); const fs = require('fs/promises'); -const {createWriteStream} = require('fs'); +const { createWriteStream } = require('fs'); const path = require('path'); const gm = require('gm'); @@ -11,14 +11,19 @@ module.exports = Self => { accessType: 'WRITE', http: { path: `/download`, - verb: 'POST' - } + verb: 'POST', + }, }); - Self.download = async() => { + Self.download = async () => { const models = Self.app.models; - const tempContainer = await models.TempContainer.container('salix-image'); - const tempPath = path.join(tempContainer.client.root, tempContainer.name); + const tempContainer = await models.TempContainer.container( + 'salix-image' + ); + const tempPath = path.join( + tempContainer.client.root, + tempContainer.name + ); const maxAttempts = 3; const collectionName = 'catalog'; @@ -27,57 +32,60 @@ module.exports = Self => { let tempFilePath; let queueRow; try { - const myOptions = {transaction: tx}; + const myOptions = { transaction: tx }; - queueRow = await Self.findOne({ - fields: [ - 'id', - 'itemFk', - 'url', - 'attempts' - ], - where: { - url: {neq: null}, - attempts: { - lt: maxAttempts - } + queueRow = await Self.findOne( + { + fields: ['id', 'itemFk', 'url', 'attempts'], + where: { + url: { neq: null }, + attempts: { + lt: maxAttempts, + }, + }, + order: 'priority, attempts, updated', }, - order: 'priority, attempts, updated' - }, myOptions); + myOptions + ); if (!queueRow) return; - const collection = await models.ImageCollection.findOne({ - fields: [ - 'id', - 'maxWidth', - 'maxHeight', - 'model', - 'property' - ], - where: {name: collectionName}, - include: { - relation: 'sizes', - scope: { - fields: ['width', 'height', 'crop'] - } - } - }, myOptions); + const collection = await models.ImageCollection.findOne( + { + fields: [ + 'id', + 'maxWidth', + 'maxHeight', + 'model', + 'property', + ], + where: { name: collectionName }, + include: { + relation: 'sizes', + scope: { + fields: ['width', 'height', 'crop'], + }, + }, + }, + myOptions + ); const fileName = `${uuid.v4()}.png`; tempFilePath = path.join(tempPath, fileName); // Insert image row - await models.Image.create({ - name: fileName, - collectionFk: collectionName, - updated: Date.vnNow() - }, myOptions); + await models.Image.create( + { + name: fileName, + collectionFk: collectionName, + updated: Date.vnNow(), + }, + myOptions + ); // Update item const model = models[collection.model]; - if (!model) - throw new Error('No matching model found'); + if (!model) throw new Error('No matching model found'); const item = await model.findById(queueRow.itemFk, null, myOptions); if (item) { @@ -101,21 +109,23 @@ module.exports = Self => { }); // Resize - const container = await models.ImageContainer.container(collectionName); + const container = await models.ImageContainer.container( + collectionName + ); const rootPath = container.client.root; const collectionDir = path.join(rootPath, collectionName); // To max size - const {maxWidth, maxHeight} = collection; + const { maxWidth, maxHeight } = collection; const fullSizePath = path.join(collectionDir, 'full'); const toFullSizePath = `${fullSizePath}/${fileName}`; - await fs.mkdir(fullSizePath, {recursive: true}); + await fs.mkdir(fullSizePath, { recursive: true }); await new Promise((resolve, reject) => { gm(tempFilePath) .resize(maxWidth, maxHeight, '>') .setFormat('png') - .write(toFullSizePath, function(err) { + .write(toFullSizePath, function (err) { if (err) reject(err); if (!err) resolve(); }); @@ -123,12 +133,12 @@ module.exports = Self => { // To collection sizes for (const size of collection.sizes()) { - const {width, height} = size; + const { width, height } = size; const sizePath = path.join(collectionDir, `${width}x${height}`); const toSizePath = `${sizePath}/${fileName}`; - await fs.mkdir(sizePath, {recursive: true}); + await fs.mkdir(sizePath, { recursive: true }); await new Promise((resolve, reject) => { const gmInstance = gm(tempFilePath); @@ -139,20 +149,20 @@ module.exports = Self => { .crop(width, height); } - if (!size.crop) - gmInstance.resize(width, height, '>'); + if (!size.crop) gmInstance.resize(width, height, '>'); gmInstance .setFormat('png') - .write(toSizePath, function(err) { + .write(toSizePath, function (err) { if (err) reject(err); if (!err) resolve(); }); }); } - if (await fs.stat(tempFilePath)) + try { await fs.unlink(tempFilePath); + } catch (error) { } await queueRow.destroy(myOptions); @@ -167,13 +177,13 @@ module.exports = Self => { await queueRow.updateAttributes({ error: error, attempts: queueRow.attempts + 1, - updated: Date.vnNew() + updated: Date.vnNew(), }); } try { await fs.unlink(tempFilePath); - } catch (error) {} + } catch (error) { } Self.download(); }