views:

1748

answers:

3

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.

A: 

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>
andi
I have :full set to the default style. It won't skip saving the originals, it just sets the style that `@foo.image.url` returns without an argument, which is :original by default. It's just so you don't have to specify the style with `@foo.image.url(:full)` all the time.
Mike Richards
+12  A: 

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.

jyurek
Thanks, this works exactly how I wanted. I actually had to fetch the latest Paperclip from Github, since my outdated version didn't have the interpolation methods yet :)
Mike Richards
A: 

Can you also just simply pass :processors => [] to the has_attached_file call?

sam