Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set path for original images using paperclip in Rails?

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 it works as it should, and makes a directory structure for each object in /public/assets/foo/, containing subdirectories for original, thumbnail, full, and feature.

The problem

I don't want the original (high resolution) images to be exposed for users to obtain. So I'm hoping there is a way to specify a different path to store the originals somewhere outside of /public/. Ideally paperclip should still be able to reprocess the styles using that original image as the source, as it does currently.

I'm also open to alternative suggestions for making the originals inaccessible to outside users. Whatever is the most practical solution here. Thanks.

like image 581
Michael Richards Avatar asked May 12 '09 05:05

Michael Richards


1 Answers

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.

like image 55
jyurek Avatar answered Sep 30 '22 08:09

jyurek