salix/modules/item/back/methods/item-image-queue/download.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

const axios = require('axios');
const fs = require('fs/promises');
const {createWriteStream} = require('fs');
const path = require('path');
module.exports = Self => {
Self.remoteMethod('download', {
description: 'Processes the image download queue',
accessType: 'WRITE',
http: {
path: `/download`,
2023-03-16 06:18:29 +00:00
verb: 'POST',
},
//accessScopes: ['read:multimedia']
});
Self.download = async() => {
const models = Self.app.models;
2023-03-16 06:18:29 +00:00
const tempContainer = await models.TempContainer.container(
'salix-image'
);
const tempPath = path.join(
tempContainer.client.root,
tempContainer.name
);
const maxAttempts = 3;
const collectionName = 'catalog';
let tempFilePath;
let queueRow;
try {
2023-03-16 06:18:29 +00:00
queueRow = await Self.findOne(
{
fields: ['id', 'itemFk', 'url', 'attempts'],
where: {
url: {neq: null},
2023-03-16 06:18:29 +00:00
attempts: {
lt: maxAttempts,
},
},
order: 'priority, attempts, updated',
}
2023-03-16 06:18:29 +00:00
);
if (!queueRow) return;
const fileName = `${queueRow.itemFk}.png`;
tempFilePath = path.join(tempPath, fileName);
// Download remote image
const response = await axios.get(queueRow.url, {
responseType: 'stream',
});
const writeStream = createWriteStream(tempFilePath);
await new Promise((resolve, reject) => {
writeStream.on('open', () => response.data.pipe(writeStream));
writeStream.on('finish', () => resolve());
writeStream.on('error', error => reject(error));
});
await models.Image.resize({
collectionName: collectionName,
srcFile: tempFilePath,
fileName: fileName,
entityId: queueRow.itemFk
});
2023-03-16 06:18:29 +00:00
try {
await fs.unlink(tempFilePath);
2023-03-16 06:18:29 +00:00
} catch (error) { }
await queueRow.destroy();
// Restart queue
Self.download();
} catch (error) {
if (queueRow.attempts < maxAttempts) {
await queueRow.updateAttributes({
error: error,
attempts: queueRow.attempts + 1,
2023-03-16 06:18:29 +00:00
updated: Date.vnNew(),
});
}
try {
await fs.unlink(tempFilePath);
2023-03-16 06:18:29 +00:00
} catch (error) { }
Self.download();
}
};
};