Changes to dowload item photos endpoint
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-08-13 12:16:25 +02:00
parent da3d20bb16
commit c9ed17d981
2 changed files with 36 additions and 14 deletions

View File

@ -20,9 +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}});
const rootPath = models.Image.getPath(); const tempPath = path.join('/temp/salix-image');
const tempPath = path.join(rootPath, 'temp');
// Create temporary path // Create temporary path
await fs.mkdir(tempPath, {recursive: true}); await fs.mkdir(tempPath, {recursive: true});
@ -33,24 +32,43 @@ 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('finish', async function() { file.on('error', async error => {
await models.Image.registerImage('catalog', fileName, filePath); await errorHandler(image.itemFk, error, filePath);
await image.destroy(); });
});
file.on('error', err => { file.on('finish', async function() {
fs.unlink(filePath); try {
await models.Image.registerImage('catalog', fileName, filePath);
throw err; await image.destroy();
} catch (error) {
await errorHandler(image.itemFk, error, filePath);
}
});
}).on('error', async error => {
await errorHandler(image.itemFk, error, filePath);
}); });
} }
return imageQueue; return imageQueue;
} catch (e) { } catch (error) {
throw e; 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);
} }
}; };
}; };

View File

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