Set restrictions for files in /public folder with rails and paperclip - ruby-on-rails

I'm using paperclip for image storage, and i have a problem. When the file is uploaded, paperclip generates two files, a processed image with a watermark and the original file, in this case the image files are in public folder, now the question is, can i restric the url in case the user enter into it like:
localhost:3000/files/photos/image_processeds/57308cd52cb1be0846e4be9f/original/image.png
If the user enter into the link, it will be forbidden.
This is my paperclip config
has_mongoid_attached_file :image_original,
:url => "/files/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/:class/:attachment/:id/:style/:basename.:extension"
and
has_mongoid_attached_file :image_processed,
processors: [:watermark],
styles: {
thumb: ['150x150', :jpg],
small: ['350x300', :jpg],
medium: ['550x500', :jpg],
original: {geometry: '60%',watermark_path: "#{Rails.root}/public/images/logo.gif", position: "Center"}
},
:url => "/files/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/:class/:attachment/:id/:style/:basename.:extension"
Theres is a way to do it or maybe find a better approach for that?

The original/image.png should be watermarked I guess.
I mean the version without watermark should not exist after processing.
Did you check it?

Solved, a reliable solution is save a copy of the original image into the database and grant access to it only when they purchase the photo, otherwise they only can see the copies with watermark :)

Related

Ruby on rails gem paperclip How to generate missing image

I uploaded a missing.png to public/images/ folder. But the the missing.png not showing correctly.
The website requires images/small/missing.png
So I guest I should generate the missing.png for thumb small medium large size.
What should I do?
Update 1:
I manually create a folder public/images/small and put missing.png inside the small folder. The website shows the missing.png. But What's is the correct way to generate all size of missing.png?
Try to add this in your model:
has_attached_file :image, styles: { :small => "150x150>", medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"

Is there anything wrong in putting default_url paperclip images in the public directory of a Rails 4.1 app?

I have was seeing several discussions (on stackoverflow here and here, and as a bug on paperclip here) around the best way to tell the default_url of images using Paperclip and Rails so that they work fine in production with the asset pipeline. All solutions appear quite complicate.
Is there anything wrong in putting the default images in the public/ directory of the Rails app? Anything I need to worry down the line or that I am missing?
If I put images in the public/ directory and access them with the code below all appears to work correctly.
has_attached_file :image,
styles: {original: "1000x1000", medium: "530x530#", thumb: "300x300#"},
default_url: "/default-avatar_:style.png"
I generally, by convention alone include the missing styles images in the assets directory. You don't need any extra coding or complex mechanisms to have paperclip utilize them.
#Images within assets:
/app/assets/images/default-avatar_original.png
default-avatar_medium.png
default-avatar_thumb.png
# paperclip config in /app/models/user.rb
has_attached_file :image, :styles => {:original => ["1000x1000", :png],
:medium => ["530x530#", :png],
:thumb => ["300x300#", :png] },
:default_url => "/assets/default-avatar_:style.png"
Note that when specifying default_url, you exclude the /image/ directory.

Paperclip: Isn't creating the thumb folder?

I'm using the paperclip gem (3.0.4) which suddenly stopped working as it is no longer creating the thumb folder, only original. Because of this my code can't find the image that was uploaded and errors out.
In my model I have:
has_attached_file :image,
:whiny => false,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename",
:styles => { :thumb => "150x100#" }
This code hasn't changed, and when I attempt to render my view I get the following error:
/public/system/images/52/thumb/2011-03-16_07-49-52_412.jpg not found
Looking in my file system I have, /public/system/images/52/original/2011-03-16_07-49-52_412.jpg
Could this be an issue with ImageMagick?
Any idea what is causing this and how to resolve it to ensure the thumb folder is being created? Thanks for your time and assistance.

Linking to an image in production using paperclip

I have a RoR app that is using paperclip for upload images, and the color-box gem to view a larger version of the image. The problem is that it woks fine in development, but when I try to take the app to production it is looking for the image in the wrong place.
It tries to find the image in the root directory and gives an error that the file does not exist at URL/system/pictures/2/large/img.png, when it should be looking for the image in URL/s12/gallery/bsd/system/pictures/2/large/img.png
I don't know how to tell the app to look for the file in URL/s12/gallery/bsd/ Any help would be appreciated, let me know if more info is needed.
Here is how I am linking to the file in the view:
<%= link_to(image_tag(#project.picture.url(:thumb)), #project.picture.url(:large), :data=> { :colorbox => true }) %>
That gets the thumbnail to show up fine, but when clicked it says there is no file in the root directory...because there isn't.
Thank you.
has_attached_file :photo, :styles => { :small => "150x150>" },
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"
you can set the url to change the defaults. also path is the system path where url is the url for the public

Paperclip and S3: Multiple thumbnail sizes not saving

I'm using Paperclip to handle image uploads for my Rails app, and it's working great when I use system storage: multiple thumbnail sizes ("styles" in Paperclip parlance) are saved to file, and I can access any of them by passing the style name to the url method.
When I set up the app to store images on S3 (using the aws-s3 gem), however, only one image is stored in my S3 bucket. For what it's worth, only the last style listed is stored. So, if in my model, I've got:
has_attached_file :photo,
:styles => { :large => "1000x1000>",
:medium => "600x600>",
:thumb => "200x200>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:bucket => AppConstants.bucket,
:path => "pictures/:id/:filename"
Only the "thumb" size will be saved to S3.
Has anybody encountered a similar problem? How can I fix this?
I'm not sure why this works locally, but you didn't specify :style in your path declaration.

Resources