Module: CloudfrontAssetHost::Uploader
- Defined in:
- lib/cloudfront_asset_host/uploader.rb
Class Method Summary collapse
- .bucket ⇒ Object
- .config ⇒ Object
- .current_paths ⇒ Object
- .delete_old_keys(dryrun, verbose, silent) ⇒ 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
- .upload!(options = {}) ⇒ Object
- .upload_keys_with_paths(keys_paths, dryrun, verbose, silent, gzip) ⇒ Object
Class Method Details
.bucket ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 141 def bucket @bucket ||= begin bucket = s3.bucket(CloudfrontAssetHost.bucket) bucket.disable_logging unless CloudfrontAssetHost.s3_logging bucket end end |
.config ⇒ Object
153 154 155 156 157 158 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 153 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
121 122 123 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 121 def current_paths @current_paths ||= Dir.glob("#{Rails.public_path}/{images,javascripts,stylesheets}/**/*").reject { |path| File.directory?(path) } end |
.delete_old_keys(dryrun, verbose, silent) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 55 def delete_old_keys(dryrun, verbose, silent) puts "-- Removing old files" unless silent counter = 0 (existing_keys - new_keys).uniq.each do |key| unless new_keys.include?(key) puts "- #{key}" if verbose counter += 1 bucket.delete_folder(key) unless dryrun end end puts "#{counter} key(s) removed" unless silent || verbose end |
.existing_keys ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 110 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
137 138 139 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 137 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
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 93 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
68 69 70 71 72 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 68 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
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 125 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
83 84 85 86 87 88 89 90 91 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 83 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
106 107 108 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 106 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
74 75 76 77 78 79 80 81 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 74 def rewritten_css_path(path) if CloudfrontAssetHost.css?(path) tmp = CloudfrontAssetHost::CssRewriter.rewrite_stylesheet(path) tmp.path else path end end |
.s3 ⇒ Object
149 150 151 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 149 def s3 @s3 ||= RightAws::S3.new(config['access_key_id'], config['secret_access_key']) end |
.upload!(options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 11 def upload!( = {}) dryrun = .delete(:dryrun) || false verbose = .delete(:verbose) || false silent = .delete(:silent) || false verbose = false if silent self.new_keys = [] puts "-- Updating uncompressed files" unless silent upload_keys_with_paths(keys_with_paths, dryrun, verbose, silent, false) if CloudfrontAssetHost.gzip puts "-- Updating compressed files" unless silent upload_keys_with_paths(gzip_keys_with_paths, dryrun, verbose, silent, true) end delete_old_keys(dryrun, verbose, silent) @existing_keys = nil end |
.upload_keys_with_paths(keys_paths, dryrun, verbose, silent, gzip) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cloudfront_asset_host/uploader.rb', line 32 def upload_keys_with_paths(keys_paths, dryrun, verbose, silent, gzip) counter = 0 keys_paths.each do |key, path| new_keys << key if !existing_keys.include?(key) || CloudfrontAssetHost.css?(path) && rewrite_all_css? puts "+ #{key}" if verbose counter += 1 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 puts "#{counter} key(s) updated" unless silent || verbose end |