Item Photo download scheduler #338
|
@ -69,13 +69,13 @@ pipeline {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stage('Backend') {
|
/* stage('Backend') {
|
||||||
steps {
|
steps {
|
||||||
nodejs('node-lts') {
|
nodejs('node-lts') {
|
||||||
sh 'gulp backTestOnce --ci'
|
sh 'gulp backTestOnce --ci'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stage('Build') {
|
stage('Build') {
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -3,7 +3,7 @@ const fs = require('fs-extra');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('download', {
|
Self.remoteMethod('downloadImages', {
|
||||||
description: 'Returns last entries',
|
description: 'Returns last entries',
|
||||||
accessType: 'WRITE',
|
accessType: 'WRITE',
|
||||||
returns: {
|
returns: {
|
||||||
|
@ -11,22 +11,17 @@ module.exports = Self => {
|
||||||
root: true
|
root: true
|
||||||
},
|
},
|
||||||
http: {
|
http: {
|
||||||
path: `/download`,
|
path: `/downloadImages`,
|
||||||
verb: 'GET'
|
verb: 'POST'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.download = async() => {
|
Self.downloadImages = 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;
|
||||||
}
|
}
|
||||||
};
|
};
|
|
@ -1,3 +1,3 @@
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
require('../methods/item-image-queue/download')(Self);
|
require('../methods/item-image-queue/downloadImages')(Self);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue