asset_id 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +1 -1
- data/lib/asset_id.rb +3 -154
- metadata +2 -2
data/README.textile
CHANGED
@@ -56,7 +56,7 @@ perform the upload for use in your deploy scripts
|
|
56
56
|
desc "uploads the current assets to s3 with stamped ids"
|
57
57
|
task :upload do
|
58
58
|
AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com" # If using EU bucket
|
59
|
-
AssetID::
|
59
|
+
AssetID::Asset.asset_paths += ['favicon.png'] # Configure additional asset paths
|
60
60
|
AssetID::S3.upload
|
61
61
|
end
|
62
62
|
|
data/lib/asset_id.rb
CHANGED
@@ -1,154 +1,3 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
require 'time'
|
5
|
-
require 'yaml'
|
6
|
-
|
7
|
-
module AssetID
|
8
|
-
|
9
|
-
class Base
|
10
|
-
DEFAULT_ASSET_PATHS = ['favicon.ico', 'images', 'javascripts', 'stylesheets']
|
11
|
-
@@asset_paths = DEFAULT_ASSET_PATHS
|
12
|
-
|
13
|
-
def self.path_prefix
|
14
|
-
File.join Rails.root, 'public'
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.asset_paths=(paths)
|
18
|
-
@@asset_paths = paths
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.asset_paths
|
22
|
-
@@asset_paths
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.absolute_path(path)
|
26
|
-
path =~ /#{path_prefix}/ ? path : File.join(path_prefix, path)
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.relative_path(path)
|
30
|
-
path.gsub(path_prefix, '')
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.assets
|
34
|
-
asset_paths.inject([]) {|assets, path|
|
35
|
-
path = absolute_path(path)
|
36
|
-
assets << path if File.exists? path and !File.directory? path
|
37
|
-
assets += Dir.glob(path+'/**/*').inject([]) {|m, file|
|
38
|
-
m << file unless File.directory? file; m
|
39
|
-
}
|
40
|
-
}
|
41
|
-
end
|
42
|
-
|
43
|
-
def self.fingerprint(path)
|
44
|
-
path = absolute_path(path)
|
45
|
-
d = Digest::MD5.hexdigest(File.read(path))
|
46
|
-
path = relative_path(path)
|
47
|
-
File.join File.dirname(path), "#{File.basename(path, File.extname(path))}-id-#{d}#{File.extname(path)}"
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
class S3 < AssetID::Base
|
53
|
-
|
54
|
-
DEFAULT_GZIP_TYPES = ['text/css', 'application/javascript']
|
55
|
-
@@gzip_types = DEFAULT_GZIP_TYPES
|
56
|
-
|
57
|
-
def self.gzip_types=(types)
|
58
|
-
@@gzip_types = types
|
59
|
-
end
|
60
|
-
|
61
|
-
def self.gzip_types
|
62
|
-
@@gzip_types
|
63
|
-
end
|
64
|
-
|
65
|
-
def self.s3_config
|
66
|
-
@@config ||= YAML.load_file(File.join(Rails.root, "config/asset_id.yml"))[Rails.env] rescue nil || {}
|
67
|
-
end
|
68
|
-
|
69
|
-
def self.connect_to_s3
|
70
|
-
AWS::S3::Base.establish_connection!(
|
71
|
-
:access_key_id => s3_config['access_key_id'],
|
72
|
-
:secret_access_key => s3_config['secret_access_key']
|
73
|
-
)
|
74
|
-
end
|
75
|
-
|
76
|
-
def self.expiry_date
|
77
|
-
@expiry_date ||= (Time.now + (60*60*24*365)).httpdate
|
78
|
-
end
|
79
|
-
|
80
|
-
def self.cache_headers
|
81
|
-
{'Expires' => expiry_date, 'Cache-Control' => 'public'} # 1 year expiry
|
82
|
-
end
|
83
|
-
|
84
|
-
def self.gzip_headers
|
85
|
-
{'Content-Encoding' => 'gzip', 'Vary' => 'Accept-Encoding'}
|
86
|
-
end
|
87
|
-
|
88
|
-
def self.s3_permissions
|
89
|
-
:public_read
|
90
|
-
end
|
91
|
-
|
92
|
-
def self.s3_bucket
|
93
|
-
s3_config['bucket']
|
94
|
-
end
|
95
|
-
|
96
|
-
def self.fingerprint(path)
|
97
|
-
#File.join "/#{self.s3_bucket}", fingerprint(path)
|
98
|
-
super(path)
|
99
|
-
end
|
100
|
-
|
101
|
-
def self.cache_path
|
102
|
-
File.join(Rails.root, 'log', 'asset_id_cache.yml')
|
103
|
-
end
|
104
|
-
|
105
|
-
def self.upload(options={})
|
106
|
-
options[:cache] ||= true
|
107
|
-
connect_to_s3
|
108
|
-
|
109
|
-
cache = {}
|
110
|
-
if options[:cache]
|
111
|
-
cache = YAML.load_file(cache_path) rescue {}
|
112
|
-
end
|
113
|
-
|
114
|
-
assets.each do |asset|
|
115
|
-
fp = fingerprint(asset)
|
116
|
-
|
117
|
-
puts "asset_id: Uploading #{relative_path(asset)} as #{fp}" if options[:debug]
|
118
|
-
|
119
|
-
mime_type = MIME::Types.of(asset).first.to_s
|
120
|
-
|
121
|
-
headers = {
|
122
|
-
:content_type => mime_type,
|
123
|
-
:access => s3_permissions,
|
124
|
-
}.merge(cache_headers)
|
125
|
-
|
126
|
-
if gzip_types.include? mime_type
|
127
|
-
data = `gzip -c #{asset}`
|
128
|
-
headers.merge!(gzip_headers)
|
129
|
-
else
|
130
|
-
data = File.read(asset)
|
131
|
-
end
|
132
|
-
|
133
|
-
puts "asset_id: headers: #{headers.inspect}" if options[:debug]
|
134
|
-
|
135
|
-
if options[:cache] and cache[relative_path(asset)] and cache[relative_path(asset)][:fingerprint] == fp
|
136
|
-
puts "asset_id: Cache hit #{relative_path(asset)} - doing nothing" if options[:debug]
|
137
|
-
else
|
138
|
-
AWS::S3::S3Object.store(
|
139
|
-
fp,
|
140
|
-
data,
|
141
|
-
s3_bucket,
|
142
|
-
headers
|
143
|
-
) unless options[:dry_run]
|
144
|
-
end
|
145
|
-
|
146
|
-
cache[relative_path(asset)] = {:expires => expiry_date.to_s, :fingerprint => fp}
|
147
|
-
end
|
148
|
-
|
149
|
-
puts "cache:\n#{YAML.dump(cache)}" if options[:debug]
|
150
|
-
File.open(cache_path, 'w') {|f| f.write(YAML.dump(cache))} if options[:cache] and !options[:dry_run]
|
151
|
-
end
|
152
|
-
|
153
|
-
end
|
154
|
-
end
|
1
|
+
require 'asset_id/asset'
|
2
|
+
require 'asset_id/cache'
|
3
|
+
require 'asset_id/backend/s3'
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: asset_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Richard Taylor
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-15 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|