Skip to content

Instantly share code, notes, and snippets.

@whisher
Last active September 14, 2018 17:50
Show Gist options
  • Save whisher/c542adabfe562d55435e5b56e23a208f to your computer and use it in GitHub Desktop.
Save whisher/c542adabfe562d55435e5b56e23a208f to your computer and use it in GitHub Desktop.
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);
};
@Deviad
Copy link

Deviad commented Sep 14, 2018



exports.gallery = (req, res, next) => {
  const url = getUrl(req);
  const imageName = prefix + imageName;
  const prefix = 'gallery_';
  const imagePath = './images/';
  const imagePath = url + '/images/' + imageName;
  const src = imagePath;
  const name = req.file.originalname;
  const data = {src: src, name: name};
  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);
    });
  readableStream.pipe(transformer).pipe(writableStream).pipe(()=>res.status(200).json(data));
};

Something like this.

@Deviad
Copy link

Deviad commented Sep 14, 2018

I think what pipe does is something like map in here: https://repl.it/@Deviad/Maybe-Data-structure
Wondering if you need also something like join...

@Deviad
Copy link

Deviad commented Sep 14, 2018

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

@whisher
Copy link
Author

whisher commented Sep 14, 2018

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