Class: AssetID::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/asset_id/backend/s3.rb

Class Method Summary collapse

Class Method Details

.connect_to_s3Object



10
11
12
13
14
15
# File 'lib/asset_id/backend/s3.rb', line 10

def self.connect_to_s3
  AWS::S3::Base.establish_connection!(
    :access_key_id => s3_config['access_key_id'],
    :secret_access_key => s3_config['secret_access_key']
  )
end

.s3_bucketObject



21
22
23
# File 'lib/asset_id/backend/s3.rb', line 21

def self.s3_bucket
  s3_config['bucket']
end

.s3_bucket_urlObject



29
30
31
# File 'lib/asset_id/backend/s3.rb', line 29

def self.s3_bucket_url
  "http://#{s3_bucket}.s3.amazonaws.com"
end

.s3_configObject



6
7
8
# File 'lib/asset_id/backend/s3.rb', line 6

def self.s3_config
  @@config ||= YAML.load_file(File.join(Rails.root, "config/asset_id.yml"))[Rails.env] rescue nil || {}
end

.s3_permissionsObject



17
18
19
# File 'lib/asset_id/backend/s3.rb', line 17

def self.s3_permissions
  :public_read
end

.s3_prefixObject



25
26
27
# File 'lib/asset_id/backend/s3.rb', line 25

def self.s3_prefix
  s3_config['prefix'] || s3_bucket_url
end

.upload(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/asset_id/backend/s3.rb', line 33

def self.upload(options={})
  Asset.init(:debug => options[:debug])
  
  assets = Asset.find
  return if assets.empty?

  connect_to_s3

  assets.each do |asset|
  
    puts "AssetID: #{asset.relative_path}" if options[:debug]
  
    headers = {
      :content_type => asset.mime_type,
      :access => s3_permissions,
    }.merge(asset.cache_headers)
    
    asset.replace_css_images!(:prefix => s3_prefix) if asset.css?
    
    if asset.gzip_type?
      headers.merge!(asset.gzip_headers)
      asset.gzip!
    end
    
    if options[:debug]
      puts "  - Uploading: #{asset.fingerprint} [#{asset.data.size} bytes]"
      puts "  - Headers: #{headers.inspect}"
    end
    
    unless options[:dry_run]
      res = AWS::S3::S3Object.store(
        asset.fingerprint,
        asset.data,
        s3_bucket,
        headers
      ) 
      puts "  - Response: #{res.inspect}" if options[:debug]
    end
  end

  Cache.save! unless options[:dry_run]
end