Module: CloudfrontAssetHost::Uploader
- Defined in:
- lib/cloudfront_asset_host/uploader.rb
Class Method Summary collapse
- .asset_dirs ⇒ Object
- .bucket ⇒ Object
- .config ⇒ Object
- .current_paths ⇒ Object
- .existing_keys ⇒ Object
- .ext_to_mime ⇒ Object
- .gzip_keys_with_paths ⇒ Object
- .gzipped_path(path) ⇒ Object
- .headers_for_path(extension, gzip = false) ⇒ Object
- .keys_with_paths ⇒ Object
- .rewritten_css_path(path) ⇒ Object
- .s3 ⇒ Object
- .should_upload?(key, options = {}) ⇒ Boolean
- .upload!(options = {}) ⇒ Object
- .upload_keys_with_paths(keys_paths, options = {}) ⇒ Object
Class Method Details
.asset_dirs ⇒ Object
128 129 130 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 128 def asset_dirs @asset_dirs ||= CloudfrontAssetHost.asset_dirs end |
.bucket ⇒ Object
116 117 118 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 116 def bucket @bucket ||= s3.bucket(CloudfrontAssetHost.bucket) end |
.config ⇒ Object
124 125 126 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 124 def config @config ||= YAML::load_file(CloudfrontAssetHost.s3_config) end |
.current_paths ⇒ Object
96 97 98 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 96 def current_paths @current_paths ||= Dir.glob("#{Rails.public_path}/{#{ asset_dirs }}/**/*").reject { |path| File.directory?(path) } end |
.existing_keys ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 87 def existing_keys @existing_keys ||= begin keys = [] keys.concat bucket.keys('prefix' => CloudfrontAssetHost.key_prefix).map { |key| key.name } keys.concat bucket.keys('prefix' => CloudfrontAssetHost.gzip_prefix).map { |key| key.name } keys end end |
.ext_to_mime ⇒ Object
112 113 114 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 112 def ext_to_mime @ext_to_mime ||= Hash[ *( YAML::load_file(File.join(File.dirname(__FILE__), "mime_types.yml")).collect { |k,vv| vv.collect{ |v| [v,k] } }.flatten ) ] end |
.gzip_keys_with_paths ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 74 def gzip_keys_with_paths current_paths.inject({}) do |result, path| source = path.gsub(Rails.public_path, '') if CloudfrontAssetHost.gzip_allowed_for_source?(source) key = "#{CloudfrontAssetHost.gzip_prefix}/" << CloudfrontAssetHost.key_for_path(path) << source result[key] = path end result end end |
.gzipped_path(path) ⇒ Object
50 51 52 53 54 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 50 def gzipped_path(path) tmp = Tempfile.new("cfah-gz") `gzip #{path} -q -c > #{tmp.path}` tmp.path end |
.headers_for_path(extension, gzip = false) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 100 def headers_for_path(extension, gzip = false) mime = ext_to_mime[extension] || 'application/octet-stream' headers = { 'Content-Type' => mime, 'Cache-Control' => "max-age=#{10.years.to_i}", 'Expires' => 1.year.from_now.utc.to_s } headers['Content-Encoding'] = 'gzip' if gzip headers end |
.keys_with_paths ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 65 def keys_with_paths current_paths.inject({}) do |result, path| key = CloudfrontAssetHost.key_for_path(path) + path.gsub(Rails.public_path, '') result[key] = path result end end |
.rewritten_css_path(path) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 56 def rewritten_css_path(path) if File.extname(path) == '.css' tmp = CloudfrontAssetHost::CssRewriter.rewrite_stylesheet(path) tmp.path else path end end |
.s3 ⇒ Object
120 121 122 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 120 def s3 @s3 ||= RightAws::S3.new(config['access_key_id'], config['secret_access_key']) end |
.should_upload?(key, options = {}) ⇒ Boolean
44 45 46 47 48 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 44 def should_upload?(key, ={}) return false if CloudfrontAssetHost.disable_cdn_for_source?(key) [:force_write] || !existing_keys.include?(key) end |
.upload!(options = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 9 def upload!( = {}) puts "-- Updating uncompressed files" if [:verbose] upload_keys_with_paths(keys_with_paths, ) if CloudfrontAssetHost.gzip puts "-- Updating compressed files" if [:verbose] upload_keys_with_paths(gzip_keys_with_paths, .merge(:gzip => true)) end @existing_keys = nil end |
.upload_keys_with_paths(keys_paths, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 21 def upload_keys_with_paths(keys_paths, ={}) gzip = [:gzip] || false dryrun = [:dryrun] || false verbose = [:verbose] || false keys_paths.each do |key, path| if should_upload?(key, ) puts "+ #{key}" if verbose extension = File.extname(path)[1..-1] path = rewritten_css_path(path) data_path = gzip ? gzipped_path(path) : path bucket.put(key, File.read(data_path), {}, 'public-read', headers_for_path(extension, gzip)) unless dryrun File.unlink(data_path) if gzip && File.exists?(data_path) else puts "= #{key}" if verbose end end end |