Permalink
Browse files

First release

  • Loading branch information...
0 parents commit e4491bff3553885a3d73c9007626039085e3038b @menno committed Mar 18, 2010
@@ -0,0 +1 @@
+pkg
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Menno van der Sman
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
+# CloudFront AssetHost
+
+Easy deployment of your assets on CloudFront or S3. When enabled in production, your assets will be served from Cloudfront/S3 which will result in a speedier front-end.
+
+## Installing
+
+Include the gem in your app's `environment.rb` or Gemfile. It is hosted at Gemcutter[http://gemcutter.org/gems/cloudfront_asset_host]
+
+### Dependencies
+
+Make sure your s3-credentials are stored in _config/s3.yml_ like this:
+
+ access_key_id: 'access_key'
+ secret_access_key: 'secret'
+
+The gem relies on +openssl md5+ and +gzip+ utilities. Make sure they are available locally and on your servers.
+
+### Configuration
+Create an initializer to configure the plugin _config/initializers/cloudfront_asset_host.rb_
+
+ # Simple configuration
+ CloudfrontAssetHost.configure do |config|
+ config.bucket = "bucketname" # required
+ config.enabled = true if Rails.env.production? # only enable in production
+ end
+
+ # Extended configuration
+ CloudfrontAssetHost.configure do |config|
+ config.bucket = "bucketname" # required
+ config.cname = "assets.domain.com" # if you have a cname configured for your distribution or bucket
+ config.key_prefix = "app/" # if you share the bucket and want to keep things separated
+ config.s3_config = "#{RAILS_ROOT}/config/s3.yml" # Alternative location of your s3-config file
+
+ # gzip related configuration
+ config.gzip = true # enable gzipped assets (defaults to true)
+ config.gzip_extensions = ['js', 'css'] # only gzip javascript or css (defaults to %w(js css))
+ config.gzip_prefix = "gz" # prefix for gzipped bucket (defaults to "gz")
+
+ config.enabled = true if Rails.env.production? # only enable in production
+ end
+
+## Usage
+
+### Uploading your assets
+Run `CloudfrontAssetHost::Uploader.upload!(:verbose => true, :dryrun => false)` before your deployment. Put it for example in your Rakefile or capistrano-recipe. Verbose output will include information about which keys are being uploaded. Enabling _dryrun_ will skip the actual upload if you're just interested to see what will be uploaded.
+
+### Hooks
+If the plugin is enabled. Rails' internal `asset_host` and `asset_id` functionality will be overridden to point to the location of the assets on Cloudfront.
+
+### Other plugins
+When using in combination with SASS and/or asset_packager it is recommended to generate the css-files and package your assets before uploading them to Cloudfront. For example, call `Sass::Plugin.update_stylesheets` and `Synthesis::AssetPackage.build_all` first.
+
+## Compatibility
+
+Tested on Rails 2.3.5 with SASS and AssetPackager plugins
+
+## Copyright
+
+Created at Wakoopa
+
+Copyright (c) 2010 Menno van der Sman, released under the MIT license
@@ -0,0 +1,39 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the cloudfront_asset_host plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the cloudfront_asset_host plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'CloudfrontAssetHost'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gemspec|
+ gemspec.name = "cloudfront_asset_host"
+ gemspec.summary = "Rails plugin to easily and efficiently deploy your assets on Amazon's S3 or CloudFront"
+ gemspec.description = "Easy deployment of your assets on CloudFront or S3 using a simple rake-task. When enabled in production, the application's asset_host and public_paths will point to the correct location."
+ gemspec.email = "menno@wakoopa.com"
+ gemspec.homepage = "http://github.com/menno/cloudfront_asset_host"
+ gemspec.authors = ["Menno van der Sman"]
+ gemspec.add_dependency 'right_aws'
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
+end
@@ -0,0 +1 @@
+1.0.0
@@ -0,0 +1,59 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{cloudfront_asset_host}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Menno van der Sman"]
+ s.date = %q{2009-11-13}
+ s.description = %q{Easy deployment of your assets on CloudFront or S3 using a simple rake-task. When enabled in production, the application's asset_host and public_paths will point to the correct location.}
+ s.email = %q{menno@wakoopa.com}
+ s.extra_rdoc_files = [
+ "README.markdown"
+ ]
+ s.files = [
+ "MIT-LICENSE",
+ "README.markdown",
+ "Rakefile",
+ "VERSION",
+ "lib/cloudfront_asset_host.rb",
+ "lib/cloudfront_asset_host/asset_tag_helper_ext.rb",
+ "lib/cloudfront_asset_host/mime_types.yml",
+ "lib/cloudfront_asset_host/tasks.rb",
+ "lib/cloudfront_asset_host/uploader.rb",
+ "tasks/cloudfront_asset_host.rake",
+ "test/app/config/s3.yml",
+ "test/app/public/javascripts/application.js",
+ "test/cloudfront_asset_host_test.rb",
+ "test/test_helper.rb",
+ "test/uploader_test.rb"
+ ]
+ s.homepage = %q{http://github.com/menno/cloudfront_asset_host}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{Rails plugin to easily and efficiently deploy your assets on Amazon's S3 or CloudFront}
+ s.test_files = [
+ "test/cloudfront_asset_host_test.rb",
+ "test/test_helper.rb",
+ "test/uploader_test.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<right_aws>, [">= 0"])
+ else
+ s.add_dependency(%q<right_aws>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<right_aws>, [">= 0"])
+ end
+end
+
@@ -0,0 +1,103 @@
+require 'cloudfront_asset_host/asset_tag_helper_ext'
+
+module CloudfrontAssetHost
+
+ autoload :Uploader, 'cloudfront_asset_host/uploader'
+ autoload :CssRewriter, 'cloudfront_asset_host/css_rewriter'
+
+ # Bucket that will be used to store all the assets (required)
+ mattr_accessor :bucket
+
+ # CNAME that is configured for the bucket or CloudFront distribution
+ mattr_accessor :cname
+
+ # Prefix keys
+ mattr_accessor :key_prefix
+
+ # Path to S3 config. Expects an +access_key_id+ and +secret_access_key+
+ mattr_accessor :s3_config
+
+ # Indicates whether the plugin should be enabled
+ mattr_accessor :enabled
+
+ # Upload gzipped assets and serve those assets when applicable
+ mattr_accessor :gzip
+
+ # Which extensions to serve as gzip
+ mattr_accessor :gzip_extensions
+
+ # Key-prefix under which to store gzipped assets
+ mattr_accessor :gzip_prefix
+
+ class << self
+
+ def configure
+ # default configuration
+ self.bucket = nil
+ self.cname = nil
+ self.key_prefix = ""
+ self.s3_config = "#{RAILS_ROOT}/config/s3.yml"
+ self.enabled = false
+
+ self.gzip = true
+ self.gzip_extensions = %w(js css)
+ self.gzip_prefix = "gz"
+
+ yield(self)
+
+ if properly_configured?
+ enable!
+ end
+ end
+
+ def asset_host(source = nil, request = nil)
+ host = cname.present? ? "http://#{self.cname}" : "http://#{self.bucket_host}"
+
+ if source && request && CloudfrontAssetHost.gzip
+ gzip_allowed = CloudfrontAssetHost.gzip_allowed_for_source?(source)
+ gzip_accepted = !(request.headers['User-Agent'].to_s =~ /(Mozilla\/4\.0[678])|(MSIE\s[1-6])/) && request.headers['Accept-Encoding'].to_s.include?('gzip')
+
+ if gzip_accepted && gzip_allowed
+ host << "/#{CloudfrontAssetHost.gzip_prefix}"
+ end
+ end
+
+ host
+ end
+
+ def bucket_host
+ "#{self.bucket}.s3.amazonaws.com"
+ end
+
+ def enable!
+ if enabled
+ ActionController::Base.asset_host = Proc.new { |source, request| CloudfrontAssetHost.asset_host(source, request) }
+ ActionView::Helpers::AssetTagHelper.send(:alias_method_chain, :rewrite_asset_path, :cloudfront)
+ ActionView::Helpers::AssetTagHelper.send(:alias_method_chain, :rails_asset_id, :cloudfront)
+ end
+ end
+
+ def key_for_path(path)
+ key_prefix + md5sum(path)[0..8]
+ end
+
+ def gzip_allowed_for_source?(source)
+ extension = source.split('.').last
+ CloudfrontAssetHost.gzip_extensions.include?(extension)
+ end
+
+ private
+
+ def properly_configured?
+ raise "You'll need to specify a bucket" if bucket.blank?
+ raise "Could not find S3-configuration" unless File.exists?(s3_config)
+ true
+ end
+
+ def md5sum(path)
+ `openssl md5 #{path}`.split(/\s/)[1].to_s
+ end
+
+ end
+
+end
@@ -0,0 +1,37 @@
+module ActionView
+ module Helpers
+ module AssetTagHelper
+
+ private
+
+ # Override asset_id so it calculates the key by md5 instead of modified-time
+ def rails_asset_id_with_cloudfront(source)
+ if @@cache_asset_timestamps && (asset_id = @@asset_timestamps_cache[source])
+ asset_id
+ else
+ path = File.join(ASSETS_DIR, source)
+ asset_id = File.exist?(path) ? CloudfrontAssetHost.key_for_path(path) : ''
+
+ if @@cache_asset_timestamps
+ @@asset_timestamps_cache_guard.synchronize do
+ @@asset_timestamps_cache[source] = asset_id
+ end
+ end
+
+ asset_id
+ end
+ end
+
+ # Override asset_path so it prepends the asset_id
+ def rewrite_asset_path_with_cloudfront(source)
+ asset_id = rails_asset_id(source)
+ if asset_id.blank?
+ source
+ else
+ "/#{asset_id}#{source}"
+ end
+ end
+
+ end
+ end
+end
@@ -0,0 +1,65 @@
+require 'tempfile'
+
+module CloudfrontAssetHost
+ module CssRewriter
+
+ # Location of the stylesheets directory
+ mattr_accessor :stylesheets_dir
+ self.stylesheets_dir = File.join(Rails.public_path, 'stylesheets')
+
+ class << self
+ # matches optional quoted url(<path>)
+ ReplaceRexeg = /url\(["']?([^\)"']+)["']?\)/i
+
+ # Returns the path to the temporary file that contains the
+ # rewritten stylesheet
+ def rewrite_stylesheet(path)
+ contents = File.read(path)
+ contents.gsub!(ReplaceRexeg) do |match|
+ rewrite_asset_link(match, path)
+ end
+
+ tmp = Tempfile.new("cfah-css")
+ tmp.write(contents)
+ tmp.flush
+ tmp
+ end
+
+ private
+
+ def rewrite_asset_link(asset_link, stylesheet_path)
+ url = asset_link.match(ReplaceRexeg)[1]
+ if url
+ path = path_for_url(url, stylesheet_path)
+
+ if path.present? && File.exists?(path)
+ key = CloudfrontAssetHost.key_for_path(path) + path.gsub(Rails.public_path, '')
+ "url(#{CloudfrontAssetHost.asset_host}/#{key})"
+ else
+ puts "Could not extract path: #{path}"
+ asset_link
+ end
+ else
+ puts "Could not find url in #{asset_link}"
+ asset_link
+ end
+ end
+
+ def path_for_url(url, stylesheet_path)
+ if url.starts_with?('/')
+ # absolute to public path
+ File.expand_path(File.join(Rails.public_path, url))
+ else
+ # relative to stylesheet_path
+ File.expand_path(File.join(File.dirname(stylesheet_path), url))
+ end
+ end
+
+ def stylesheets_to_rewrite
+ Dir.glob("#{stylesheets_dir}/**/*.css")
+ end
+
+ end
+
+ end
+end
Oops, something went wrong.

0 comments on commit e4491bf

Please sign in to comment.