Skip to content

Instantly share code, notes, and snippets.

@ScotterC
Created April 26, 2013 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScotterC/5467778 to your computer and use it in GitHub Desktop.
Save ScotterC/5467778 to your computer and use it in GitHub Desktop.
Find paperclip meta for images without meta
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