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
- .rewrite_all_css? ⇒ Boolean
- .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
143 144 145 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 143 def asset_dirs @asset_dirs ||= CloudfrontAssetHost.asset_dirs end |
.bucket ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 124 def bucket @bucket ||= begin bucket = s3.bucket(CloudfrontAssetHost.bucket) bucket.disable_logging unless CloudfrontAssetHost.s3_logging bucket end end |
.config ⇒ Object
136 137 138 139 140 141 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 136 def config @config ||= begin config = YAML::load_file(CloudfrontAssetHost.s3_config) config.has_key?(Rails.env) ? config[Rails.env] : config end end |
.current_paths ⇒ Object
104 105 106 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 104 def current_paths @current_paths ||= Dir.glob("#{Rails.public_path}/{#{asset_dirs.join(',')}}/**/*").reject { |path| File.directory?(path) } end |
.existing_keys ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 93 def existing_keys @existing_keys ||= begin keys = [] prefix = CloudfrontAssetHost.key_prefix prefix = "#{CloudfrontAssetHost.plain_prefix}/#{prefix}" if CloudfrontAssetHost.plain_prefix.present? keys.concat bucket.keys('prefix' => prefix).map { |key| key.name } keys.concat bucket.keys('prefix' => CloudfrontAssetHost.gzip_prefix).map { |key| key.name } keys end end |
.ext_to_mime ⇒ Object
120 121 122 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 120 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
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 76 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
51 52 53 54 55 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 51 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
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 108 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
66 67 68 69 70 71 72 73 74 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 66 def keys_with_paths current_paths.inject({}) do |result, path| key = CloudfrontAssetHost.plain_prefix.present? ? "#{CloudfrontAssetHost.plain_prefix}/" : "" key << CloudfrontAssetHost.key_for_path(path) + path.gsub(Rails.public_path, '') result[key] = path result end end |
.rewrite_all_css? ⇒ Boolean
89 90 91 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 89 def rewrite_all_css? @rewrite_all_css ||= !keys_with_paths.delete_if { |key, path| existing_keys.include?(key) || !CloudfrontAssetHost.image?(path) }.empty? end |
.rewritten_css_path(path) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 57 def rewritten_css_path(path) if CloudfrontAssetHost.css?(path) tmp = CloudfrontAssetHost::CssRewriter.rewrite_stylesheet(path) tmp.path else path end end |
.s3 ⇒ Object
132 133 134 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 132 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 49 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 44 def should_upload?(key, ={}) return false if CloudfrontAssetHost.disable_cdn_for_source?(key) return true if CloudfrontAssetHost.css?(key) && rewrite_all_css? [: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 |