Skip to content

Instantly share code, notes, and snippets.

@valakirka
Created October 19, 2010 17:14
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 valakirka/634593 to your computer and use it in GitHub Desktop.
Save valakirka/634593 to your computer and use it in GitHub Desktop.
namespace :assets do
desc "Normalize all Candidate attachments names"
task :normalize => :environment do
Candidate.find(:all, :conditions => ['candidates.id >= ? AND candidates.avatar_file_name IS NOT ?', ENV['RESUME'] || 1, nil]).each do |candidate|
@old_paths = {}
@bucket = ENV['BUCKET'] || 'jobandtalent_staging'
begin
%w{original identity contact profile}.each do |style|
@old_paths.merge!({style => candidate.avatar.path(style)})
end
format = candidate.avatar.path.split('.').last
candidate.avatar_file_name = "image.#{format}"
@new_paths = {}
%w{original identity contact profile}.each do |style|
puts "Renaming #{style} image from Candidate ##{candidate.id}"
@new_paths.merge!({style => candidate.avatar.path(style)})
AWS::S3::S3Object.rename(@old_paths[style], @new_paths[style], @bucket, :copy_acl => true) unless @new_paths[style] == @old_paths[style]
end
candidate.save
rescue AWS::S3::S3Exception => e
puts "Failed renaming images for Candidate ##{candidate.id} with error '#{e.message}'. Reverting..."
%{original identity contact profile}.each do |style|
if @new_paths[style] && AWS::S3::S3Object.exists?(@new_paths[style], @bucket)
AWS::S3::S3Object.rename(@new_paths[style], @old_paths[style], @bucket, :copy_acl => true)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment