I would suggest creating a controller which limits access to the downloads. The trick is to serve the file through a controller, not directly from Apache. I'm assuming that you're using RestfulAuthentication and Paperclip here:
This is the model (a photo which has an image attachment)
class Photo < ActiveRecord::Base
belongs_to :user
has_attached_file :image, #:styles => {:thumbnail => "100x100#", :large => "800x600>"},
:path => ":rails_root/assets/:attachment/:id_partition/:style/:basename.:extension",
:url => "/assets/:userid/:attachment/:id/:style"
end
Note the url attribute which, in combination with the route below, will force the image to be accessed via a controller and not directly from the public directory. Also note the :path attribute which specifies a private assets directory instead of the default public one.
map.assets 'assets/:userid/:attachment/:id/:style', :controller => 'assets', :action => 'get', :conditions => {:method => :get }
Now, this is the simple controller which requires the user to be logged in, checks the user id and sends the appropriate file back to the user.
class AssetsController < ApplicationController
before_filter :login_required
def get
if current_user.id == params[:userid].to_i
@photo = Photo.find params[:id]
send_file @photo.image.path(params[:style]), :type => @photo.image_content_type, :disposition => 'inline'
else
render :nothing => true
end
end
end
You should also specify :x_sendfile => true
in send_file to improve the performance by letting Apache take the strain of actually serving the file once your controller has approved it.