Module: CloudfrontAssetHost::Uploader

Defined in:
lib/cloudfront_asset_host/uploader.rb

Class Method Summary collapse

Class Method Details

.bucketObject



109
110
111
# File 'lib/cloudfront_asset_host/uploader.rb', line 109

def bucket
  @bucket ||= s3.bucket(CloudfrontAssetHost.bucket)
end

.configObject



118
119
120
# File 'lib/cloudfront_asset_host/uploader.rb', line 118

def config
  @config ||= YAML::load_file(CloudfrontAssetHost.s3_config)
end

.current_pathsObject



89
90
91
# File 'lib/cloudfront_asset_host/uploader.rb', line 89

def current_paths
  @current_paths ||= Dir.glob("#{Rails.public_path}/{files,flash,images,javascripts,stylesheets}/**/*").reject { |path| File.directory?(path) }
end

.existing_keysObject



80
81
82
83
84
85
86
87
# File 'lib/cloudfront_asset_host/uploader.rb', line 80

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_mimeObject



105
106
107
# File 'lib/cloudfront_asset_host/uploader.rb', line 105

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_pathsObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cloudfront_asset_host/uploader.rb', line 67

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



43
44
45
46
47
# File 'lib/cloudfront_asset_host/uploader.rb', line 43

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



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cloudfront_asset_host/uploader.rb', line 93

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_pathsObject



58
59
60
61
62
63
64
65
# File 'lib/cloudfront_asset_host/uploader.rb', line 58

def keys_with_paths
  current_paths.inject({}) do |result, path|
    key = "#{CloudfrontAssetHost.plain_prefix}/" << CloudfrontAssetHost.key_for_path(path) + path.gsub(Rails.public_path, '')

    result[key] = path
    result
  end
end

.rewritten_css_path(path) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/cloudfront_asset_host/uploader.rb', line 49

def rewritten_css_path(path)
  if File.extname(path) == '.css'
    tmp = CloudfrontAssetHost::CssRewriter.rewrite_stylesheet(path)
    tmp.path
  else
    path
  end
end

.s3Object



113
114
115
116
# File 'lib/cloudfront_asset_host/uploader.rb', line 113

def s3
  env_config = config.has_key?(Rails.env) ? config[Rails.env] : config
  @s3 ||= RightAws::S3.new(env_config['access_key_id'], env_config['secret_access_key'])
end

.upload!(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cloudfront_asset_host/uploader.rb', line 9

def upload!(options = {})
  dryrun = options.delete(:dryrun) || false
  verbose = options.delete(:verbose) || false

  puts "-- Updating uncompressed files" if verbose
  upload_keys_with_paths(keys_with_paths, dryrun, verbose, false)

  if CloudfrontAssetHost.gzip
    puts "-- Updating compressed files" if verbose
    upload_keys_with_paths(gzip_keys_with_paths, dryrun, verbose, true)
  end

  @existing_keys = nil
end

.upload_keys_with_paths(keys_paths, dryrun, verbose, gzip) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cloudfront_asset_host/uploader.rb', line 24

def upload_keys_with_paths(keys_paths, dryrun, verbose, gzip)
  keys_paths.each do |key, path|
    if existing_keys.include?(key)
      puts "= #{key}" if verbose
    else
      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)
    end
  end
end