Skip to content

Commit

Permalink
Add ability to specify an exclude regex for CDN content
Browse files Browse the repository at this point in the history
  • Loading branch information
Winfield Peterson authored and Menno van der Sman committed Oct 2, 2010
1 parent ae3ddad commit 5b53f3f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/cloudfront_asset_host.rb
Expand Up @@ -33,6 +33,9 @@ module CloudfrontAssetHost
# Directories to search for asset files in
mattr_accessor :asset_dirs

# Key Regular Expression to filter out/exclude content
mattr_accessor :exclude_pattern


class << self

Expand All @@ -45,6 +48,7 @@ def configure
self.enabled = false

self.asset_dirs = "images,javascripts,stylesheets"
self.exclude_pattern = nil

self.gzip = true
self.gzip_extensions = %w(js css)
Expand All @@ -58,6 +62,8 @@ def configure
end

def asset_host(source = nil, request = nil)
return '' if source && disable_cdn_for_source?(source)

host = cname.present? ? "http://#{self.cname}" : "http://#{self.bucket_host}"

if source && request && CloudfrontAssetHost.gzip
Expand Down Expand Up @@ -98,6 +104,10 @@ def gzip_allowed_for_source?(source)
CloudfrontAssetHost.gzip_extensions.include?(extension)
end

def disable_cdn_for_source?(source)
source.match(exclude_pattern) if exclude_pattern.present?
end

private

def properly_configured?
Expand Down
3 changes: 2 additions & 1 deletion lib/cloudfront_asset_host/asset_tag_helper_ext.rb
Expand Up @@ -10,7 +10,8 @@ def rails_asset_id_with_cloudfront(source)
asset_id
else
path = File.join(ASSETS_DIR, source)
asset_id = File.exist?(path) ? CloudfrontAssetHost.key_for_path(path) : ''
rewrite_path = File.exist?(path) && !CloudfrontAssetHost.disable_cdn_for_source?(source)
asset_id = rewrite_path ? CloudfrontAssetHost.key_for_path(path) : ''

if @@cache_asset_timestamps
@@asset_timestamps_cache_guard.synchronize do
Expand Down
2 changes: 2 additions & 0 deletions lib/cloudfront_asset_host/uploader.rb
Expand Up @@ -42,6 +42,8 @@ def upload_keys_with_paths(keys_paths, options={})
end

def should_upload?(key, options={})
return false if CloudfrontAssetHost.disable_cdn_for_source?(key)

options[:force_write] || !existing_keys.include?(key)
end

Expand Down
24 changes: 24 additions & 0 deletions test/uploader_test.rb
Expand Up @@ -125,6 +125,30 @@ class UploaderTest < Test::Unit::TestCase

end

context 'with exclude_pattern' do
setup do
@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"
config.key_prefix = ""
config.s3_config = "#{RAILS_ROOT}/config/s3.yml"
config.enabled = false
config.exclude_pattern = /style/
end
end

should "filter keys out" do
bucket_mock = mock
bucket_mock.expects(:put).times(3)
CloudfrontAssetHost::Uploader.stubs(:bucket).returns(bucket_mock)
CloudfrontAssetHost::Uploader.stubs(:existing_keys).returns([])

CloudfrontAssetHost::Uploader.upload!
end
end

def md5_key(path)
CloudfrontAssetHost.send(:md5sum, File.join('test/app/public', path))[0..8]
end
Expand Down

0 comments on commit 5b53f3f

Please sign in to comment.