回形针,如何在文件末尾附加一个随机图章?

我正在使用paperclip和我的rails 3应用程序。 我想在文件末尾附加一个随机字符串,没什么可长或疯狂来缓存CDN。 有人知道一个真正简单的方法吗?

这是我目前的情况:

has_attached_file :photo, :styles => { :thumb => "70x70>" }, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml", :path => "/:rails_env/public/users/:id/:style/:basename.:extension", ..... 

我想要一个像FILENAME_31313.png这样的文件名

每次保存照片时31313是随机的。

谢谢

你可以使用这样的东西来完成工作:

 before_create :generate_random_hex private def generate_random_hex self.random_hex = ActiveSupport::SecureRandom.hex(8) end Paperclip.interpolates :random_hex do |attachment, style| attachment.instance.random_hex end 

然后修改您的回形针设置,如下所示:

 has_attached_file :photo, :styles => { :thumb => "70x70>" }, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml", :path => "/:rails_env/public/users/:id/:style/:basename_:random_hex.:extension", 

Paperclip(现在?)支持开箱即用:

 has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>"}, :url => "/system/:id_partition/:style/:hash.:extension", :hash_secret => Test2::Application.config.secret_token 

这样,图像存储在/system/000/000/006/thumb/1c4fef2bf61f39193f8606521e880cbde54e04a1.jpg 。 不过不短。 使用:basename您可以将基本名称添加到URL。 有关详细信息,请参阅https://github.com/thoughtbot/paperclip#uri-obfuscation 。