Module: CloudfrontAssetHost::Uploader

Defined in:
lib/cloudfront_asset_host/uploader.rb

Class Method Summary collapse

Class Method Details

.asset_dirsObject



128
129
130
# File 'lib/cloudfront_asset_host/uploader.rb', line 128

def asset_dirs
  @asset_dirs ||= CloudfrontAssetHost.asset_dirs
end

.bucketObject



116
117
118
# File 'lib/cloudfront_asset_host/uploader.rb', line 116

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

.configObject



124
125
126
# File 'lib/cloudfront_asset_host/uploader.rb', line 124

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

.current_pathsObject



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_keysObject



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_mimeObject



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_pathsObject



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_pathsObject



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

.s3Object



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

Returns:

  • (Boolean)


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

def should_upload?(key, options={})
  return false if CloudfrontAssetHost.disable_cdn_for_source?(key)

  options[: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!(options = {})
  puts "-- Updating uncompressed files" if options[:verbose]
  upload_keys_with_paths(keys_with_paths, options)

  if CloudfrontAssetHost.gzip
    puts "-- Updating compressed files" if options[:verbose]
    upload_keys_with_paths(gzip_keys_with_paths, options.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, options={})
  gzip = options[:gzip] || false
  dryrun = options[:dryrun] || false
  verbose = options[:verbose] || false

  keys_paths.each do |key, path|
    if should_upload?(key, options)
      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