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

90 lines
3.0 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-09-08 13:07:57 +00:00
const image = await Self.findOne({
where: {error: null, url: {neq: 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-12-18 16:23:04 +00:00
const srcFile = image.url.split('/').pop();
const fileName = srcFile.split('.')[0];
const file = `${fileName}.png`;
const filePath = path.join(tempPath, file);
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 {
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();
} catch (error) {
await errorHandler(image.itemFk, error, filePath);
}
});
2020-08-27 12:09:21 +00:00
}, 1000);
} 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);
2020-08-27 12:11:22 +00:00
} catch (err) {
2020-08-27 12:23:40 +00:00
throw new Error(`ErrorHandler error: ${err}`);
2020-08-26 05:33:00 +00:00
}
2020-07-13 09:58:31 +00:00
}
};
};