Paperclip change images path after upgrade to rails 3.2 - ruby-on-rails

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.

Related

Paperclip - Using custom watermark with dynamic path

I'm trying to create a custom path using this:
has_attached_file :avatar,
:processors => [:watermark],
:styles => lambda { |attachment| {
:medium => {
:geometry => "300x300>",
:watermark_path => Customer.find(attachment.instance.customer_id).signature.path
},
:thumb => "100x100>",
}
}
This is working fine with Customer.find(attachment.instance.customer_id).signature.path with a static path to image, but in this case it is throwing an exception
Paperclip::Errors::InfiniteInterpolationError in Api::Ipad::V1::ImagesController#create
Can anyone tell how to ignore this exception ?
Usually that error comes from not explicity defining the :url and :path options for the attachment, might try setting those options and see if the interpolation error goes away.

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.

Paperclip change default path and hash image names

I'm new to Rails and using Paperclip, I have it set up on my model already without issue. I just don't like the path it's generating for my images right now (:root_path/system/users/avatars/000/000/001) I really don't even understand it. How can I modify this default path for my images to something more friendly? And how can I hash the image names?
In your model, you can set the default path, styles and url as so:
has_attached_file :avatar,
:styles => { :large => "500x500>", :medium => "300x300>", :thumb => "100x100>" },
:path => ":rails_root/public/images/:id/:style/:filename",
:url => "/images/:id/:style/:filename"
you can setup the hash on the paperclip initializer file (config/initializers/paperclip_defaults.rb)
Quoting from paperclip wiki:
Paperclip::Attachment.default_options.update({
:path => ":class/:attachment/:hash/:style.:extension",
:hash_secret => "SOME_RANDOM_SECRET"
})
The :hash part is generated from :hash_secret and the pattern given by the :hash_data option, which by default is ":class/:attachment/:id/:style/:updated_at".

Paperclip reprocess new style with new format

Below is a hash of my paperclip thumb style types. Ive added new image types in the past and backfilled only the specific versions by using the Image.find(some_id).image.reprocess! :sailthru_bid_now
This command and approach has always worked.
Recently I added a new image type :sailthru_bid_now, that uses format png. Works perfectly creating new images, but when I try to run the command to backfill the type, it runs without error, and doesn't create the new type. WEIRD! If i remove the custom format it works.
So here I am, begging for any help! Thanks a million in advance.
ManualCropper.rb
THUMB_STYLES = {
home_page_thumb: "216x223#",
thumb: "85x60#",
large_list: "150x200>",
detail: '420x2000>',
full_screen: '1024x1004>',
featured: '159x116#',
iframethumb: '266x207#',
sailthru: '274x282#',
:sailthru_bid_now => {
:geometry => '274x282#',
:watermark_path => "public/images/bid_now_watermark.jpg",
:format => 'png'
},
#IOS IMAGES SIZES
ios_small_thumb: '75x75#',
ios_large_thumb: '158x158#'
}
Image.rb:
has_attached_file(:image, :styles => Paperclip::ManualCropper::THUMB_STYLES,
:processors => [:manual_cropper,:watermark],
:storage => :s3,
:s3_credentials => Rails.application.config.s3_yaml_path,
:path => "images/:id/:style.:extension")
validates :image, :presence => true
validate :file_dimensions, :unless => "errors.any?"

How to pass additional convert options to paperclip on Heroku?

class User < ActiveRecord::Base
has_attached_file :photo, :styles => { :square => "100%", :large => "100%" },
:convert_options => {
:square => "-auto-orient -geometry 70X70#",
:large => "-auto-orient -geometry X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
validates_attachment_size :photo,
:less_than => 5.megabyte
end
Works great on local machine, but gives me an error on Heroku: There was an error processing the thumbnail for stream.20143
The thing is I want to auto-orient photos before resizing, so they resized properly.
The only working variant now(thanks to jonnii) is resizing without auto-orient:
...
as_attached_file :photo, :styles => { :square => "70X70#", :large => "X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
...
How to pass additional convert options to paperclip on Heroku?
UPD
I discover, the trouble in "-auto-orient" option. It seems like this option is broken in version of ImageMagick used by Heroku. I created custom paperclip image processor inherited from paperclip's standard thumbnail:
module Paperclip
class Ao < Thumbnail
def transformation_command
super + " -auto-orient"
end
end
end
It works perfect on local machine, but fails on Heroku.
These are the sizes I use. They all work fine on heroku:
SIZES = {
:original => "640x480>",
:thumb => "150x150#",
:mini => "60x60#",
:micro => "30x30#"
}
Make sure your gem version of paperclip is the same as heroku's. You can specify the specific gem version in your .gems file and in your environment.rb to make sure they line up.
I'm not sure exactly why your convert_options are causing problems, but if I remember correctly paperclip uses ImageScience directly and your chosen options might be incompatible with the read only heroku file system.
If this is critical and you need an answer right now I'd raise a support ticket on heroku. If you get a response make sure you post it back here!

Resources