Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a buffer for a file (image) from CollectionFS

I'm trying to insert an image into a pdf I'm creating server-side with PDFkit. I'm using cfs:dropbox to store my files. Before when I was using cvs:filesystem, it was easy to add the images to my pdf's cause they were right there. Now that they're stored remotely, I'm not sure how to add them, since PDFkit does not support adding images with just the url. It will, however, accept a buffer. How can I get a buffer from my CollectionFS files?

So far I have something like this:

var portrait = Portraits.findOne('vS2yFy4gxXdjTtz5d');
readStream = portrait.createReadStream('portraits');

I tried getting the buffer two ways so far:

First using dataMan, but the last command never comes back:

var dataMan = new DataMan.ReadStream(readStream, portrait.type());
var buffer = Meteor.wrapAsync(Function.prototype.bind(dataMan.getBuffer, dataMan))();

Second buffering the stream manually:

var buffer = new Buffer(0);
readStream.on('readable', function() {
    buffer = Buffer.concat([buffer, readStream.read()]);
});
readStream.on('end', function() {
    console.log(buffer.toString('base64'));
});

That never seems to come back either. I double-checked my doc to make sure it was there and it has a valid url and the image appears when I put the url in my browser. Am I missing something?

like image 737
Jared Martin Avatar asked Jun 23 '15 00:06

Jared Martin


People also ask

What is the use of PiL image from buffer?

PIL.Image.frombuffer () Creates an image memory referencing pixel data in a byte buffer. Note that this function decodes pixel data only, not entire images. If you have an entire image file in a string, wrap it in a BytesIO object, and use open () to load it.

How to convert arraybuffer to a buffer object?

The process is: call arrayBuffer () on the fetch response object. convert the ArrayBuffer object to a Buffer object with Buffer.from () save the file via fs.createWriteStream () and write () the Buffer object

How do I load an image file from a string?

If you have an entire image file in a string, wrap it in a BytesIO object, and use open () to load it. Syntax: PIL.Image.frombuffer (mode, size, data, decoder_name=’raw’, *args)


1 Answers

I had to do something similar and since there's no answer to this question, here is how I do it:

// take a cfs file and return a base64 string
var getBase64Data = function(file, callback) {
  // callback has the form function (err, res) {}
  var readStream = file.createReadStream();
  var buffer = [];
  readStream.on('data', function(chunk) {
    buffer.push(chunk);
  });
  readStream.on('error', function(err) {
    callback(err, null);
  });
  readStream.on('end', function() {
    callback(null, buffer.concat()[0].toString('base64'));
  });
};

// wrap it to make it sync    
var getBase64DataSync = Meteor.wrapAsync(getBase64Data);

// get a cfs file
var file = Files.findOne();

// get the base64 string
var base64str = getBase64DataSync(file);

// get the buffer from the string
var buffer = new Buffer(base64str, 'base64')

Hope it'll help!

like image 90
acemtp Avatar answered Sep 23 '22 06:09

acemtp