So, I'm trying to figure out how to download a remote image, and then store the downloaded image using CollectionFS.
I was trying to use the automatic URL handling in CFS, but the host I'm downloading images -from- has HEAD requests disabled, so I can't use it.
I was either going to use Meteor.get, or NPM's 'request', but I don't really understand how to combine the two to get the desired result.
Any thoughts would be be greatly appreciated. All I know how to do is use the URL in a Meteor.get request, but after that, I'm really lost.
This is sort of what I get so far, but I don't know what to do with the result of the request afterwards:
var result = HTTP.get(url);
I only assume that I'm supposed to do something with result.body (as per the Meteor documentation), but I don't know how to properly encode that object so that it can be shoved into a CFS collection locally.
At some point Meteor added
npmRequestOptions
as a way to override their reasonable defaults for cases like this.You can now simply do
HTTP.get(url, {npmRequestOptions: {encoding: 'binary'}})
to get correct image data
From what I've read on the CollectionFS API, server-side inserts can take a Node.js
Buffer
object as parameter.https://github.com/CollectionFS/Meteor-CollectionFS#getting-started
A
Buffer
object is what you'll get from npmrequest
package with anencoding
set tonull
, and this is what you're expected to insert into CollectionFS.If we don't set
encoding
tonull
, the response will pass through string encoding that will break our image data since this is raw binary.Unfortunately you can't use Meteor HTTP package to do this because it acts as a wrapper around npm
request
and specifically force the encoding to utf-8 as seen in line 74 :https://github.com/meteor/meteor/blob/devel/packages/http/httpcall_server.js#L74
As you probably know, npm packages are not directly usable in Meteor because server-side environment relies on
Fiber
s.So here is the necessary wrapping around request, as a yet unreleased package :
/packages/request/package.js :
/packages/request/server/lib/request.js
:Then you can use request like this :
The
buffer
variable will hold the unaltered image data you need to pass to CollectionFS insert.