Created
April 26, 2013 14:34
-
-
Save ScotterC/5467778 to your computer and use it in GitHub Desktop.
Find paperclip meta for images without meta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Image.where(attachment_meta: nil).each do |img| | |
meta = {} | |
Image.attachment_definitions[:attachment][:styles].merge({:original => nil}).keys.each do |style| | |
url = img.attachment.url(style) | |
uploaded_file = URI.parse(url) | |
begin | |
file = Paperclip.io_adapters.for(uploaded_file) | |
geo = Paperclip::Geometry.from_file file | |
meta[style] = {:width => geo.width.to_i, :height => geo.height.to_i, :size => file.size } | |
rescue Paperclip::Errors::NotIdentifiedByImageMagickError => e | |
meta[style] = {} | |
end | |
end | |
unless meta == {} | |
img.send("attachment_meta=", img.attachment.send(:meta_encode, meta)) | |
img.class.where(img.class.primary_key => img.id).update_all({ "attachment_meta" => img.attachment.send(:meta_encode, meta) }) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment