Fixed async image registration
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
80075a6471
commit
3074c9ce35
|
@ -3,7 +3,11 @@ const sharp = require('sharp');
|
|||
const path = require('path');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.register = async(collectionName, file, srcFilePath) => {
|
||||
Self.getPath = function() {
|
||||
return '/var/lib/salix/image';
|
||||
};
|
||||
|
||||
Self.registerImage = async(collectionName, file, srcFilePath) => {
|
||||
const models = Self.app.models;
|
||||
const tx = await Self.beginTransaction({});
|
||||
const myOptions = {transaction: tx};
|
||||
|
@ -25,11 +29,10 @@ module.exports = Self => {
|
|||
fields: ['width', 'height', 'crop']
|
||||
}
|
||||
}
|
||||
});
|
||||
}, myOptions);
|
||||
|
||||
const fileName = file.split('.')[0];
|
||||
// const rootPath = 'C:\\Users\\jsanc\\Desktop\\images';
|
||||
const rootPath = '/var/lib/salix/image';
|
||||
const rootPath = Self.getPath();
|
||||
const data = {
|
||||
name: fileName,
|
||||
collectionFk: collectionName
|
||||
|
|
|
@ -19,14 +19,9 @@ module.exports = Self => {
|
|||
Self.download = async() => {
|
||||
const models = Self.app.models;
|
||||
|
||||
let filePath;
|
||||
try {
|
||||
const imageQueue = await Self.find({
|
||||
limit: 3
|
||||
});
|
||||
|
||||
// const rootPath = 'C:\\Users\\jsanc\\Desktop\\images';
|
||||
const rootPath = '/var/lib/salix/image';
|
||||
const imageQueue = await Self.find({limit: 25});
|
||||
const rootPath = models.Image.getPath();
|
||||
const tempPath = path.join(rootPath, 'temp');
|
||||
|
||||
// Create temporary path
|
||||
|
@ -34,21 +29,27 @@ module.exports = Self => {
|
|||
|
||||
for (let image of imageQueue) {
|
||||
const fileName = `${image.itemFk}.png`;
|
||||
filePath = path.join(tempPath, fileName);
|
||||
const filePath = path.join(tempPath, fileName);
|
||||
const file = fs.createWriteStream(filePath);
|
||||
|
||||
https.get(image.url, async response => {
|
||||
// Upload file to temporary path
|
||||
const file = fs.createWriteStream(filePath);
|
||||
response.pipe(file);
|
||||
});
|
||||
|
||||
await models.Image.register('catalog', fileName, filePath);
|
||||
file.on('finish', async function() {
|
||||
await models.Image.registerImage('catalog', fileName, filePath);
|
||||
await image.destroy();
|
||||
});
|
||||
|
||||
file.on('error', err => {
|
||||
fs.unlink(filePath);
|
||||
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
return imageQueue;
|
||||
} catch (e) {
|
||||
fs.unlink(filePath);
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue