Skip to content

Instantly share code, notes, and snippets.

@nthj
Created October 2, 2012 18:14
Show Gist options
  • Save nthj/3821943 to your computer and use it in GitHub Desktop.
Save nthj/3821943 to your computer and use it in GitHub Desktop.
How to fix lost Paperclip asset pictures, because asset_file_name wasn't successfully saved
namespace :paperclip do
# Fix broken timestamps
task :fix => :environment do
# All user picture files in the production environment (directories[1])
files = User.last.picture.send(:connection).directories[Rails.env.staging? ? 2 : 1].files.select do |file|
file.key.starts_with?('users/pictures')
end.sort_by(&:last_modified)
(0..300).each do |ranged|
puts "Ranged: #{ranged}"
Paperclip.each_instance_with_attachment('User', :picture) do |instance|
# User.find_by_nickname('Cyn').tap do |instance| # Run only one person
begin
puts instance
files.each do |file|
# This shouldn't be called because of our select block above
# but, well...
next unless file.key.starts_with?('users/pictures')
[ranged, -ranged].each do |difference|
instance.picture_updated_at = file.last_modified + difference.seconds
if file.key.include?(instance.picture.hash_key)
p [instance.name, instance.nickname, instance.id, file.last_modified, file.key, '...']
puts instance.picture.url
instance.update_column :picture_updated_at, instance.picture_updated_at
end
end
end
rescue => e
puts [e.class.name, e.message, *e.backtrace].join "\n"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment