我想知道我是否有照片的网址,我可以使用回形针在我的网站上保存它的副本吗?如果是这样的话?
答案 0 :(得分:3)
是的,实际上非常简单。我的用户模型中的代码使用了头像:
#a part of your model
has_attached_file :avatar, :styles => { :thumb => "50x50" }, :storage => :s3,
:path => "/:attachment/:id/:style/:filename",
:s3_credentials => Rails.root.join("config/s3.yml")
attr_accessor :avatar_url
before_validation :download_remote_file, :if => :avatar_url_provided?
def avatar_url_provided?
!self.avatar_url.blank?
end
def download_remote_file
self.avatar = do_download_remote_file
end
def do_download_remote_file
io = open(URI.parse(avatar_url))
def
io.original_filename
base_uri.path.split('/').last
end
io.original_filename.blank? ? nil : io
rescue Exception => exc
logger.debug "ERROR WHEN DOWNLOADING REMOTE AVATAR FOR USER #{self.id} AND REMOTE URL #{self.avatar_url} - ERROR IS #{exc.message}"
end
答案 1 :(得分:1)
当然你可以做到