问题
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.
回答1:
Use your model to save your facebook image via paperclip see below :
member model
def self.from_omniauth(auth, signed_in_resource=nil)
member = Member.where(:provider => auth.provider, :uid => auth.uid).first
if member
member.member_pic = "https://graph.facebook.com/#{auth["uid"]}/picture?type=large"
member.save
return member
else
registered_member = Member.where(:email => auth.info.email).first
if registered_member
return registered_member
else
member = Member.create(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
member_pic: "https://graph.facebook.com/#{auth["uid"]}/picture?type=large",
password:Devise.friendly_token[0,20]
)
end
end
end
回答2:
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.
回答3:
In omniauth.rb
:secure_image_url => true
Really does the thing.
I have a polymorphic Attachment
class that I attach to user.photo
.
Here are my "Interactor" and Attachment
class snippets:
class CreateAttachmentFromUrl
include Interactor
require "open-uri"
## Create user service object
# @param url => url
# @param user => current_user
def call
url = context.url
user = context.user
file = nil
begin
file = URI.open(url)
rescue => e
file = File.open('user_avatar.jpeg', 'wb') do |file|
file << URI.open(url).read
end
end
context.attachment = Attachment.create!({ user: user }) do |a|
a.data.attach(io: file, filename: 'user_avatar.jpeg')
end
rescue => e
context.fail!(message: e.message)
end
end
include Rails.application.routes.url_helpers
class Attachment < ApplicationRecord
has_one_attached :data
belongs_to :user, required: true
belongs_to :attachable, polymorphic: true, optional: true
def url
if data.attached?
uri = URI(data.service_url)
uri.query = nil
uri.to_s
else
nil
end
end
end
来源:https://stackoverflow.com/questions/30748779/want-to-save-facebook-image-into-my-rails-app