使用流星(CFS)下载远程图像 [英] Downloading remote images using Meteor (CFS)

查看:141
本文介绍了使用流星(CFS)下载远程图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我试图找出如何下载远程图像,然后使用CollectionFS存储下载的图像。

So, I'm trying to figure out how to download a remote image, and then store the downloaded image using CollectionFS.

我正在尝试使用自动在CFS中的URL处理,但是我正在下载图像-from-主机请求被禁用,所以我不能使用它。

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.

我要使用Meteor.get或者NPM的请求,但我不太明白如何组合这两个来获得所需的结果。

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.

任何想法都将不胜感激。所有我知道如何使用Meteor.get请求中的URL,但之后,我真的迷失了。

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);

我只假设我应该用result.body做一些事情(根据Meteor文档),但是我不知道如何正确地对该对象进行编码,以便可以在本地将其推入CFS集合。

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.

推荐答案

从我在CollectionFS API上读到的内容,服务器端插入可以使用Node.js 缓冲区对象作为参数。

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-开始

A 缓冲区对象是从npm获得的对象请求包含编码,设置为 null ,这是您期望的要插入到CollectionFS中。

A Buffer object is what you'll get from npm request package with an encoding set to null, and this is what you're expected to insert into CollectionFS.

如果我们没有将 encoding 设置为 code>,响应将通过字符串编码,这将破坏我们的图像数据是原始的二进制。

If we don't set encoding to null, the response will pass through string encoding that will break our image data since this is raw binary.

不幸的是,您不能使用Meteor HTTP包来执行此操作,因为它作为npm 请求并特别强制编码为utf-8,如第74行所示:

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

您可能知道,npm软件包在Meteor中不能直接使用,因为服务器端环境依赖于 Fiber s

As you probably know, npm packages are not directly usable in Meteor because server-side environment relies on Fibers.

所以这里是必要的包装要求,作为一个尚未发布的包:

So here is the necessary wrapping around request, as a yet unreleased package :

/packages/request/package.js:

Package.describe({
  summary:"Simplified HTTP request client",
  version:"2.40.0"
});

Npm.depends({
  "fibers":"1.0.1",
  "request":"2.40.0"
});

Package.onUse(function(api){
  //
  api.versionsFrom("METEOR@0.9.0.1");
  //
  api.use("underscore","server");
  //
  api.addFiles("server/lib/request.js","server");
  //
  api.export("request","server");
});

/packages/request/server/lib/request.js

var Future=Npm.require("fibers/future");
request=Npm.require("request");

var requestSync=function(uri,options){
  var future=new Future();
  request(uri,options,function(error,response,body){
    if(error){
      console.log(error);
      throw error;
    }
    future.return({
      response:response,
      body:body
    });
  });
  return future.wait();
};

_.extend(request,{
  putSync:function(uri,options){
    options.method="PUT";
    return requestSync(uri,options);
  },
  patchSync:function(uri,options){
    options.method="PATCH";
    return requestSync(uri,options);
  },
  postSync:function(uri,options){
    options.method="POST";
    return requestSync(uri,options);
  },
  headSync:function(uri,options){
    options.method="HEAD";
    return requestSync(uri,options);
  },
  delSync:function(uri,options){
    options.method="DELETE";
    return requestSync(uri,options);
  },
  getSync:requestSync
});

然后你可以使用这样的请求:

Then you can use request like this :

var result=request.getSync(url,{
  encoding:null
});
var buffer=result.body;

缓冲区变量将保存未更改的图像您需要传递给CollectionFS插入的数据。

The buffer variable will hold the unaltered image data you need to pass to CollectionFS insert.

这篇关于使用流星(CFS)下载远程图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆