我正在使用ominauth在我的Rails应用程序中注册用户。我也有一个基本的注册选项,也有电子邮件地址。因此,对于这两个用户,我都必须将上传用户图片保存在我的文件系统中,对于电子邮件用户,我要定期上传文件,但对于FB用户,我希望将他们的图片保存在光盘中,以便我可以使用相同的代码,而在使用FB图形链接时显示。
FB使用图形API发送这种格式的图像
http://graph.facebook.com/100007619644580/picture?type=large
如何将其保存在我存储所有用户图像的公用文件夹中。
我试过了
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
但这给我错误
No such file or directory @ rb_sysopen - http://graph.facebook.com/100007619644580/picture?type=large
让我知道你是否还有其他解决方案。
谢谢你的回答,是的,我可以使用回形针gem,但是我已经在使用cloudinary上传图像,所以我不想使用其他基于图像的gem来强制从fb用户配置文件中保存文件。我能够锻炼以下代码更改。
在omniauth.rb中
:secure_image_url => true
有关:scope
允许我使用的最后一个参数和安全网址。
并在我的身份验证控制器中
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"
而 :secure_image_url => true
奏效了我。