class Middleman::S3Sync::Resource

Constants

CONTENT_MD5_KEY

Attributes

content_type[RW]
full_s3_resource[RW]
gzipped[RW]
options[RW]
partial_s3_resource[RW]
path[RW]
resource[RW]

Public Class Methods

new(resource, partial_s3_resource) click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 10
def initialize(resource, partial_s3_resource)
  @resource = resource
  @path = resource ? resource.destination_path : partial_s3_resource.key
  @partial_s3_resource = partial_s3_resource
end

Public Instance Methods

alternate_encoding?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 109
def alternate_encoding?
  status == :alternate_encoding
end
attributes()
Alias for: to_h
caching_policy() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 217
def caching_policy
  @caching_policy ||= Middleman::S3Sync.caching_policy_for(content_type)
end
caching_policy_match?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 221
def caching_policy_match?
  if (caching_policy)
    caching_policy.cache_control == full_s3_resource.cache_control
  else
    true
  end
end
create!() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 83
def create!
  say_status "#{ANSI.green{"Creating"}} #{remote_path}#{ gzipped ? ANSI.white {' (gzipped)'} : ''}"
  local_content { |body|
    bucket.files.create(to_h.merge(body: body)) unless options.dry_run
  }
end
destroy!() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 78
def destroy!
  say_status "#{ANSI.red{"Deleting"}} #{remote_path}"
  bucket.files.destroy remote_path unless options.dry_run
end
directory?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 180
def directory?
  File.directory?(local_path)
end
encoding_match?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 192
def encoding_match?
  (options.prefer_gzip && gzipped && full_s3_resource.content_encoding == 'gzip') || (!options.prefer_gzip && !gzipped && !full_s3_resource.content_encoding )
end
identical?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 113
def identical?
  status == :identical
end
ignore!() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 90
def ignore!
  if options.verbose
    reason = if redirect?
               :redirect
             elsif directory?
               :directory
             end
    say_status "#{ANSI.yellow{"Ignoring"}} #{remote_path} #{ reason ? ANSI.white {"(#{reason})" } : "" }"
  end
end
key()
Alias for: remote_path
local?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 168
def local?
  File.exist?(local_path) && resource
end
local_content(&block) click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 125
def local_content(&block)
  File.open(local_path, &block)
end
local_content_md5() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 204
def local_content_md5
  @local_content_md5 ||= Digest::MD5.hexdigest(File.read(original_path))
end
local_object_md5() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 200
def local_object_md5
  @local_object_md5 ||= Digest::MD5.hexdigest(File.read(local_path))
end
local_path() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 69
def local_path
  local_path = build_dir + '/' + path.gsub(/^#{options.prefix}/, '')
  if options.prefer_gzip && File.exist?(local_path + ".gz")
    @gzipped = true
    local_path += ".gz"
  end
  local_path
end
original_path() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 208
def original_path
  gzipped ? local_path.gsub(/\.gz$/, '') : local_path
end
redirect?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 176
def redirect?
  full_s3_resource && full_s3_resource.metadata.has_key?('x-amz-website-redirect-location')
end
relative_path() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 184
def relative_path
  @relative_path ||= local_path.gsub(/#{build_dir}/, '')
end
remote?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 172
def remote?
  !!s3_resource
end
remote_content_md5() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 196
def remote_content_md5
  full_s3_resource.metadata[CONTENT_MD5_KEY]
end
remote_object_md5() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 188
def remote_object_md5
  s3_resource.etag
end
remote_path() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 25
def remote_path
  s3_resource ? s3_resource.key : "#{options.prefix}#{path}"
end
Also aliased as: key
s3_resource() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 16
def s3_resource
  @full_s3_resource || @partial_s3_resource
end
status() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 129
def status
  @status ||= if directory?
                if remote?
                  :deleted
                else
                  :ignored
                end
              elsif local? && remote?
                if options.force
                  :updated
                elsif not caching_policy_match?
                  :updated
                elsif local_object_md5 == remote_object_md5
                  :identical
                else
                  if !gzipped
                    # we're not gzipped, object hashes being different indicates updated content
                    :updated
                  elsif !encoding_match? || local_content_md5 != remote_content_md5
                    # we're gzipped, so we checked the content MD5, and it also changed
                    :updated
                  else
                    # we're gzipped, the object hashes differ, but the content hashes are equal
                    # this means the gzipped bits changed while the original bits did not
                    # what's more, we spent a HEAD request to find out
                    :alternate_encoding
                  end
                end
              elsif local?
                :new
              elsif remote? && redirect?
                :ignored
              elsif remote?
                :deleted
              else
                :ignored
              end
end
to_create?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 105
def to_create?
  status == :new
end
to_delete?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 101
def to_delete?
  status == :deleted
end
to_h() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 30
def to_h
  attributes = {
    :key => key,
    :acl => options.acl,
    :content_type => content_type,
    CONTENT_MD5_KEY => local_content_md5
  }

  if caching_policy
    attributes[:cache_control] = caching_policy.cache_control
    attributes[:expires] = caching_policy.expires
  end

  if options.prefer_gzip && gzipped
    attributes[:content_encoding] = "gzip"
  end

  if options.reduced_redundancy_storage
    attributes[:storage_class] = 'REDUCED_REDUNDANCY'
  end

  if options.encryption
    attributes[:encryption] = 'AES256'
  end

  attributes
end
Also aliased as: attributes
to_ignore?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 121
def to_ignore?
  status == :ignored || status == :alternate_encoding
end
to_update?() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 117
def to_update?
  status == :updated
end
update!() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 59
def update!
  local_content { |body|
    say_status "#{ANSI.blue{"Updating"}} #{remote_path}#{ gzipped ? ANSI.white {' (gzipped)'} : ''}"
    s3_resource.merge_attributes(to_h)
    s3_resource.body = body

    s3_resource.save unless options.dry_run
  }
end

Protected Instance Methods

bucket() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 230
def bucket
  Middleman::S3Sync.bucket
end
build_dir() click to toggle source
# File lib/middleman/s3_sync/resource.rb, line 234
def build_dir
  options.build_dir || 'build'
end