Module: Imagery::S3

Defined in:
lib/imagery/s3.rb

Defined Under Namespace

Modules: Config, Gateway

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keysObject (readonly)

Convenience attribute which returns all size keys including ‘:original`.



32
33
34
# File 'lib/imagery/s3.rb', line 32

def keys
  @keys
end

Class Method Details

.included(imagery) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/imagery/s3.rb', line 5

def self.included(imagery)
  imagery.extend Config

  # Set the default host for amazon S3. You can also set this
  # to https://s3.amazon.com if you want to force secure connections
  # on a global scale.
  imagery.s3_host "http://s3.amazonaws.com"
end

Instance Method Details

#deleteObject

Deletes all keys defined for this object, which includes ‘:original` and all keys in `sizes`.



66
67
68
69
70
71
72
# File 'lib/imagery/s3.rb', line 66

def delete
  super

  keys.each do |file|
    Gateway.delete(s3_key(file), self.class.s3_bucket)
  end
end

#initialize(*args) ⇒ Object



34
35
36
37
38
# File 'lib/imagery/s3.rb', line 34

def initialize(*args)
  super

  @keys = [@original] + sizes.keys
end

#s3_key(file) ⇒ Object

Returns the complete S3 id used for this object. The S3 id is simply composed of the prefix and filename, e.g.

  • photos/1001/original.jpg

  • photos/1001/small.jpg

  • photos/1001/tiny.jpg



60
61
62
# File 'lib/imagery/s3.rb', line 60

def s3_key(file)
  "#{prefix}/#{id}/#{ext(file)}"
end

#save(io, id = nil) ⇒ Object

Save the object as we normall would, but also upload all resulting files to S3. We set the proper content type and Cache-Control setting optimized for a cloudfront setup.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/imagery/s3.rb', line 77

def save(io, id = nil)
  super

  keys.each do |file|
    Gateway.store(s3_key(file),
      File.open(root(ext(file))),
      self.class.s3_bucket,
      :access => :public_read,
      :content_type => "image/jpeg",
      "Cache-Control" => "max-age=315360000"
    )
  end
end

#url(file = @original) ⇒ Object

If you specify a distribution domain (i.e. a cloudfront domain, or even an S3 domain with a prefix), that distribution domain is used.

Otherwise the default canonical S3 url is used.



45
46
47
48
49
50
51
# File 'lib/imagery/s3.rb', line 45

def url(file = @original)
  if self.class.s3_distribution_domain
    "#{self.class.s3_distribution_domain}#{super}"
  else
    "#{self.class.s3_host}/#{self.class.s3_bucket}#{super}"
  end
end