Paperclip不以url以适当的格式存储提取的图像

时间:2014-07-08 14:05:27

标签: ruby-on-rails paperclip omniauth omniauth-facebook

当我尝试通过图形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

您可以看到由名称&#39;图片&#39;存储的提取图像文件。但是如果要通过直接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

1 个答案:

答案 0 :(得分:0)

两个提取网址都没有问题....两个工作正常... 不同之处仅在于paperclip从URL的最后部分生成文件名... 所以在第一个网址

https://graph.facebook.com/666980153384194/图片β型=大

在第二个网址

https://scontent-a-lax.xx.fbcdn.net/hphotos-prn2/v/t1.0-9/ 10478546_662150043867205_2640371404472615909_n.jpg OH = cb2fe9d421fef3d7d2220bb48a2a36e2&安培; OE = 5418E8FB

我已经粗体化了回形针用于生成文件名的URL的部分....在这两种情况下,你都不应该为文件名真的烦恼...因为你可以做任何事情操作你想要回形针。

您可以通过检查路径public/images/users/:id(回形针路径配置)来确保上传文件