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 14:26:35 +00:00
|
|
|
const imageQueue = await Self.find({where: {error: null}, limit: 25});
|
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
|
|
|
|
|
|
|
for (let image of imageQueue) {
|
2020-07-13 12:13:21 +00:00
|
|
|
const fileName = `${image.itemFk}.png`;
|
2020-07-14 07:32:50 +00:00
|
|
|
const filePath = path.join(tempPath, fileName);
|
|
|
|
const file = fs.createWriteStream(filePath);
|
2020-07-13 09:58:31 +00:00
|
|
|
|
|
|
|
https.get(image.url, async response => {
|
2020-08-13 10:16:25 +00:00
|
|
|
if (response.statusCode != 200) {
|
|
|
|
const error = new Error(`Could not download the image. Status code ${response.statusCode}`);
|
2020-07-13 09:58:31 +00:00
|
|
|
|
2020-08-13 10:16:25 +00:00
|
|
|
file.close();
|
|
|
|
await errorHandler(image.itemFk, error, filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
response.pipe(file);
|
2020-07-14 07:32:50 +00:00
|
|
|
|
2020-08-13 10:16:25 +00:00
|
|
|
file.on('error', async error => {
|
|
|
|
await errorHandler(image.itemFk, error, filePath);
|
|
|
|
});
|
2020-07-14 07:32:50 +00:00
|
|
|
|
2020-08-13 10:16:25 +00:00
|
|
|
file.on('finish', async function() {
|
|
|
|
try {
|
|
|
|
await models.Image.registerImage('catalog', fileName, filePath);
|
|
|
|
await image.destroy();
|
|
|
|
} catch (error) {
|
|
|
|
await errorHandler(image.itemFk, error, filePath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).on('error', async error => {
|
|
|
|
await errorHandler(image.itemFk, error, filePath);
|
2020-07-14 07:32:50 +00:00
|
|
|
});
|
2020-07-13 09:58:31 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|