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

86 lines
2.8 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;
try {
2020-08-18 07:48:02 +00:00
const tempPath = path.join('/tmp/salix-image');
2020-07-13 09:58:31 +00:00
// Create temporary path
2020-07-13 12:31:50 +00:00
await fs.mkdir(tempPath, {recursive: true});
2020-07-13 09:58:31 +00:00
2020-08-18 06:37:39 +00:00
const timer = setInterval(async() => {
2020-08-27 11:53:11 +00:00
const image = await Self.findOne({where: {error: null}});
2020-07-13 09:58:31 +00:00
2020-08-18 06:37:39 +00:00
// Exit loop
2020-08-18 07:45:26 +00:00
if (!image) return clearInterval(timer);
2020-08-18 06:37:39 +00:00
2020-08-18 07:45:26 +00:00
const fileName = `${image.itemFk}.png`;
const filePath = path.join(tempPath, fileName);
2020-07-13 09:58:31 +00:00
2020-08-18 07:45:26 +00:00
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}`);
2020-08-18 07:45:26 +00:00
return await errorHandler(image.itemFk, error, filePath);
}
2020-07-14 07:32:50 +00:00
2020-08-18 07:45:26 +00:00
response.pipe(writeStream);
}).on('error', async error => {
await errorHandler(image.itemFk, error, filePath);
});
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() {
try {
await models.Image.registerImage('catalog', fileName, filePath);
await image.destroy();
} catch (error) {
await errorHandler(image.itemFk, error, filePath);
}
});
2020-08-27 11:53:11 +00:00
}, 2500);
} catch (error) {
2020-08-27 11:53:11 +00:00
throw new Error('Try-catch error: ', error);
}
async function errorHandler(rowId, error, filePath) {
2020-08-26 05:33:00 +00:00
try {
const row = await Self.findById(rowId);
2020-08-26 05:33:00 +00:00
if (!row)
throw new Error(`Could not update due error ${error}`);
await row.updateAttribute('error', error);
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);
} catch (error) {
2020-08-27 11:53:11 +00:00
throw new Error('ErrorHandler error: ', error);
2020-08-26 05:33:00 +00:00
}
2020-07-13 09:58:31 +00:00
}
};
};