Created
January 24, 2018 22:36
-
-
Save cknoxrun/b492fa25811446ca0f163b095446eb8f to your computer and use it in GitHub Desktop.
This file contains 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 :update do | |
desc "Upload all Paperclip attachments to S3" | |
task s3: [:environment] do | |
models = { | |
documents: [Document, :file], | |
ms_ms_peak_assignments: [MsMsPeakAssignment, :assignment_view], | |
sops: [Sop, :file] | |
} | |
models.each do |name, (klass, attachment_method)| | |
Dir.glob("public/system/#{name}/#{attachment_method.to_s.pluralize}/**/*").reject { |f| File.directory?(f) }.each do |path| | |
attachment = File.open path | |
id = File.expand_path(attachment).match(/([\d\/]+)\/original\/.+$/)[1].remove("/").to_i | |
puts "Re-saving #{klass} ##{id}..." | |
record = klass.find_by(id: id) | |
if record.nil? | |
puts "\tCan't find record" | |
next | |
end | |
# Paperclip will re-save the attachment using the new settings | |
record.send("#{attachment_method}=", attachment) | |
record.save! | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment