Created
January 27, 2015 16:03
-
-
Save daniel-nelson/05c8c8d6bbe515612bbf to your computer and use it in GitHub Desktop.
assets:copy_to_s3 rake task
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 'copy precompiled assets to S3' | |
task :copy_to_s3 => :environment do | |
Rails.logger.info('Copying compiled assets to s3...') | |
s3 = AWS::S3.new | |
bucket = s3.buckets[APP_CONFIG[:rails_assets_bucket]] | |
base_dir = Rails.root.join('public', 'assets').to_s + '/' | |
regexp_extensions = [] | |
['js', 'css', 'html', 'png', 'jpg', 'jpeg', 'gif', 'ttf', 'woff', 'svg', 'eot', 'otf'].each do |ext| | |
regexp_extensions << "\\.#{ext}" | |
regexp_extensions << "\\.#{ext}\\.gz" | |
end | |
regexp = /#{regexp_extensions.join('|')}$/i | |
gzip_lookup = {} | |
Dir[Rails.root.join('public', 'assets', '**/*.gz')].each do |asset_file_path| | |
# Overwrite non-gzipped files with gzipped | |
omit_gz_file_path = asset_file_path.sub(/\.gz$/, '') | |
FileUtils.mv(asset_file_path, omit_gz_file_path) | |
key = path_to_key(omit_gz_file_path, base_dir) | |
gzip_lookup[key] = true | |
end | |
Dir[Rails.root.join('public', 'assets', '**/*.*')].each do |asset_file_path| | |
copy_file_to_s3(regexp, bucket, base_dir, asset_file_path, gzip_lookup) | |
end | |
end | |
def path_to_key(asset_file_path, base_dir) | |
asset_file_path.sub(base_dir, '') | |
end | |
def copy_file_to_s3(regexp, bucket, base_dir, asset_file_path, gzip_lookup, force_upload=false) | |
key = path_to_key(asset_file_path, base_dir) | |
if key =~ regexp | |
if key =~ /\.js(\.gz)?$/i | |
content_type = 'application/javascript' | |
elsif key =~ /\.css(\.gz)?$/i | |
content_type = 'text/css' | |
elsif key =~ /\.html(\.gz)?$/i | |
content_type = 'text/html' | |
elsif key =~ /\.png(\.gz)?$/i | |
content_type = 'image/png' | |
elsif key =~ /\.je?pg(\.gz)?$/i | |
content_type = 'image/jpeg' | |
elsif key =~ /\.gif(\.gz)?$/i | |
content_type = 'image/gif' | |
elsif key =~ /\.ttf(\.gz)?$/i | |
content_type = 'application/x-font-ttf' | |
elsif key =~ /\.woff(\.gz)?$/i | |
content_type = 'application/x-font-woff' | |
elsif key =~ /\.svg(\.gz)?$/i | |
content_type = 'image/svg+xml' | |
elsif key =~ /\.eot(\.gz)?$/i | |
content_type = 'application/vnd.ms-fontobject' | |
elsif key =~ /\.otf(\.gz)?$/i | |
content_type = 'application/octet-stream' | |
else | |
content_type = nil | |
end | |
s3_object = bucket.objects["assets/#{key}"] | |
if force_upload || !s3_object.exists? | |
options = { | |
:file => asset_file_path, | |
:acl => :public_read, | |
:content_type => content_type, | |
:cache_control => 'public, max-age=31557600' | |
} | |
options[:content_encoding] = 'gzip' if gzip_lookup[key] | |
Rails.logger.info("uploading #{key}") | |
Rails.logger.info(' gzipped') if gzip_lookup[key] | |
s3_object.write(options) | |
else | |
Rails.logger.info("#{key} already exists") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment