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

113 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-07-13 09:58:31 +00:00
const https = require('https');
const fs = require('fs-extra');
const path = require('path');
module.exports = Self => {
2020-07-14 09:16:50 +00:00
Self.remoteMethod('downloadImages', {
2020-07-13 09:58:31 +00:00
description: 'Returns last entries',
accessType: 'WRITE',
returns: {
2020-07-13 12:13:21 +00:00
type: ['Object'],
2020-07-13 09:58:31 +00:00
root: true
},
http: {
2020-07-14 09:16:50 +00:00
path: `/downloadImages`,
verb: 'POST'
2020-07-13 09:58:31 +00:00
}
});
2020-07-14 09:16:50 +00:00
Self.downloadImages = async() => {
2020-07-13 09:58:31 +00:00
const models = Self.app.models;
const container = await models.TempContainer.container('salix-image');
const tempPath = path.join(container.client.root, container.name);
const maxAttempts = 3;
2020-07-13 09:58:31 +00:00
2021-01-28 14:57:33 +00:00
const images = await Self.find({
2021-01-28 15:08:57 +00:00
where: {attempts: {eq: maxAttempts}}
2021-01-28 14:57:33 +00:00
});
for (let image of images) {
const currentStamp = new Date().getTime();
const updatedStamp = image.updated.getTime();
const graceTime = Math.abs(currentStamp - updatedStamp);
const maxTTL = 3600 * 48 * 1000; // 48 hours in ms;
if (graceTime >= maxTTL)
await Self.destroyById(image.itemFk);
}
download();
2020-07-13 09:58:31 +00:00
async function download() {
const image = await Self.findOne({
where: {url: {neq: null}, attempts: {lt: maxAttempts}},
2021-02-18 12:03:49 +00:00
order: 'priority, attempts, updated'
});
2020-07-13 09:58:31 +00:00
if (!image) return;
2021-02-24 13:37:49 +00:00
const srcFile = image.url;
2021-02-24 14:55:09 +00:00
const fileName = srcFile.replace(/\.|\/|:|\?|\\|=/g, '');
const file = `${fileName}.png`;
const filePath = path.join(tempPath, file);
2020-07-13 09:58:31 +00:00
https.get(image.url, async response => {
if (response.statusCode != 200) {
const error = new Error(`Could not download the image. Status code ${response.statusCode}`);
2020-08-18 06:37:39 +00:00
return await errorHandler(image.itemFk, error, filePath);
}
2020-07-13 09:58:31 +00:00
2020-08-18 07:45:26 +00:00
const writeStream = fs.createWriteStream(filePath);
writeStream.on('open', () => {
response.pipe(writeStream);
2020-08-18 07:45:26 +00:00
});
2020-07-14 07:32:50 +00:00
2020-08-18 07:45:26 +00:00
writeStream.on('error', async error => {
await errorHandler(image.itemFk, error, filePath);
2020-07-14 07:32:50 +00:00
});
2020-08-18 07:45:26 +00:00
writeStream.on('finish', async function() {
writeStream.end();
});
writeStream.on('close', async function() {
2020-08-18 07:45:26 +00:00
try {
2020-12-18 16:23:04 +00:00
await models.Image.registerImage('catalog', filePath, fileName, image.itemFk);
2020-08-18 07:45:26 +00:00
await image.destroy();
download();
2020-08-18 07:45:26 +00:00
} catch (error) {
await errorHandler(image.itemFk, error, filePath);
}
});
}).on('error', async error => {
await errorHandler(image.itemFk, error, filePath);
});
}
async function errorHandler(rowId, error, filePath) {
2020-08-26 05:33:00 +00:00
try {
const row = await Self.findById(rowId);
2021-01-28 14:57:33 +00:00
if (!row) return;
2020-08-26 05:33:00 +00:00
if (row.attempts < maxAttempts) {
await row.updateAttributes({
error: error,
attempts: row.attempts + 1,
updated: new Date()
});
}
2020-08-26 05:33:00 +00:00
2020-08-27 11:53:11 +00:00
if (filePath && fs.existsSync(filePath))
2020-08-26 05:33:00 +00:00
await fs.unlink(filePath);
download();
2020-08-27 12:11:22 +00:00
} catch (err) {
2021-01-28 14:57:33 +00:00
throw new Error(`Image download failed: ${err}`);
2020-08-26 05:33:00 +00:00
}
2020-07-13 09:58:31 +00:00
}
};
};