开发者

Convert file upload contents to a binary file without saving (Rails)

I have a rails 3 app where I am using the 'face' gem to reference the Face.com API. The api method takes a parameter of the form:

:file => File.new(path_to_file, 'rb')

which works.

I am trying to change the flow of the app so that the file can be uploaded via a form, do some work with RMagick and then make the API call, all without saving the file to disk.

I can generate the RMagick 'Image' with

image 开发者_如何学JAVA= Magick::Image.from_blob(upload_image_field.read)

I can then manipulate the file with RMagick and even save the results into the database with:

self.data = image.to_blob #normally 'upload_image_field.read' if not using RMagick

My problem is that I can't change the image file (or the blob) into something that the API will recognize (without saving it to disk and then referencing the file on disk).

For example using this in the API method fails:

:file => image.to_blob 

How do I convert he blob into the same format as

File.new(path_to_file, 'rb')

Thanks


OK, I could be wrong on this one... but I wanted to dig this up. Unfortunately, you just have to live with saving it as a file. The reason is because the API makes an HTTP POST. Unfortunately, this needs to be a file.

References from: [https://github.com/rociiu/face/tree/master/lib/face]:

recognition.rb:

def faces_detect(opts={})
    opts.assert_valid_keys(:urls, :file, :detector, :attributes, :callback, :callback_url)
    make_request(:faces_detect, opts)
end

utils.rb:

def make_request(api_method, opts={})
   ....
    response = JSON.parse( RestClient.post(API_METHODS[ api_method ], opts.merge(api_crendential)).body )
   ....
  end

So, why is it a problem to save to a file then?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜