Set path for original images using paperclip in Rails?

后端 未结 2 381
再見小時候
再見小時候 2020-12-24 09:27

The situation

I have a simple model with an attached image using paperclip, which has a couple of processed styles for it (thumbnail, full, feature). At this point

相关标签:
2条回答
  • 2020-12-24 10:20

    If it is acceptable, you could skip saving the originals, by setting the default style.

      has_attached_file :image,
                        :styles => { :normal => "800x600>" },
                        :default_style => :normal
    

    If not, and you want to keep the originals, if you are using apache, you could use a .htaccess file to restrict the access to the originals directory

    <FilesMatch "^\.(jpe?g|gif|png)$">
       Order allow,deny
       Deny from all
    </Files>
    
    0 讨论(0)
  • 2020-12-24 10:26

    I would recommend using a custom interpolation that will place your original files outside the public directory. Something like this:

    
    Paperclip.interpolates :maybe_public do |attachment, style|
      style == :original ? "private" : "public"
    end
    
    has_attached_file :image, :path => ":rails_root/:maybe_public/:attachment..."
    

    This will save your :original files in a non-publicly accessible directory for protection, but still allow Paperclip access. And it will keep your thumbnails in the public directory for standard access.

    0 讨论(0)
提交回复
热议问题