Created
October 19, 2010 17:14
-
-
Save valakirka/634593 to your computer and use it in GitHub Desktop.
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
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