Permalink
Browse files

Features:

 * config.cname accepts Proc
 * rewrite css files when image changes
 * ability to disable s3 logging
  • Loading branch information...
1 parent 08531fd commit 4254f47ff43628a8a47142e4fc3be71ea1cb1346 @zabojnik zabojnik committed with Jul 27, 2010
Showing with 46 additions and 7 deletions.
  1. +29 −4 lib/cloudfront_asset_host.rb
  2. +12 −3 lib/cloudfront_asset_host/uploader.rb
  3. +5 −0 test/cloudfront_asset_host_test.rb
@@ -1,5 +1,6 @@
require 'digest/md5'
require 'cloudfront_asset_host/asset_tag_helper_ext'
+require 'digest/md5'
module CloudfrontAssetHost
@@ -18,6 +19,9 @@ module CloudfrontAssetHost
# Path to S3 config. Expects an +access_key_id+ and +secret_access_key+
mattr_accessor :s3_config
+ # Log S3 server access on a bucket
+ mattr_accessor :s3_logging
+
# Indicates whether the plugin should be enabled
mattr_accessor :enabled
@@ -30,6 +34,9 @@ module CloudfrontAssetHost
# Key-prefix under which to store gzipped assets
mattr_accessor :gzip_prefix
+ # Which extensions are likely to occur in css files
+ mattr_accessor :image_extensions
+
# Array of directories to search for asset files in
mattr_accessor :asset_dirs
@@ -44,6 +51,7 @@ def configure
self.cname = nil
self.key_prefix = ""
self.s3_config = "#{RAILS_ROOT}/config/s3.yml"
+ self.s3_logging = false
self.enabled = false
self.asset_dirs = %w(images javascripts stylesheets)
@@ -53,6 +61,8 @@ def configure
self.gzip_extensions = %w(js css)
self.gzip_prefix = "gz"
+ self.image_extensions = %w(jpg jpeg gif png)
+
yield(self)
if properly_configured?
@@ -61,10 +71,16 @@ def configure
end
def asset_host(source = nil, request = nil)
- return '' if source && disable_cdn_for_source?(source)
-
- cname_for_source = (self.cname =~ /%d/) ? self.cname % (source.hash % 4) : self.cname
- host = cname_for_source.present? ? "http://#{ cname_for_source }" : "http://#{self.bucket_host}"
+ if cname.present?
+ if cname.is_a?(Proc)
+ host = cname.call(source, request)
+ else
+ host = (cname =~ /%d/) ? cname % (source.hash % 4) : cname.to_s
+ host = "http://#{host}"
+ end
+ else
+ host = "http://#{self.bucket_host}"
+ end
if source && request && CloudfrontAssetHost.gzip
gzip_allowed = CloudfrontAssetHost.gzip_allowed_for_source?(source)
@@ -104,6 +120,15 @@ def gzip_allowed_for_source?(source)
CloudfrontAssetHost.gzip_extensions.include?(extension)
end
+ def image?(path)
+ extension = path.split('.').last
+ CloudfrontAssetHost.image_extensions.include?(extension)
+ end
+
+ def css?(path)
+ File.extname(path) == '.css'
+ end
+
def disable_cdn_for_source?(source)
source.match(exclude_pattern) if exclude_pattern.present?
end
@@ -43,6 +43,7 @@ def upload_keys_with_paths(keys_paths, options={})
def should_upload?(key, options={})
return false if CloudfrontAssetHost.disable_cdn_for_source?(key)
+ return true if CloudfrontAssetHost.css?(key) && rewrite_all_css?
options[:force_write] || !existing_keys.include?(key)
end
@@ -54,7 +55,7 @@ def gzipped_path(path)
end
def rewritten_css_path(path)
- if File.extname(path) == '.css'
+ if CloudfrontAssetHost.css?(path)
tmp = CloudfrontAssetHost::CssRewriter.rewrite_stylesheet(path)
tmp.path
else
@@ -84,6 +85,10 @@ def gzip_keys_with_paths
end
end
+ def rewrite_all_css?
+ @rewrite_all_css ||= !keys_with_paths.delete_if { |key, path| existing_keys.include?(key) || !CloudfrontAssetHost.image?(path) }.empty?
+ end
+
def existing_keys
@existing_keys ||= begin
keys = []
@@ -114,7 +119,11 @@ def ext_to_mime
end
def bucket
- @bucket ||= s3.bucket(CloudfrontAssetHost.bucket)
+ @bucket ||= begin
+ bucket = s3.bucket(CloudfrontAssetHost.bucket)
+ bucket.disable_logging unless CloudfrontAssetHost.s3_logging
+ bucket
+ end
end
def s3
@@ -132,4 +141,4 @@ def asset_dirs
end
end
-end
+end
@@ -50,6 +50,11 @@ class CloudfrontAssetHostTest < Test::Unit::TestCase
assert_equal "http://assethost-3.com", CloudfrontAssetHost.asset_host(@source)
end
+ should "call proc for asset_host" do
+ CloudfrontAssetHost.cname = Proc.new { |source, request| "http://assethost-proc.com" }
+ assert_equal "http://assethost-proc.com", CloudfrontAssetHost.asset_host(@source)
+ end
+
should "use bucket_host when cname is not present" do
CloudfrontAssetHost.cname = nil
assert_equal "http://bucketname.s3.amazonaws.com", CloudfrontAssetHost.asset_host(@source)

0 comments on commit 4254f47

Please sign in to comment.