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');
|
const path = require('path');
|
||||||
|
|
||||||
module.exports = Self => {
|
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 models = Self.app.models;
|
||||||
const tx = await Self.beginTransaction({});
|
const tx = await Self.beginTransaction({});
|
||||||
const myOptions = {transaction: tx};
|
const myOptions = {transaction: tx};
|
||||||
|
@ -25,11 +29,10 @@ module.exports = Self => {
|
||||||
fields: ['width', 'height', 'crop']
|
fields: ['width', 'height', 'crop']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}, myOptions);
|
||||||
|
|
||||||
const fileName = file.split('.')[0];
|
const fileName = file.split('.')[0];
|
||||||
// const rootPath = 'C:\\Users\\jsanc\\Desktop\\images';
|
const rootPath = Self.getPath();
|
||||||
const rootPath = '/var/lib/salix/image';
|
|
||||||
const data = {
|
const data = {
|
||||||
name: fileName,
|
name: fileName,
|
||||||
collectionFk: collectionName
|
collectionFk: collectionName
|
||||||
|
|
|
@ -19,14 +19,9 @@ module.exports = Self => {
|
||||||
Self.download = async() => {
|
Self.download = async() => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
|
|
||||||
let filePath;
|
|
||||||
try {
|
try {
|
||||||
const imageQueue = await Self.find({
|
const imageQueue = await Self.find({limit: 25});
|
||||||
limit: 3
|
const rootPath = models.Image.getPath();
|
||||||
});
|
|
||||||
|
|
||||||
// const rootPath = 'C:\\Users\\jsanc\\Desktop\\images';
|
|
||||||
const rootPath = '/var/lib/salix/image';
|
|
||||||
const tempPath = path.join(rootPath, 'temp');
|
const tempPath = path.join(rootPath, 'temp');
|
||||||
|
|
||||||
// Create temporary path
|
// Create temporary path
|
||||||
|
@ -34,21 +29,27 @@ module.exports = Self => {
|
||||||
|
|
||||||
for (let image of imageQueue) {
|
for (let image of imageQueue) {
|
||||||
const fileName = `${image.itemFk}.png`;
|
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 => {
|
https.get(image.url, async response => {
|
||||||
// Upload file to temporary path
|
|
||||||
const file = fs.createWriteStream(filePath);
|
|
||||||
response.pipe(file);
|
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();
|
await image.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
file.on('error', err => {
|
||||||
|
fs.unlink(filePath);
|
||||||
|
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return imageQueue;
|
return imageQueue;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
fs.unlink(filePath);
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue