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

View File

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