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-13 11:38:52 +00:00
|
|
|
/* const tempPath = path.join('/tmp/salix-image'); */
|
|
|
|
const rootPath = models.Image.getPath();
|
|
|
|
const tempPath = path.join(rootPath, 'temp');
|
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-18 06:10:40 +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-13 10:16:25 +00:00
|
|
|
|
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 => {
|
2020-08-13 10:16:25 +00:00
|
|
|
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 => {
|
2020-08-13 10:16:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1000);
|
2020-08-13 10:16:25 +00:00
|
|
|
} catch (error) {
|
|
|
|
await errorHandler(image.itemFk, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function errorHandler(rowId, error, filePath) {
|
|
|
|
const row = await Self.findById(rowId);
|
|
|
|
await row.updateAttribute('error', error);
|
|
|
|
|
|
|
|
if (filePath)
|
|
|
|
await fs.unlink(filePath);
|
2020-07-13 09:58:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|