Merge branch 'master' of https://gitea.verdnatura.es/verdnatura/salix into test
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
commit
dd5e71c9fc
|
@ -20,7 +20,8 @@ module.exports = Self => {
|
|||
const models = Self.app.models;
|
||||
|
||||
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 tempPath = path.join(rootPath, 'temp');
|
||||
|
||||
|
@ -33,24 +34,41 @@ module.exports = Self => {
|
|||
const file = fs.createWriteStream(filePath);
|
||||
|
||||
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);
|
||||
|
||||
file.on('error', async error => {
|
||||
await errorHandler(image.itemFk, error, filePath);
|
||||
});
|
||||
|
||||
file.on('finish', async function() {
|
||||
try {
|
||||
await models.Image.registerImage('catalog', fileName, filePath);
|
||||
await image.destroy();
|
||||
} catch (error) {
|
||||
await errorHandler(image.itemFk, error, filePath);
|
||||
}
|
||||
});
|
||||
|
||||
file.on('error', err => {
|
||||
fs.unlink(filePath);
|
||||
|
||||
throw err;
|
||||
}).on('error', async error => {
|
||||
await errorHandler(image.itemFk, error, filePath);
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
await errorHandler(image.itemFk, error);
|
||||
}
|
||||
|
||||
return imageQueue;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
async function errorHandler(rowId, error, filePath) {
|
||||
const row = await Self.findById(rowId);
|
||||
await row.updateAttribute('error', error);
|
||||
|
||||
if (filePath)
|
||||
await fs.unlink(filePath);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -16,6 +16,10 @@
|
|||
"url": {
|
||||
"type": "String",
|
||||
"required": true
|
||||
},
|
||||
"error": {
|
||||
"type": "String",
|
||||
"required": true
|
||||
}
|
||||
},
|
||||
"relations": {
|
||||
|
|
Loading…
Reference in New Issue