Last active
September 14, 2018 17:50
-
-
Save whisher/c542adabfe562d55435e5b56e23a208f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createGallery = (imageName) => { | |
const prefix = 'gallery_'; | |
const imagePath = './images/'; | |
const readableStream = fs.createReadStream(imagePath + imageName); | |
const writableStream = fs.createWriteStream(imagePath + prefix + imageName); | |
const transformer = sharp() | |
.resize(1110, 600) | |
.crop(sharp.strategy.entropy) | |
.on('error', function(err) { | |
console.log('sharp ', err); | |
}); | |
pump(readableStream, transformer, writableStream, function(err) { | |
if (err) { | |
console.log('pipe sharp finished', err); | |
} | |
}); | |
return prefix + imageName; | |
}; | |
exports.gallery = (req, res, next) => { | |
const url = getUrl(req); | |
const imageName = createGallery(req.file.filename); | |
const imagePath = url + '/images/' + imageName; | |
const src = imagePath; | |
const name = req.file.originalname; | |
const data = {src: src, name: name}; | |
// Hack | |
setInterval(function() { | |
res.status(200).json(data); | |
}, 1000); | |
}; |
const createGallery = (req, res) => {
const prefix = 'gallery_';
const imagePath = './images/';
const readableStream = fs.createReadStream(imagePath + req.file.filename);
const writableStream = fs.createWriteStream(imagePath + prefix + req.file.filename);
const transformer = sharp()
.resize(1110, 600)
.crop(sharp.strategy.entropy)
.on('error', function(err) {
console.log('sharp ', err);
});
pump(readableStream, transformer, writableStream, function(err) {
if (err) {
console.log('pipe sharp finished', err);
res.status(500);
}
const imageName = prefix + req.file.filename;
const url = getUrl(req);
const imagePath = url + '/images/' + imageName;
const src = imagePath;
const name = req.file.originalname;
const data = {src: src, name: name};
res.status(200).json(data);
});
};
exports.gallery = (req, res, next) => {
createGallery(req, res);
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://medium.freecodecamp.org/node-js-streams-everything-you-need-to-know-c9141306be93 This would point in the direction that I just said. :)
Just try with readableStream.pipe(transformer).pipe(writableStream).pipe(res.status(200).json(data)); as well