想要将facebook图像保存到我的rails应用程序中

时间:2015-06-10 06:35:24

标签: ruby-on-rails facebook facebook-graph-api open-uri omniauth-facebook

我正在使用ominauth在我的rails app中注册用户。我也有一个带电子邮件地址的基本注册选项。所以对于这两个用户我必须在我的文件系统中保存上传用户图片我为我的电子邮件用户定期上传文件但是对于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

如果您有任何其他解决方案,请与我们联系。

3 个答案:

答案 0 :(得分:3)

使用您的模型通过回形针保存您的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

答案 1 :(得分:1)

感谢您的回答,是的,我可以使用回形针宝石,但我已经使用cloudinary上传图片,所以我不想使用另一个基于图像的宝石只是为了强制保存来自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为我做了诀窍。

答案 2 :(得分:0)

omniauth.rb

:secure_image_url => true

真正做到这一点。

我有一个附加到Attachment的多态user.photo类。

这是我的“交互器”和Attachment类片段:

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