Merge branch 'master' of https://gitea.verdnatura.es/verdnatura/salix into test
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-08-17 09:42:29 +02:00
commit dd5e71c9fc
2 changed files with 35 additions and 13 deletions

View File

@ -20,7 +20,8 @@ module.exports = Self => {
const models = Self.app.models; const models = Self.app.models;
try { try {
const imageQueue = await Self.find({limit: 25}); const imageQueue = await Self.find({where: {error: null}, limit: 25});
/* const tempPath = path.join('/tmp/salix-image'); */
const rootPath = models.Image.getPath(); const rootPath = models.Image.getPath();
const tempPath = path.join(rootPath, 'temp'); const tempPath = path.join(rootPath, 'temp');
@ -33,24 +34,41 @@ module.exports = Self => {
const file = fs.createWriteStream(filePath); const file = fs.createWriteStream(filePath);
https.get(image.url, async response => { https.get(image.url, async response => {
if (response.statusCode != 200) {
const error = new Error(`Could not download the image. Status code ${response.statusCode}`);
file.close();
await errorHandler(image.itemFk, error, filePath);
}
response.pipe(file); response.pipe(file);
file.on('error', async error => {
await errorHandler(image.itemFk, error, filePath);
}); });
file.on('finish', async function() { file.on('finish', async function() {
try {
await models.Image.registerImage('catalog', fileName, filePath); await models.Image.registerImage('catalog', fileName, filePath);
await image.destroy(); await image.destroy();
} catch (error) {
await errorHandler(image.itemFk, error, filePath);
}
}); });
}).on('error', async error => {
file.on('error', err => { await errorHandler(image.itemFk, error, filePath);
fs.unlink(filePath);
throw err;
}); });
} }
} catch (error) {
await errorHandler(image.itemFk, error);
}
return imageQueue; async function errorHandler(rowId, error, filePath) {
} catch (e) { const row = await Self.findById(rowId);
throw e; await row.updateAttribute('error', error);
if (filePath)
await fs.unlink(filePath);
} }
}; };
}; };

View File

@ -16,6 +16,10 @@
"url": { "url": {
"type": "String", "type": "String",
"required": true "required": true
},
"error": {
"type": "String",
"required": true
} }
}, },
"relations": { "relations": {