Reputation: 1680
I have a photo attachment with several styles all of which convert the image to a jpg, e.g.:
styles: { original: { geometry: "1500x1500>", format: :jpg},
large: { geometry: "1000x1000>", format: :jpg } }
The model also has a before_post_process
that renames photo's filename to a standard name:
def rename_photo
extension = File.extname(photo_file_name).downcase
self.photo.instance_write :file_name, "original#{extension}"
end
This works fine: whatever I upload is converted and uploaded to AWS S3 as a JPG
.
However, if the image is a PNG
its file_name and content_type are stored in db as original.png
and image/png
accordingly, while on S3 it is stored as a JPG
. When I query photo's url, a correct JPG
url is returned as well.
So while everything works, it bothers me that incorrect information is stored in the db.
Upvotes: 0
Views: 792
Reputation: 111
After digging into the source code for PaperClip 2.7.5, I came up with this solution that is working for me. Add this to config/initializers/paperclip.rb in your app.
# In Paperclip 2.3.1.1 to 2.7.5 (maybe more) the extension method fails to
# return the extension of an attachment with a style that has a 'format'.
module Paperclip
module Interpolations
def extension attachment, style_name
style = attachment.styles[style_name.to_s] || attachment.styles[style_name.to_s.to_sym]
style && style[:format] ? style[:format].to_s : File.extname(attachment.original_filename).gsub(/^\.+/, '')
end
end
Upvotes: 0
Reputation: 111
I am working on older rails 2.3 code and had to upgraded Paperclip from 2.3.1.1 to 2.7.5 and I have this same issue. Everything worked beautifully for years in 2.3.1.1. I have one style that resizes and converts to png. In 2.3.1.1, regardless of the extension on the original file (jpg, gif, png) Paperclip would save my styled image file as png, as I requested. When I asked for the url for my converted image the file's extension in the url was properly set as png. All of this regardless of the extension of the original image.
In Paperclip 2.7.5, my untouched has_attached_file options saves the styled image file with the same extension as the original file and the request for the url to the styled image file also returns with the extension set the same as the original file.
I observed the ImageMagick convert command that was invoked by paperclip to create the styled image and it properly refers to a png file for conversion and output. I also viewed the contents of the styled image in hex and it is formatted as a png file (original was a jpg). So it seems that the conversion is working by Paperclip is not giving the styled image the extension of png, like it used to.
has_attached_file :image,
:storage => :s3,
:s3_credentials => Application.s3_credentials,
:s3_protocol => 'https',
:path => "home_pages/:id/images/:basename-:style.:extension",
:default_style => '450',
:styles => { '450' => ['450x253>', :png] }
Unfortunately I cannot spend resources at this time to rewrite our app for Rails 4 so I am stuck here in the twilight zone. The reason I had to upgrade to the last version of paperclip that is supposed to work with Rails 2.3 is so I could upgrade the aws-sdk gem in order to use some new features of Amazon AWS.
Has anyone experienced this and maybe figured out a soulution?
Upvotes: 1