diff --git a/README.md b/README.md index c3e0a88c..5d611213 100644 --- a/README.md +++ b/README.md @@ -302,6 +302,9 @@ AssetSync.config.gzip_compression == ENV['ASSET_SYNC_GZIP_COMPRESSION'] #### Rackspace (Optional) * **rackspace\_auth\_url**: Rackspace auth URL, for Rackspace London use: lon.auth.api.rackspacecloud.com +* **rackspace_allow_origin**: "*" optional - enables rackspace CORS requests to your assets by setting Allow-Access-Control-Origin - to the value provided. +* **rackspace_origin**: "mysite.com" optional - for sets the origin header for assets. + ## Amazon S3 Multiple Region Support diff --git a/lib/asset_sync/config.rb b/lib/asset_sync/config.rb index aa976020..cd194a18 100644 --- a/lib/asset_sync/config.rb +++ b/lib/asset_sync/config.rb @@ -10,6 +10,8 @@ class Invalid < StandardError; end attr_accessor :manifest attr_accessor :fail_silently attr_accessor :always_upload + attr_accessor :always_upload_all + attr_accessor :warn_on_failure attr_accessor :ignored_files attr_accessor :prefix attr_accessor :public_path @@ -17,6 +19,7 @@ class Invalid < StandardError; end attr_accessor :custom_headers attr_accessor :run_on_precompile + # FOG configuration attr_accessor :fog_provider # Currently Supported ['AWS', 'Rackspace'] attr_accessor :fog_directory # e.g. 'the-bucket-name' @@ -26,7 +29,7 @@ class Invalid < StandardError; end attr_accessor :aws_access_key_id, :aws_secret_access_key, :aws_reduced_redundancy # Rackspace - attr_accessor :rackspace_username, :rackspace_api_key, :rackspace_auth_url + attr_accessor :rackspace_username, :rackspace_api_key, :rackspace_auth_url, :rackspace_origin, :rackspace_allow_origin # Google Storage attr_accessor :google_storage_secret_access_key, :google_storage_access_key_id @@ -50,9 +53,13 @@ def initialize self.manifest = false self.fail_silently = false self.always_upload = [] + self.always_upload_all = false self.ignored_files = [] self.custom_headers = {} self.enabled = true + self.warn_on_failure=false + self.rackspace_allow_origin = "" + self.rackspace_origin = "" self.run_on_precompile = true load_yml! if defined?(Rails) && yml_exists? end @@ -121,16 +128,18 @@ def public_path end def load_yml! - self.enabled = yml["enabled"] if yml.has_key?('enabled') - self.fog_provider = yml["fog_provider"] - self.fog_directory = yml["fog_directory"] - self.fog_region = yml["fog_region"] - self.aws_access_key_id = yml["aws_access_key_id"] - self.aws_secret_access_key = yml["aws_secret_access_key"] - self.aws_reduced_redundancy = yml["aws_reduced_redundancy"] - self.rackspace_username = yml["rackspace_username"] - self.rackspace_auth_url = yml["rackspace_auth_url"] if yml.has_key?("rackspace_auth_url") - self.rackspace_api_key = yml["rackspace_api_key"] + self.enabled = yml["enabled"] if yml.has_key?('enabled') + self.fog_provider = yml["fog_provider"] + self.fog_directory = yml["fog_directory"] + self.fog_region = yml["fog_region"] + self.aws_access_key_id = yml["aws_access_key_id"] + self.aws_secret_access_key = yml["aws_secret_access_key"] + self.aws_reduced_redundancy = yml["aws_reduced_redundancy"] + self.rackspace_username = yml["rackspace_username"] + self.rackspace_auth_url = yml["rackspace_auth_url"] if yml.has_key?("rackspace_auth_url") + self.rackspace_api_key = yml["rackspace_api_key"] + self.rackspace_allow_origin = yml["rackspace_allow_origin"] if yml.has_key?("rackspace_allow_origin") + self.rackspace_origin = yml["rackspace_origin"] if yml.has_key?("rackspace_origin") self.google_storage_secret_access_key = yml["google_storage_secret_access_key"] self.google_storage_access_key_id = yml["google_storage_access_key_id"] self.existing_remote_files = yml["existing_remote_files"] if yml.has_key?("existing_remote_files") @@ -138,6 +147,8 @@ def load_yml! self.manifest = yml["manifest"] if yml.has_key?("manifest") self.fail_silently = yml["fail_silently"] if yml.has_key?("fail_silently") self.always_upload = yml["always_upload"] if yml.has_key?("always_upload") + self.always_upload_all = yml["always_upload_all"] if yml.has_key?("always_upload") + self.warn_on_failure = yml["warn_on_failure"] if yml.has_key?("warn_on_failure") self.ignored_files = yml["ignored_files"] if yml.has_key?("ignored_files") self.custom_headers = yml["custom_headers"] if yml.has_key?("custom_headers") self.run_on_precompile = yml["run_on_precompile"] if yml.has_key?("run_on_precompile") diff --git a/lib/asset_sync/multi_mime.rb b/lib/asset_sync/multi_mime.rb index d1d3cf09..880f735b 100644 --- a/lib/asset_sync/multi_mime.rb +++ b/lib/asset_sync/multi_mime.rb @@ -1,17 +1,16 @@ +require 'mime/types' + module AssetSync class MultiMime def self.lookup(ext) - - if defined?(Mime::Type) - Mime::Type.lookup_by_extension(ext) - elsif defined?(Rack::Mime) - ext_with_dot = ".#{ext}" - Rack::Mime.mime_type(ext_with_dot) - else - MIME::Types.type_for(ext).first - end - + "#{MIME::Types.type_for(ext).first}" + #if defined?(Rack::Mime) + # ext_with_dot = ".#{ext}" + # Rack::Mime.mime_type(ext_with_dot) + #else + # + #end end end diff --git a/lib/asset_sync/storage.rb b/lib/asset_sync/storage.rb index c8e96eca..a8f95756 100644 --- a/lib/asset_sync/storage.rb +++ b/lib/asset_sync/storage.rb @@ -134,6 +134,18 @@ def upload_file(f) gzipped = "#{path}/#{f}.gz" ignore = false + if config.rackspace_allow_origin.present? + file.merge!({ + :access_control_allow_origin => config.rackspace_allow_origin + }) + end + + if config.rackspace_origin.present? + file.merge!({ + :origin => config.rackspace_origin + }) + end + if config.gzip? && File.extname(f) == ".gz" # Don't bother uploading gzipped assets if we are in gzip_compression mode # as we will overwrite file.css with file.css.gz if it exists. @@ -180,15 +192,22 @@ def upload_file(f) end def upload_files - # get a fresh list of remote files - remote_files = ignore_existing_remote_files? ? [] : get_remote_files - # fixes: https://github.com/rumblelabs/asset_sync/issues/19 - local_files_to_upload = local_files - ignored_files - remote_files + always_upload_files + local_files_to_upload = local_files - ignored_files + unless config.always_upload_all + # get a fresh list of remote files + remote_files = ignore_existing_remote_files? ? [] : get_remote_files + local_files_to_upload = local_files_to_upload - remote_files + always_upload_files + end # Upload new files local_files_to_upload.each do |f| next unless File.file? "#{path}/#{f}" # Only files. - upload_file f + begin + upload_file f + rescue Exception => ex + log "error - #{$!} - file: #{f}" + raise ex unless config.warn_on_failure + end end end diff --git a/lib/asset_sync/version.rb b/lib/asset_sync/version.rb index 4edeef8a..e8e63523 100644 --- a/lib/asset_sync/version.rb +++ b/lib/asset_sync/version.rb @@ -1,3 +1,3 @@ module AssetSync - VERSION = "0.5.4" + VERSION = "0.5.5" end diff --git a/spec/fixtures/rackspace_with_yml/config/asset_sync.yml b/spec/fixtures/rackspace_with_yml/config/asset_sync.yml index d693fdf5..604b8260 100644 --- a/spec/fixtures/rackspace_with_yml/config/asset_sync.yml +++ b/spec/fixtures/rackspace_with_yml/config/asset_sync.yml @@ -2,6 +2,8 @@ defaults: &defaults fog_provider: "Rackspace" rackspace_username: "xxxx" rackspace_api_key: "zzzz" + rackspace_origin: "mysite.com" + rackspace_allow_origin: "*" region: "eu-west-1" development: diff --git a/spec/integration/aws_integration_spec.rb b/spec/integration/aws_integration_spec.rb index 2e48208b..a6c71870 100644 --- a/spec/integration/aws_integration_spec.rb +++ b/spec/integration/aws_integration_spec.rb @@ -31,7 +31,7 @@ def execute(command) end it "sync" do - execute "rake ASSET_SYNC_PREFIX=#{@prefix} assets:precompile" + execute "rake FOG_PROVIDER=AWS ASSET_SYNC_PREFIX=#{@prefix} assets:precompile" bucket(@prefix).files.size.should == 5 app_js = bucket(@prefix).files.get("#{@prefix}/application.js") @@ -43,12 +43,12 @@ def execute(command) end it "sync with enabled=false" do - execute "rake ASSET_SYNC_PREFIX=#{@prefix} ASSET_SYNC_ENABLED=false assets:precompile" + execute "rake FOG_PROVIDER=AWS ASSET_SYNC_PREFIX=#{@prefix} ASSET_SYNC_ENABLED=false assets:precompile" bucket(@prefix).files.size.should == 0 end it "sync with gzip_compression=true" do - execute "rake ASSET_SYNC_PREFIX=#{@prefix} ASSET_SYNC_GZIP_COMPRESSION=true assets:precompile" + execute "rake FOG_PROVIDER=AWS ASSET_SYNC_PREFIX=#{@prefix} ASSET_SYNC_GZIP_COMPRESSION=true assets:precompile" bucket(@prefix).files.size.should == 3 app_js = bucket(@prefix).files.get("#{@prefix}/application.js") diff --git a/spec/integration/rackspace_integration_spec.rb b/spec/integration/rackspace_integration_spec.rb new file mode 100644 index 00000000..331bf53b --- /dev/null +++ b/spec/integration/rackspace_integration_spec.rb @@ -0,0 +1,63 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +def bucket(name) + options = { + :provider => 'rackspace', + :rackspace_username => ENV['RACKSPACE_USERNAME'], + :rackspace_api_key => ENV['RACKSPACE_API_KEY'] + } + + connection = Fog::Storage.new(options) + connection.directories.get(ENV['FOG_DIRECTORY'], :prefix => name) +end + +def execute(command) + app_path = File.expand_path("../../dummy_app", __FILE__) + Dir.chdir app_path + `#{command}` +end + +describe "AssetSync" do + + before(:each) do + @prefix = SecureRandom.hex(6) + end + + after(:each) do + @directory = bucket(@prefix) + @directory.files.each do |f| + f.destroy + end + end + + it "sync" do + execute "rake ASSET_SYNC_PREFIX=#{@prefix} assets:precompile" + bucket(@prefix).files.size.should == 5 + + app_js = bucket(@prefix).files.get("#{@prefix}/application.js") + app_js.content_type.should == "text/javascript" + app_js.origin.should == 'mysite.com' + app_js.access_control_allow_origin.should == '' + + app_js_gz = bucket(@prefix).files.get("#{@prefix}/application.js.gz") + app_js_gz.content_type.should == "text/javascript" + app_js_gz.content_encoding.should == "gzip" + + app_js_gz.content_encoding.should == "gzip" + end + + it "sync with enabled=false" do + execute "rake ASSET_SYNC_PREFIX=#{@prefix} ASSET_SYNC_ENABLED=false assets:precompile" + bucket(@prefix).files.size.should == 0 + end + + it "sync with gzip_compression=true" do + execute "rake ASSET_SYNC_PREFIX=#{@prefix} ASSET_SYNC_GZIP_COMPRESSION=true assets:precompile" + bucket(@prefix).files.size.should == 3 + + app_js = bucket(@prefix).files.get("#{@prefix}/application.js") + app_js.content_type.should == "text/javascript" + end + +end + diff --git a/spec/unit/rackspace_spec.rb b/spec/unit/rackspace_spec.rb index 3f7a6c60..489d423c 100644 --- a/spec/unit/rackspace_spec.rb +++ b/spec/unit/rackspace_spec.rb @@ -14,6 +14,8 @@ config.rackspace_username = 'aaaa' config.rackspace_api_key = 'bbbb' config.existing_remote_files = 'keep' + config.rackspace_origin = 'mysite.com' + config.rackspace_allow_origin = '*' end end @@ -54,6 +56,14 @@ AssetSync.config.rackspace_auth_url.should be_false end + it "should set origin rackspace_origin" do + AssetSync.config.rackspace_origin.should == "mysite.com" + end + + it "should set allow origin rackspace_allow_origin" do + AssetSync.config.rackspace_allow_origin.should == "*" + end + end describe 'using Rackspace from yml' do @@ -86,5 +96,14 @@ it "should configure existing_remote_files" do AssetSync.config.existing_remote_files.should == "keep" end + + it "should set origin rackspace_origin" do + AssetSync.config.rackspace_origin.should == "mysite.com" + end + + it "should set allow origin rackspace_allow_origin" do + AssetSync.config.rackspace_allow_origin.should == "*" + end + end end