ruby-on-railsfacebookfacebook-graph-apiopen-uriomniauth-facebook

Want to save facebook image into my rails app


I am using ominauth to signup users in my rails app.i also have a basic signup option with email address also. So for both users i have to save upload user picture in my file system I have regular file upload for my email users but for FB users i want their pics to save in my disc so i can use same code and not use FB graph link while displaying.

FB sends images in this format with graph API

http://graph.facebook.com/100007619644580/picture?type=large

How can i save that in my public folder where i store all the user images.

i tried

directory = "public/data/orig/"
      #name = num1+'_'+params[:upload]['datafile'].original_filename
      name = "new_name_for image"
      path = File.join(directory, name)
      #File.open(path, "wb") { |f| f.write(params[:upload_hover]['datafile'].read) }

      File.open(path, 'wb') do |file|
        file << open('http://graph.facebook.com/100007619644580/picture?type=large').read
      end

but it gives me error

No such file or directory @ rb_sysopen - http://graph.facebook.com/100007619644580/picture?type=large

Let me know if you have any other solutions.


Solution

  • Thanks for the answer, Yes i could use the paperclip gem but i'm already using cloudinary to upload image so i didn't wanted to use another image based gem just to force save files from fb user profile. I was able to workout the following code changes.

    In omniauth.rb

    :secure_image_url => true
    

    for last parameters with :scope which allowed me and secure URL.

    and in my auth controller

     img_path = env["omniauth.auth"]["info"]["image"].gsub(/$/,'?type=large')
        require "open-uri"
    
      num1 = 'logo_'+SecureRandom.hex(5)
      img_name = num1+'_fbimage.jpg'
      directory = "public/data/orig/org"
      path = File.join(directory, img_name)
      File.open(path, 'wb') do |file|
        file << open(img_path).read
      end  
    

    require "open-uri" and :secure_image_url => true did the trick for me.