当我尝试通过图形 URL(https://graph.facebook.com)获取 facebook 图像时,回形针将 avatar_image_name 存储为数据库中的“图片”。代码示例:
img = UserImages.new
img.avatar = URI.parse('https://graph.facebook.com/666980153384194/picture?type=large')
img.save
控制台日志中的 MySQL 查询:
=> #<URI::HTTPS:0x00000009954490 URL:https://graph.facebook.com/666980153384194/picture?type=large>
irb(main):009:0> img.save
(0.0ms) BEGIN
SQL (0.0ms) INSERT INTO `user_images` (`avatar_content_type`, `avatar_file_name`, `avatar_file_size`, `avatar_updated
_at`, `created_at`, `updated_at`) VALUES ('image/jpeg', 'picture', 6157, '2014-07-08 13:55:53', '2014-07-08 13:56:02', '
2014-07-08 13:56:02')
(29.1ms) COMMIT
如您所见,提取的图像文件以名称“图片”存储。但如果通过直接 URL 获取图像,例如:
https://scontent-a-lax.xx.fbcdn.net/hphotos-prn2/v/t1.0-9/10478546_662150043867205_2640371404472615909_n.jpg?oh=cb2fe9d421fef3d7d2220bb48a2a36e2&oe=5418E8FB
获取的图像存储为:
(0.0ms) BEGIN
Command :: file -b --mime "C:/Users/Windows/AppData/Local/Temp/f1620d075c0642a77f7b98e532d8a8eb20140708-1040-1h5z4e6.jpg
"
SQL (1.0ms) INSERT INTO `user_images` (`avatar_content_type`, `avatar_file_name`, `avatar_file_size`, `avatar_updated
_at`, `created_at`, `updated_at`) VALUES ('image/jpeg', '10478546_662150043867205_2640371404472615909_n.jpg', 25862, '20
14-07-08 14:02:45', '2014-07-08 14:02:49', '2014-07-08 14:02:49')
(34.1ms) COMMIT
=> true
任何建议(解决方案)将不胜感激。
UserImages 类的来源:
class UserImages < ActiveRecord::Base
belongs_to :imageable ,polymorphic: true
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "125x125>" },
:path => ":rails_root/public/images/users/:id/:style/:hash.:extension",
:default_url => "/images/normal/missing.jpg",
:url => "/images/users/:id/:style/:hash.:extension",
:hash_secret => "EWRWerrew234UTY"
validates_attachment :avatar, :content_type => { :content_type => ["image/jpeg", "image/gif", "image/png"] },
:size => { :in => 0..5.megabytes }
end