Paperclip: Isn't creating the thumb folder? - ruby-on-rails

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.

Related

Adding Paperclip Attachment to Spree Orders Table,

I am working on an ecommerce website using Solidus, Rails. The site allows you to order photo frames & prints from a variety of options.
To print a photo a user must upload the jpg file of the photo. So, to allow that I modified the orders table and added a paperclip attachment called 'attachment'
I ran the following command
rails generate paperclip SpreeOrder attachment
Which generated the migrations, then I ran rake db:migrate
Then I created a spree/order_decorator.rb file, and added has_attached_file
module Spree::OrderDecorator
has_attached_file :attachment, styles: {
:medium => {
:geometry => "640x480",
:format => 'jpeg'
},
:thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
}, :processors => [:transcoder]
validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\z/
Spree::Order.prepend self
end
After this I ran the server, and ended up getting this error
undefined method `has_attached_file' for Spree::OrderDecorator:Module (NoMethodError)
I have configured solidus for use with paperclip only, so I am really confused as to why I am getting this error, even later I manually went and generated a paperclip.rb file in the config/initializers directory, but still I get the same error.
Please help with this!!
Thank You!!
You should add those paperclip method at class level in the prepended module:
def self.prepended(base)
base.has_attached_file
end

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

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 :)

Paperclip attachment saving with incorrect S3 URL about 50% of the time

Update:
So this is definitely related to the :hash_data option, specifically the :updated_at segment. Somehow the files are being saved to the S3 bucket with a different :updated_at value than Paperclip uses to read the file. Could this be due to some race condition, considering that it occurs intermittently? As I mentioned below, this issue began after upgrading Paperclip to 4.2.1.
I will greatly appreciate any thoughts/tips you guys have on this. Thank you!
When uploading images via Paperclip to S3 bucket, it sometimes saves the files with a different filename than that returned by the attachment#url method. For example, an image is saved to
main_event_photos_46_47fd4f3c2fea17fbb7a0bd27c648911557f9e12b_main.png
However calling #event.main_event_photo.url(:main) returns
main_event_photos_46_15744de74a36207b672356b5ad4c6b30eb4ba85f_main.png
So as you can see, the :hash section of the interpolation does not match, and I have no way of finding the actual url besides opening the bucket in the S3 console. This issue seems to occur about half the time. Sometimes uploading the exact same file does save properly, and the url method accesses it correctly.
This issue began occurring after we upgraded Rails/Ruby/Paperclip. We're now using:
Ruby 2.1.5
Rails 4.2.0
Paperclip 4.2.1
Note that on development, files always save correctly (local filesystem). I have scoured Stackoverflow and Google to no avail. Please let me know if I can provide any additional information. Thank you!
EDIT:
Model:
has_attached_file :main_event_photo, {
:styles => { :original => {:geometry => "1280x800#", :format => 'png'},
:main => {:geometry => "640x400#", :format => 'png'},
:thumb => {:geometry => "330x220#", :format => 'png'}
},
:convert_options => {:original => '-quality 80',
:main => '-quality 80',
:thumb => '-quality 80'
},
:default_style => :main
}.merge!(PAPERCLIP_STORAGE_OPTIONS) # this is defined in the config/environments
validates_attachment_content_type :main_event_photo, :content_type => ['image/jpeg', 'image/png', 'image/gif', 'image/x-png', 'image/pjpeg']
validates_attachment_presence :main_event_photo
Form (basically):
<%= simple_form_for(#event, :url => { :action => #event.id.nil? ? "create" : "update" }) do |f| %>
<%= f.file_field :main_event_photo %>
<% end %>
Note we have many models with Paperclip attachments, and the issue occurs on each.
So this turned out to be the result of a bug. I upgraded Rails to 4.2.1.rc1 which was released last week, and the issue was resolved. If anyone wants more information, check out the thread on Github: https://github.com/thoughtbot/paperclip/issues/1772. It includes a workaround for those who can't upgrade Rails.

Getting Image Paperclip::Errors::NotIdentifiedByImageMagickError after updating to Paperclip 3.4.0

I got the following message after upgrading:
Paperclip 3.0 introduces a non-backward compatible change in your
attachment path. This will help to prevent attachment name clashes
when you have multiple attachments with the same name. If you didn't
alter your attachment's path and are using Paperclip's default, you'll
have to add :path and :url to your has_attached_file definition.
For example:
has_attached_file :avatar,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
So I did so:
post.rb:
has_attached_file :image, :styles => { :medium => "170x300>",
:thumb => "142x185>" },
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
But then I saw this error message:
Image Paperclip::Errors::NotIdentifiedByImageMagickError
I even added this to environments/development.rb:
Paperclip.options[:command_path] = "/usr/bin/"
(which identify outputs /usr/bin/identify)
But still no luck.
What could be the problem?
Wow, I didn't expect this. The problem wasn't due to upgrading.
It was because the file I was uploading was named like this:
Screenshot at 2012-11-26 16:22:44.png
Weird.
The issue is in the filename.
colons are not accepted, if you remove the colon from the attachment name using gsub it'll be accepted always.

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

Resources