Class: StaticSync::Uploadable

Inherits:
Struct
  • Object
show all
Defined in:
lib/static_sync/uploadable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config

Returns:

  • (Object)

    the current value of config



10
11
12
# File 'lib/static_sync/uploadable.rb', line 10

def config
  @config
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



10
11
12
# File 'lib/static_sync/uploadable.rb', line 10

def path
  @path
end

Instance Method Details

#cache_timeObject



36
37
38
39
40
41
42
43
44
# File 'lib/static_sync/uploadable.rb', line 36

def cache_time
  if mime
    type = mime.sub_type
    type = mime.media_type if mime.media_type == "image"
    if config.cache.has_key?(type)
      return config.cache[type].to_i
    end
  end
end

#contentObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/static_sync/uploadable.rb', line 46

def content
  @content ||= begin
    result = File.open(path, 'rb') { |f| f.read }
    if gzipped?
      result = File.open(gzip(result), 'rb') { |f| f.read }
    end
    result
  rescue
    ""
  end
end

#content_encodingObject



30
31
32
33
34
# File 'lib/static_sync/uploadable.rb', line 30

def content_encoding
  if gzipped?
    'gzip'
  end
end

#content_typeObject



20
21
22
23
24
# File 'lib/static_sync/uploadable.rb', line 20

def content_type
  if mime
    MIME::Type.simplified(mime)
  end
end

#etagObject



58
59
60
# File 'lib/static_sync/uploadable.rb', line 58

def etag
  @etag ||= Digest::MD5.hexdigest(content)
end

#gzipped?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/static_sync/uploadable.rb', line 26

def gzipped?
  mime && mime.ascii?
end

#headersObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/static_sync/uploadable.rb', line 66

def headers
  base_headers = {
    :key           => path,
    :body          => content,
    :etag          => etag,
    :content_md5   => md5,
    :storage_class => 'REDUCED_REDUNDANCY',
    :public        => true
  }
  base_headers.merge!(:cache_control    => "public, max-age=#{cache_time}")         if cache_time
  base_headers.merge!(:content_type     => content_type)                            if content_type
  base_headers.merge!(:content_encoding => content_encoding)                        if content_encoding
  base_headers.merge!(:expires          => CGI.rfc1123_date(Time.now + cache_time)) if cache_time
  base_headers
end

#md5Object



62
63
64
# File 'lib/static_sync/uploadable.rb', line 62

def md5
  @md5 ||= Digest::MD5.base64digest(content)
end

#mimeObject



16
17
18
# File 'lib/static_sync/uploadable.rb', line 16

def mime
  MIME::Types::of(path).first
end

#versionObject



12
13
14
# File 'lib/static_sync/uploadable.rb', line 12

def version
  Storage::Version.new(path, etag)
end