asset_id 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/asset_id.rb +36 -8
  2. metadata +4 -24
data/lib/asset_id.rb CHANGED
@@ -2,6 +2,7 @@ require 'digest/md5'
2
2
  require 'mime/types'
3
3
  require 'aws/s3'
4
4
  require 'time'
5
+ require 'yaml'
5
6
 
6
7
  module AssetID
7
8
 
@@ -68,8 +69,12 @@ module AssetID
68
69
  )
69
70
  end
70
71
 
72
+ def self.expiry_date
73
+ @expiry_date ||= (Time.now + (60*60*24*365)).httpdate
74
+ end
75
+
71
76
  def self.cache_headers
72
- {'Expires' => (Time.now + (60*60*24*365)).httpdate, 'Cache-Control' => 'public'} # 1 year expiry
77
+ {'Expires' => expiry_date, 'Cache-Control' => 'public'} # 1 year expiry
73
78
  end
74
79
 
75
80
  def self.gzip_headers
@@ -89,10 +94,24 @@ module AssetID
89
94
  super(path)
90
95
  end
91
96
 
97
+ def self.cache_path
98
+ File.join(Rails.root, 'log', 'asset_id_cache.yml')
99
+ end
100
+
92
101
  def self.upload(options={})
102
+ options[:cache] ||= true
93
103
  connect_to_s3
104
+
105
+ cache = {}
106
+ if options[:cache]
107
+ cache = YAML.load_file(cache_path) rescue {}
108
+ end
109
+
94
110
  assets.each do |asset|
95
- puts "asset_id: Uploading #{asset} as #{fingerprint(asset)}" if options[:debug]
111
+ fp = fingerprint(asset)
112
+
113
+ puts "asset_id: Uploading #{asset} as #{fp}" if options[:debug]
114
+
96
115
  mime_type = MIME::Types.of(asset).first.to_s
97
116
 
98
117
  headers = {
@@ -109,13 +128,22 @@ module AssetID
109
128
 
110
129
  puts "asset_id: headers: #{headers.inspect}" if options[:debug]
111
130
 
112
- AWS::S3::S3Object.store(
113
- fingerprint(asset),
114
- data,
115
- s3_bucket,
116
- headers
117
- ) unless options[:dry_run]
131
+ if options[:cache] and cache[asset] and cache[asset][:fingerprint] == fp
132
+ puts "asset_id: Cache hit #{asset} - doing nothing"
133
+ else
134
+ AWS::S3::S3Object.store(
135
+ fp,
136
+ data,
137
+ s3_bucket,
138
+ headers
139
+ ) unless options[:dry_run]
140
+ end
141
+
142
+ cache[asset] = {:expires => expiry_date.to_s, :fingerprint => fp}
118
143
  end
144
+
145
+ puts "cache:\n#{YAML.dump(cache)}" if options[:debug]
146
+ File.open(cache_path, 'w') {|f| f.write(YAML.dump(cache))} unless options[:dry_run]
119
147
  end
120
148
 
121
149
  end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asset_id
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 3
10
- version: 0.1.3
4
+ prerelease:
5
+ version: 0.1.4
11
6
  platform: ruby
12
7
  authors:
13
8
  - Richard Taylor
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2010-11-04 00:00:00 +00:00
13
+ date: 2011-05-14 00:00:00 +01:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,10 +21,6 @@ dependencies:
26
21
  requirements:
27
22
  - - ">="
28
23
  - !ruby/object:Gem::Version
29
- hash: 47
30
- segments:
31
- - 1
32
- - 16
33
24
  version: "1.16"
34
25
  type: :runtime
35
26
  version_requirements: *id001
@@ -41,11 +32,6 @@ dependencies:
41
32
  requirements:
42
33
  - - ">="
43
34
  - !ruby/object:Gem::Version
44
- hash: 3
45
- segments:
46
- - 0
47
- - 6
48
- - 2
49
35
  version: 0.6.2
50
36
  type: :runtime
51
37
  version_requirements: *id002
@@ -76,23 +62,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
62
  requirements:
77
63
  - - ">="
78
64
  - !ruby/object:Gem::Version
79
- hash: 3
80
- segments:
81
- - 0
82
65
  version: "0"
83
66
  required_rubygems_version: !ruby/object:Gem::Requirement
84
67
  none: false
85
68
  requirements:
86
69
  - - ">="
87
70
  - !ruby/object:Gem::Version
88
- hash: 3
89
- segments:
90
- - 0
91
71
  version: "0"
92
72
  requirements: []
93
73
 
94
74
  rubyforge_project: asset_id
95
- rubygems_version: 1.3.7
75
+ rubygems_version: 1.5.0
96
76
  signing_key:
97
77
  specification_version: 2
98
78
  summary: asset_id is a library for uploading static assets to Amazon S3.