Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional support for CORS requests for assets on rackspace #149

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 22 additions & 11 deletions lib/asset_sync/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ 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
attr_accessor :enabled
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'
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -121,23 +128,27 @@ 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")
self.gzip_compression = yml["gzip_compression"] if yml.has_key?("gzip_compression")
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")
Expand Down
19 changes: 9 additions & 10 deletions lib/asset_sync/multi_mime.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 24 additions & 5 deletions lib/asset_sync/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/asset_sync/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AssetSync
VERSION = "0.5.4"
VERSION = "0.5.5"
end
2 changes: 2 additions & 0 deletions spec/fixtures/rackspace_with_yml/config/asset_sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions spec/integration/aws_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
63 changes: 63 additions & 0 deletions spec/integration/rackspace_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -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

19 changes: 19 additions & 0 deletions spec/unit/rackspace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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