Paperclip attachment saving with incorrect S3 URL about 50% of the time - ruby-on-rails

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.

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

Paperclip validation issue on production

I have a Problem when I deploy my application on google cloud I get this error
has contents that are not what they are reported to be
Locally it works fine! I already tried to using the command_path. So I really don't know what I have to do next...
This is my model
has_mongoid_attached_file :image,
:styles => { :large => "380x380!" , :medium => "240x240", :small => "120x120!" },
:storage => :fog,
:fog_public => true,
:fog_directory => 'XXXX',
:path => "images/:id/:style/:basename.:extension",
:fog_credentials => { :provider => 'Google',
:google_storage_access_key_id => 'XXXXX',
:google_storage_secret_access_key => 'XXXXX'}
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
Thank you for your efforts. I hope you guys can help me
Okay I found a result. I just created a initializers/paperclip.rb file
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
Right now it work's perfectly for me.
If you have problems with ImageMagick on App Engine using Rails see this link
That issue occurs because the content-type discovered from file command returns empty string.
Actually system is not able to find the file executable so a exception is raised and empty string is returned back.
Check the code below
begin
Paperclip.run("file", "-b --mime :file", :file => '/tmp/RackMultipart20160826-15649-kwvnq2.png').split(/[:;]\s+/).first
rescue Cocaine::CommandLineError
""
end
Solution:-
Add below line in you initializer file.
Paperclip.options[:command_path] = '/usr/bin'
Looks like Google Cloud can't determine MIME type of uploaded files.
You can map file extensions to types in you initializer (application.rb, production.rb or create initializers/paperclip.rb)
Paperclip.options[:content_type_mappings] = {
:jpg => "image/jpeg",
:png => "image/png",
:gif => "image/gif"
}
But this way spoofing check won't be performed for image files.
I know I am late to the party, but in working with a legacy RoR system, I ran into this issue. The problem arose in setting the app up in Docker. Ultimately paperclip calling imagemagick was attempting to use file to identify mime-type and the minimal Docker did not have it installed. apt-get install file fixed it.

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.

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.

Paperclip change images path after upgrade to rails 3.2

i have a problem with paperclip (3.0.2) after upgrade to rails 3.2 (from 3.0.10).
Originally the path of one image was:
"http://localhost:3000/system/photos/94/small/AudiLogo.jpg?1335392139"
and after the upgrade this kind of images never show again!, but if i upload a new picture this will display fine on page, but the new path that use is:
"localhost:3000/system/products/photos/000/000/094/smal/AudiLogo.jpg?1335392139"
Whats happend in the upgrade ? There's any solution for convert the olds path to new ?
I try with "rake paperclip:refresh:missing_styles" but dosen't works.
The paperclip config section it's this.
has_attached_file :photo,
:processors => lambda { |a|
if a.external?
[:thumbnail]
else
[:thumbnail,:watermark]
end
},
:styles => {
:slider => { :geometry => "350x312#", :format => :jpg, :watermark_path => "#{Rails.root}/public/images/watermark.png", :position => "NorthEast" },
:small => "100x50>",
:medium => "200>x200",
:thumb => "100x100>",
:big => { :geometry => "640x480>", :format => :jpg, :watermark_path => "#{Rails.root}/public/images/watermark.png" }
},
:default_url => "/images/noimage.png"
Thanks in advance.
I had the same problem. You can fix this by creating a file like config/initializers/paperclip.rb and put
Paperclip::Attachment.default_options.merge!(
:path => ":rails_root/public/system/:attachment/:id/:style/:basename.:extension",
:url => "/system/:attachment/:id/:style/:basename.:extension"
)
I just had a similar upgrade and routed around my problem this way:
has_attached_file :image,
:url => "/images/photos/:id/:basename_:style.:extension",
:path => ":rails_root/public/images/photos/:id/:basename_:style.:extension",
Assuming the "small" vs "smal" difference between original and current path is a typo, the other obvious change is the addition of the two numeric segments after the "/photos/".
".../photos/000/000/094/smal/AudiLogo.jpg?1335392139"
I suspect this is coming from an id_partition being used for the path. Are you setting a different default path interpolation in some other place?
Looking at Paperclip's code I see the id_partition method that would be responsible for this but still have not found any documentation pointing in the direction of a change in the default behavior. I did't get to follow the code in the gem to determine if it is a bug or undocumented change.

Resources