Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow force_write option to re-upload existing assets.

  • Loading branch information...
commit d81ebd3818406414b7368e68215c0fffefa95c09 1 parent 4aa83c7
Winfield Peterson authored committed
Showing with 36 additions and 14 deletions.
  1. +16 −11 lib/cloudfront_asset_host/uploader.rb
  2. +20 −3 test/uploader_test.rb
View
27 lib/cloudfront_asset_host/uploader.rb
@@ -7,25 +7,24 @@ module Uploader
class << self
def upload!(options = {})
- dryrun = options.delete(:dryrun) || false
- verbose = options.delete(:verbose) || false
-
- puts "-- Updating uncompressed files" if verbose
- upload_keys_with_paths(keys_with_paths, dryrun, verbose, false)
+ puts "-- Updating uncompressed files" if options[:verbose]
+ upload_keys_with_paths(keys_with_paths, options)
if CloudfrontAssetHost.gzip
- puts "-- Updating compressed files" if verbose
- upload_keys_with_paths(gzip_keys_with_paths, dryrun, verbose, true)
+ puts "-- Updating compressed files" if options[:verbose]
+ upload_keys_with_paths(gzip_keys_with_paths, options.merge(:gzip => true))
end
@existing_keys = nil
end
- def upload_keys_with_paths(keys_paths, dryrun, verbose, gzip)
+ def upload_keys_with_paths(keys_paths, options={})
+ gzip = options.delete(:gzip) || false
+ dryrun = options.delete(:dryrun) || false
+ verbose = options.delete(:verbose) || false
+
keys_paths.each do |key, path|
- if existing_keys.include?(key)
- puts "= #{key}" if verbose
- else
+ if should_upload?(key, options)
puts "+ #{key}" if verbose
extension = File.extname(path)[1..-1]
@@ -36,10 +35,16 @@ def upload_keys_with_paths(keys_paths, dryrun, verbose, gzip)
bucket.put(key, File.read(data_path), {}, 'public-read', headers_for_path(extension, gzip)) unless dryrun
File.unlink(data_path) if gzip && File.exists?(data_path)
+ else
+ puts "= #{key}" if verbose
end
end
end
+ def should_upload?(key, options={})
+ options[:force_write] || !existing_keys.include?(key)
+ end
+
def gzipped_path(path)
tmp = Tempfile.new("cfah-gz")
`gzip #{path} -q -c > #{tmp.path}`
View
23 test/uploader_test.rb
@@ -4,8 +4,8 @@ class UploaderTest < Test::Unit::TestCase
context "A configured uploader" do
setup do
- @css_md5 = CloudfrontAssetHost.send(:md5sum, 'test/app/public/stylesheets/style.css')[0..8] #7026e6ce3
- @js_md5 = CloudfrontAssetHost.send(:md5sum, 'test/app/public/javascripts/application.js')[0..8] #8ed41cb87
+ @css_md5 = md5_key('stylesheets/style.css') #7026e6ce3
+ @js_md5 = md5_key('javascripts/application.js') #8ed41cb87
CloudfrontAssetHost.configure do |config|
config.cname = "assethost.com"
config.bucket = "bucketname"
@@ -80,7 +80,7 @@ class UploaderTest < Test::Unit::TestCase
CloudfrontAssetHost::Uploader.upload!
end
- should "not re-upload existing keys" do
+ should "not re-upload existing keys by default" do
CloudfrontAssetHost::Uploader.expects(:bucket).never
CloudfrontAssetHost::Uploader.stubs(:existing_keys).returns(
["gz/#{@js_md5}/javascripts/application.js", "#{@js_md5}/javascripts/application.js",
@@ -91,6 +91,19 @@ class UploaderTest < Test::Unit::TestCase
CloudfrontAssetHost::Uploader.upload!
end
+ should "re-upload existing keys w/ force_write" do
+ bucket_mock = mock
+ bucket_mock.expects(:put).times(5)
+ CloudfrontAssetHost::Uploader.stubs(:bucket).returns(bucket_mock)
+ CloudfrontAssetHost::Uploader.stubs(:existing_keys).returns(
+ ["gz/#{@js_md5}/javascripts/application.js", "#{@js_md5}/javascripts/application.js",
+ "d41d8cd98/images/image.png",
+ "#{@css_md5}/stylesheets/style.css", "gz/#{@css_md5}/stylesheets/style.css"]
+ )
+
+ CloudfrontAssetHost::Uploader.upload!(:force_write => true)
+ end
+
should "correctly gzip files" do
path = File.join(RAILS_ROOT, 'public', 'javascripts', 'application.js')
contents = File.read(path)
@@ -111,4 +124,8 @@ class UploaderTest < Test::Unit::TestCase
end
end
+
+ def md5_key(path)
+ CloudfrontAssetHost.send(:md5sum, File.join('test/app/public', path))[0..8]
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.