2023-03-15 13:24:30 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
const fs = require('fs/promises');
|
2023-04-14 11:04:40 +00:00
|
|
|
const {createWriteStream} = require('fs');
|
2023-03-15 13:24:30 +00:00
|
|
|
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',
|
|
|
|
},
|
2024-04-17 07:52:53 +00:00
|
|
|
accessScopes: ['read:multimedia']
|
2023-03-15 13:24:30 +00:00
|
|
|
});
|
|
|
|
|
2023-04-14 11:04:40 +00:00
|
|
|
Self.download = async() => {
|
2023-03-15 13:24:30 +00:00
|
|
|
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
|
|
|
|
);
|
2023-03-15 13:24:30 +00:00
|
|
|
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: {
|
2023-04-14 11:04:40 +00:00
|
|
|
url: {neq: null},
|
2023-03-16 06:18:29 +00:00
|
|
|
attempts: {
|
|
|
|
lt: maxAttempts,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
order: 'priority, attempts, updated',
|
2023-04-14 11:04:40 +00:00
|
|
|
}
|
2023-03-16 06:18:29 +00:00
|
|
|
);
|
2023-03-15 13:24:30 +00:00
|
|
|
|
|
|
|
if (!queueRow) return;
|
|
|
|
|
2023-04-14 11:04:40 +00:00
|
|
|
const fileName = `${queueRow.itemFk}.png`;
|
2023-03-15 13:24:30 +00:00
|
|
|
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));
|
|
|
|
});
|
|
|
|
|
2023-04-14 11:04:40 +00:00
|
|
|
await models.Image.resize({
|
|
|
|
collectionName: collectionName,
|
|
|
|
srcFile: tempFilePath,
|
|
|
|
fileName: fileName,
|
|
|
|
entityId: queueRow.itemFk
|
2023-03-15 13:24:30 +00:00
|
|
|
});
|
|
|
|
|
2023-03-16 06:18:29 +00:00
|
|
|
try {
|
2023-03-15 13:24:30 +00:00
|
|
|
await fs.unlink(tempFilePath);
|
2023-03-16 06:18:29 +00:00
|
|
|
} catch (error) { }
|
2023-03-15 13:24:30 +00:00
|
|
|
|
2023-04-14 11:04:40 +00:00
|
|
|
await queueRow.destroy();
|
2023-03-15 13:24:30 +00:00
|
|
|
|
|
|
|
// 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(),
|
2023-03-15 13:24:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await fs.unlink(tempFilePath);
|
2023-03-16 06:18:29 +00:00
|
|
|
} catch (error) { }
|
2023-03-15 13:24:30 +00:00
|
|
|
|
|
|
|
Self.download();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|