Paperclip saving/retrieving files above public/

Hello all,

I’m quite baffled/agitated by this. I’m using paperclip to allow upload
of images to my application. This is working fine, it’s saving the the
images exactly where its supposed to and it’s saving the image objects
as its supposed to.

The problem is that I can’t display these images in the browser. I’ve
had this issue in a couple other applications I’ve made and just got
around it by having paperclip save to the public folder and pull from
there to display the desired image. But I feel as though I should get
this sorted out.

So here goes:

My image model (paperclip)

class Image < ActiveRecord::Base
class ContentType
IMAGES = [“image/png”, “image/x-png”, “image/jpg”, “image/jpeg”,
“image/pjpeg”, “image/gif”, “image/bmp”, “image/tiff”]
GIF = [“image/gif”]

def self.allowed_types
  IMAGES + GIF
end

end

belongs_to :attachable, :polymorphic => true
has_attached_file :attachment,
:path =>
“:rails_root/uploaded/:attachable_type/:attachable_id/:id_:style.:extension”,
:url => “/images/:id_:style.:extension”,
:styles => lambda { |attachment|
ContentType::IMAGES.include?(attachment.instance_read(:content_type)) ?
{ :thumb => [“80x80>”,
:png], :preview => [“400x400>”, :png], :large => [“1000x1000>”, :png] }
:
ContentType::GIF.include?(attachment.instance_read(:content_type))
?
{:thumb =>
[“80x80>”, :png], :preview => [“400x400>”, :gif], :large =>
[“1000x1000>”, :gif] } :
{} },
:default_style => :preview

validates_attachment_size :attachment, :in =>
1.kilobytes…24.megabytes
validates_attachment_content_type :attachment, :content_type =>
ContentType.allowed_types

… code omitted

end

I have included this line in my ‘config/environments/development.rb’
file
config.action_dispatch.x_sendfile_header = “X-Sendfile”

and I have the mod_xsendfile.x.x.so in my vhost:

<VirtualHost *:80>
ServerName myapp.local
DocumentRoot “/Users//Sites/myapp/public”
RackEnv development
XSendFile on
#XSendFileAllowAbove on
XSendFilePath /Users//Sites/myapp
<Directory “/Users//Sites/myapp/public”>
Order allow,deny
Allow from all

When I print out the image.url and the image.path I get:
/images/2_preview.png?1315164150
/Users//Sites/myapp/uploaded/avatars/1/2_preview.png

Any help here would be greatly appreciated. I’m at a loss as to how
this is not working.

On Sep 11, 1:29am, Keith R. [email protected] wrote:

there to display the desired image. But I feel as though I should get
GIF = [“image/gif”]
:url => “/images/:id_:style.:extension”,

When I print out the image.url and the image.path I get:
/images/2_preview.png?1315164150
/Users//Sites/myapp/uploaded/avatars/1/2_preview.png

Any help here would be greatly appreciated. I’m at a loss as to how
this is not working.

Unless you’ve got an ImagesController (not shown here), the :url
argument you’re using is the problem. Files in the public directory
are served as-is, so yours will be in /
uploaded/:attachable_type/:attachable_id/:id_:style.:extension

–Matt J.

Ok that makes sense. Thank you.

Though I’m still not getting this to work. Where do I go from here?

I created the images_controller and created routes for them. I looked
into this and saw some one else doing this in their controller to handle
‘documents’ (pdfs, jpgs, pngs, etc)

def show
@image = Image.find(params[:id])
respond_to do |format|
@imgstyle = params[:style].blank? ? # ERROR B?C imgstyle == nil
params[:format].blank? ? nil : params[:format].to_sym :
params[:style]
mime_type = case @imgstyle
when :thumb, :preview then “jpeg”
when :original then “tiff”
end

  # render show action
  format.html {}

  # send original for download
  format.original { |format| send_file @image.path(:original),

:filename => @image.filename }

  # used with image_tag helper to send file to the browser
  format.any      { |format| send_file @image.path(@imgstyle), :type

=> “image/#{mime_type}”, :filename => @image.filename, :disposition =>
‘inline’ }
end
end

and it makes sense, but how do I get my show action for Character
(characters/show.html.haml) to contact the images_controller for the
show action?

Again thank you for your help. Definitely pointed my in the right
direction.

Anyone have anything they can add? Any help at this point would be
appreciated.

Does anyone have any ideas on this?

Please I’m still desperate for an answer

why don’t you use the standard and simple way ?

this is what I have in my app :

model Product
has_attached_file :product_image, :styles => { :left =>
‘225>x238>’, :big => ‘600x600>’}

product_image_file_name :string(255)

product_image_content_type :string(255)

product_image_file_size :integer(4)

product_image_updated_at :date time

uploads are saved in /public/system/product_images/:id/big/* /public/
system/product_images/:id/left/* /public/system/product_images/:id/
original/*

I display the pictures with the following url ::
@product.product_image.url(:left) @product.product_image.url(:big)
@product.product_image.url

Try adding “public” to your path:
:path => ":rails_root/public/
uploaded/:attachable_type/:attachable_id/:id_:style.:extension "

Then you can just link directly to it:

For those of you who recommend the public folder, the only problem with
that for me and I’m sure others is that we may want some uploads to be
private, and prevent users from simply browsing the public directory on
our webapps to look at every-bodies uploads. By using our own custom
folders we can prevent this with very little code.

On Monday, 7 May 2012 14:01:50 UTC-4, Ruby-Forum.com User wrote:

For those of you who recommend the public folder, the only problem with
that for me and I’m sure others is that we may want some uploads to be
private, and prevent users from simply browsing the public directory on
our webapps to look at every-bodies uploads. By using our own custom
folders we can prevent this with very little code.

By correctly configuring your server, you can prevent this without any
code. “Options -Indexes” on Apache, “autoindex off” in nginx.

–Matt J.