-
-
Save martinstreicher/422657 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
# Examples: | |
# | |
# Reprocess all versions of User#avatar | |
# rake carrierwave:reprocess class=User mounted_uploader=avatar | |
# | |
# Reprocess the versions: thumb, small, medium for User#avatar | |
# rake carrierwave:reprocess class=User mounted_uploader=avatar versions='thumb, small, medium' | |
namespace :carrierwave do | |
## | |
# Only tested with Amazon S3 Storage | |
# Needs some minor modifications if you want to use this for File System Store, Cloud Files and GridFS probably. | |
# This should work without Ruby on Rails as well, just set a different TMP_PATH. | |
desc "Reprocesses Carrier Wave file versions of a given model." | |
task :reprocess => :environment do | |
## | |
# Load in the OPEN URI library to be able | |
# to pull down and store the original file in a temp directory | |
require 'open-uri' | |
## | |
# Default constants | |
TMP_PATH = "#{Rails.root}/tmp/carrierwave" | |
## | |
# Set environment constants | |
CLASS = ENV['class'].capitalize | |
MOUNTED_UPLOADER = ENV['mounted_uploader'].to_sym | |
VERSIONS = ENV['versions'].nil? ? Array.new : ENV['versions'].split(',').map {|version| version.strip.to_sym} | |
## | |
# Find the Model | |
MODEL = Kernel.const_get(CLASS) | |
## | |
# Create the temp directory | |
%x(mkdir -p "#{TMP_PATH}") | |
## | |
# Find all records for the provided Model | |
records = MODEL.all | |
## | |
# Output to console | |
puts "\nCarrier Wave Version Reprocessing!" | |
puts "=======================================" | |
puts "Model: #{CLASS}" | |
puts "Mounted Uploader: #{MOUNTED_UPLOADER}" | |
puts "Versions: #{VERSIONS.empty? ? "all" : VERSIONS.join(', ')}\n\n" | |
## | |
# Run through all records | |
records.each do |record| | |
## | |
# Set the mounted uploader object | |
mounted_object = record.send(MOUNTED_UPLOADER) | |
## | |
# Retrieve Filename | |
filename = mounted_object.path.split('/').last | |
## | |
# Output to console | |
puts "Reprocessing: #{filename}" | |
## | |
# Read out the original file from the remote location | |
# and write it out to the temp directory (TMP_PATH) | |
# This file will be used as the base file to reprocess | |
# the versions. Once all versions have been processed, | |
# this temp file will be directly removed. | |
open(mounted_object.url) do |original_object| | |
File.open(File.join(TMP_PATH, filename), 'w') do |temp_file| | |
temp_file.write(original_object.read) | |
end | |
end | |
## | |
# By default it will add all available versions to the versions variable | |
# which means that all available versions will be reprocessed. | |
# If the "versions" argument has been provided, then only the specified | |
# version(s) will be set to the versions variable, and thus, only these | |
# will be reprocessed. | |
versions = mounted_object.versions.map {|version| version[0]} | |
versions = VERSIONS unless VERSIONS.empty? | |
## | |
# Reprocesses the versions | |
versions.each do |version| | |
mounted_object.send(version).cache!(File.open(File.join(TMP_PATH, filename))) | |
mounted_object.send(version).store! | |
end | |
## | |
# Removes the temp file | |
%x(rm "#{TMP_PATH}/#{filename}") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment