【问题标题】:How to define url/path for each style with Paperclip如何使用 Paperclip 为每种样式定义 url/路径
【发布时间】:2014-12-10 14:00:15
【问题描述】:

我正在尝试为每种样式设置不同的 URL 定义,例如:

has_attached_file :asset, styles: {
                              original: "1920x1920>",
                              cropped: {:geometry => "200x200#", :processors => [:cropper] }
                            }, 
                            urls: {
                              original: "/images/:hash_path/:filename",
                              cropped: "/images/:hash_path/cropped/:filename"`
                            }

目标是根据 MD5 校验和(指纹)只存储一次相同的图像,但使用当前样式的校验和(如拇指,裁剪)而不是原始文件。

示例:10 位学生上传了同一张学校照片和选定的面部区域以创建头像。学校大照片在存储中只能保存一次,但必须保留每个头像。

到目前为止,我还没有找到一种简单的方法来使用 Paperclip 来做到这一点,它现在能够为原始图像保存指纹。我很好奇是否有一种方法可以定义每种样式的 url?这可以特别解决这个问题。

感谢任何其他建议如何继续。

编辑:/:hash_path 我的意思是这里提到的某种插值http://jonathanng.com/ruby-on-rails/getting-around-ext3-inode-limitations-using-md5-file-paths-and-paperclip-interpolations/

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    一个建议是使用插值https://github.com/thoughtbot/paperclip/wiki/Interpolations

    设置网址类似:

    :url => "/attachments/:hash_path/:basename_:style.:extension",
    

    然后在插值中:

    Paperclip.interpolates :hash_path do |attachment, style|
      hash_path = "whatever_#{style}" # generate hash path here
    end
    

    这应该将 hash_path 插值返回的任何内容放入 url 中,替换 :hash_path 键。

    【讨论】:

    • 感谢您的建议,我没有意识到插值块中也可以使用样式。所以这是我想要的行为:Paperclip.interpolates :md5_path do |attachment, style| if style == :thumb "#{attachment.instance.asset_fingerprint[0..2]}/#{attachment.instance.asset_fingerprint[3..5]}/#{attachment.instance.album.token_folder}/#{attachment.instance.asset_fingerprint}" else "#{attachment.instance.asset_fingerprint[0..2]}/#{attachment.instance.asset_fingerprint[3..5]}/#{attachment.instance.asset_fingerprint}" end end
    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2023-02-01
    相关资源
    最近更新 更多