cloudinary / cloudinary_gem

Cloudinary GEM for Ruby on Rails integration
https://cloudinary.com
420 stars 285 forks source link

Cloudinary direct upload bypassing rails app #117

Closed jlerpscher closed 10 years ago

jlerpscher commented 10 years ago

Hi,

I am trying to upload directly to Cloudinary, bypassing my rails app. I am working with Carrierwave and a Picture Uploader .

class PictureUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave

  eager
  make_private

  version :tiny do
    resize_to_fill(100, 100)
  end

  version :full do
    cloudinary_transformation transformation: [
      { width: 1500, height: 1200, crop: :limit, quality: 80, flags: 'progressive' }
    ]
  end
= cl_form_tag my_callback_url, form: { class: 'fileupload' }, type: :private do
  = file_field_tag :file
  = submit_tag 'Upload'

Until now, everything is fine. But in my controller, I am unable to save the picture path unless doing the following:

@car = current_user.cars.build
path = "#{params[:resource_type]}/#{params[:type]}/v#{params[:version]}/#{params[:public_id]}"
path += ".#{params[:format]}" if params[:format].present?
path += "##{params[:signature]}"
@car.picture = path
# here path eq "image/private/v14030428xxx/lez0juhg5d5owvnsxxx.jpg#xxxxxx729ef01301xxx2e3171d316e0805c2xxx"
@car.save!

I guess it's not the cleanest way to do that.

I have tried to build directly with params but it did not save. I also tried with cloudinary_url but it also failed. The result looks like:

"http://res.cloudinary.com/hd0w8otcz/image/private/v140304xxxx/lez0juhg5d5owxxxxxxx.jpg"

The params are:

{"bytes"=>"73526",
 "created_at"=>"2014-06-17T22:07:23Z",
 "etag"=>"43e4702e787fc96b2920a939efcaxxxx",
 "format"=>"jpg",
 "height"=>"606",
 "public_id"=>"lez0juhg5d5owvnszljo",
 "resource_type"=>"image",
 "signature"=>"xxxxxx729ef01301xxx2e3171d316e0805c2xxx",
 "type"=>"private",
 "version"=>"1403042843",
 "width"=>"808"}

So, what is the best way to upload to cloudinary bypassing my rails app?

taragano commented 10 years ago

When using Direct upload with CarrierWave, the library automatically takes the image's details and stores it as the identifier. You can have a look at the Sample project which demonstrates both server-side and client-side uploads, including CarrierWave integration. https://github.com/cloudinary/cloudinary_gem/tree/master/samples/photo_album

jlerpscher commented 10 years ago

Thank you for your answer.

The solution provided in the sample app require javascript. I am looking for a solution without any javascript.

taragano commented 10 years ago

If you want to convert an upload response to something CarrierWave will be willing to accept, you can use:

Cloudinary::Utils.signed_preloaded_image(params)
jlerpscher commented 10 years ago

Hi,

Sorry, back from holidays.

Cloudinary::Utils.signed_preloaded_image(params)

doesn't seems handle private upload, I need something like:

"image/private/v1404760155/aaar1lttbgujqhdaaa.jpg#aaaaaab0b57be13297c6bb0a790e309"

but I get something like that:

"image/upload/v1404760287/cwuqrownwmqxl3pay4de.jpg#8cf120c91bb36a8e8a136170b6a8e28431bcdc18"
"#{result["resource_type"]}/upload/v#{result["version"]}/#{[result["public_id"], result["format"]].reject(&:blank?).join(".")}##{result["signature"]}"

params type it is not used in the function. Am I missing something?

jlerpscher commented 10 years ago

works great! Thank you.