想要将 Facebook 图片保存到我的 rails 应用中
Want to save facebook image into my rails app
我正在使用 ominauth 来注册我的 rails 中的用户 app.i 也有一个包含电子邮件地址的基本注册选项。因此,对于这两个用户,我必须在我的文件系统中保存上传用户图片,我为我的电子邮件用户定期上传文件,但对于 FB 用户,我希望他们的图片保存在我的光盘中,这样我就可以使用相同的代码而不使用 FB 图 link 显示时。
FB 以这种格式发送图片并附上图 API
http://graph.facebook.com/100007619644580/picture?type=large
如何将其保存在我存储所有用户图像的 public 文件夹中。
我试过了
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
如果您有任何其他解决方案,请告诉我。
使用您的模型通过回形针保存您的 Facebook 图片
见下文:
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
感谢您的回答,是的,我可以使用回形针 gem 但我已经在使用 cloudinary 上传图片所以我不想使用其他基于 gem 的图片来强制从 fb 用户配置文件保存文件。
我能够锻炼以下代码更改。
在omniauth.rb
:secure_image_url => true
对于带有 :scope
的最后一个参数,这允许我和安全 URL。
在我的授权控制器中
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
帮了我大忙。
在omniauth.rb
:secure_image_url => true
真正做到了。
我有一个多态 Attachment
class 我附加到 user.photo
.
这是我的“交互器”和 Attachment
class 片段:
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
我正在使用 ominauth 来注册我的 rails 中的用户 app.i 也有一个包含电子邮件地址的基本注册选项。因此,对于这两个用户,我必须在我的文件系统中保存上传用户图片,我为我的电子邮件用户定期上传文件,但对于 FB 用户,我希望他们的图片保存在我的光盘中,这样我就可以使用相同的代码而不使用 FB 图 link 显示时。
FB 以这种格式发送图片并附上图 API
http://graph.facebook.com/100007619644580/picture?type=large
如何将其保存在我存储所有用户图像的 public 文件夹中。
我试过了
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
如果您有任何其他解决方案,请告诉我。
使用您的模型通过回形针保存您的 Facebook 图片 见下文:
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
感谢您的回答,是的,我可以使用回形针 gem 但我已经在使用 cloudinary 上传图片所以我不想使用其他基于 gem 的图片来强制从 fb 用户配置文件保存文件。 我能够锻炼以下代码更改。
在omniauth.rb
:secure_image_url => true
对于带有 :scope
的最后一个参数,这允许我和安全 URL。
在我的授权控制器中
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
帮了我大忙。
在omniauth.rb
:secure_image_url => true
真正做到了。
我有一个多态 Attachment
class 我附加到 user.photo
.
这是我的“交互器”和 Attachment
class 片段:
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