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

gzip files before upload, fix header setting #310

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions asset_sync.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
$:.push File.expand_path("../lib", __FILE__)

require "asset_sync/version"
require "zlib"

Gem::Specification.new do |s|
s.name = "asset_sync"
Expand Down
2 changes: 2 additions & 0 deletions lib/asset_sync/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Invalid < StandardError; end
# AssetSync
attr_accessor :existing_remote_files # What to do with your existing remote files? (keep or delete)
attr_accessor :gzip_compression
attr_accessor :extensions_to_gzip
attr_accessor :manifest
attr_accessor :fail_silently
attr_accessor :log_silently
Expand Down Expand Up @@ -50,6 +51,7 @@ def initialize
self.fog_region = nil
self.existing_remote_files = 'keep'
self.gzip_compression = false
self.extensions_to_gzip = 'css,js'
self.manifest = false
self.fail_silently = false
self.log_silently = true
Expand Down
1 change: 1 addition & 0 deletions lib/asset_sync/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Engine < Rails::Engine
config.existing_remote_files = ENV['ASSET_SYNC_EXISTING_REMOTE_FILES'] || "keep"

config.gzip_compression = (ENV['ASSET_SYNC_GZIP_COMPRESSION'] == 'true') if ENV.has_key?('ASSET_SYNC_GZIP_COMPRESSION')
config.extensions_to_gzip = ENV['ASSET_SYNC_EXTENSIONS_TO_GZIP'] if ENV.has_key?('ASSET_SYNC_EXTENSIONS_TO_GZIP')
config.manifest = (ENV['ASSET_SYNC_MANIFEST'] == 'true') if ENV.has_key?('ASSET_SYNC_MANIFEST')
end

Expand Down
26 changes: 24 additions & 2 deletions lib/asset_sync/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_local_files
elsif File.exist?(self.config.manifest_path)
log "Using: Manifest #{self.config.manifest_path}"
yml = YAML.load(IO.read(self.config.manifest_path))

return yml.map do |original, compiled|
# Upload font originals and compiled
if original =~ /^.+(eot|svg|ttf|woff)$/
Expand Down Expand Up @@ -137,7 +137,9 @@ def upload_file(f)
:content_type => mime
}

if /-[0-9a-fA-F]{32}$/.match(File.basename(f,File.extname(f)))
uncompressed_filename = f.sub(/\.gz\z/, '')
basename = File.basename(uncompressed_filename, File.extname(uncompressed_filename))
if /-[0-9a-fA-F]{32,64}$/.match(basename)
file.merge!({
:cache_control => "public, max-age=#{one_year}",
:expires => CGI.rfc1123_date(Time.now + one_year)
Expand Down Expand Up @@ -207,6 +209,25 @@ def upload_file(f)
file = bucket.files.create( file ) unless ignore
end

def compress_files
extensions_to_gzip = Regexp.union(*config.extensions_to_gzip.split(','))
self.local_files.each do |file|
if File.extname(file)[1..-1] =~ extensions_to_gzip
file_path = File.join(path, file)
if File.file?(file_path)
gz_path = file_path + ".gz"
log "Writing #{gz_path}"
Zlib::GzipWriter.open(gz_path, Zlib::BEST_COMPRESSION) do |gz|
gz.mtime = File.mtime(file_path)
gz.orig_name = file_path
gz.write(IO.binread(file_path))
end
end
end
end
@local_files = nil # break memoization to include new .gz files
end

def upload_files
# get a fresh list of remote files
remote_files = ignore_existing_remote_files? ? [] : get_remote_files
Expand All @@ -231,6 +252,7 @@ def upload_files
def sync
# fixes: https://github.com/rumblelabs/asset_sync/issues/19
log "AssetSync: Syncing."
compress_files if config.gzip?
upload_files
delete_extra_remote_files unless keep_existing_remote_files?
log "AssetSync: Done."
Expand Down
11 changes: 10 additions & 1 deletion spec/dummy_app/app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
console.log("hello");
var ipsum = "Lorem ipsum dolor sit amet, mel lobortis volutpat reformidans eu. Neglegentur mediocritatem pri eu, eu per ignota probatus. Vis facilisi posidonium et, id eum numquam sapientem. Ignota minimum id per, rebum persius nominati sed id. \
\
Ad mea simul perfecto patrioque. Id sumo etiam evertitur per, iisque lucilius ne mei. In pro mentitum deserunt. Per cu nibh nominavi, ne pro velit ponderum verterem, dolor perpetua sit eu. \
\
Ei nec sanctus vivendo. Saepe partiendo vix ne. Stet labitur ut his, ei tibique vivendum quo. His iuvaret qualisque ex, at rebum fierent prodesset eos. Sit enim soluta et. \
\
Eripuit abhorreant efficiantur at his. Ex duo putent aliquip adipisci. Ea albucius vivendum mel, ex alienum omittantur vim. Illum ridens utroque at usu, vix at adhuc simul, elit possim oblique sit id. Posse postea gubergren eum at, qui in wisi option splendide, liber assentior disputando ei vix. An quo clita definiebas, sed possit pericula cu, tamquam accusata at vim."

console.log(ipsum);

18 changes: 10 additions & 8 deletions spec/integration/aws_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def bucket(name)

def execute(command)
app_path = File.expand_path("../../dummy_app", __FILE__)
Dir.chdir app_path
`#{command}`
Dir.chdir app_path do
puts `#{command}`
end
end

describe "AssetSync" do
Expand Down Expand Up @@ -47,14 +48,10 @@ def execute(command)
files = bucket(@prefix).files

app_js_path = files.select{ |f| f.key =~ app_js_regex }.first
app_js_gz_path = files.select{ |f| f.key =~ app_js_gz_regex }.first

app_js = files.get( app_js_path.key )
expect(app_js.content_type).to eq("text/javascript")

app_js_gz = files.get( app_js_gz_path.key )
expect(app_js_gz.content_type).to eq("text/javascript")
expect(app_js_gz.content_encoding).to eq("gzip")
expect(app_js.cache_control).to match("public")
end

it "sync with enabled=false" do
Expand All @@ -64,11 +61,16 @@ def execute(command)

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
expect(bucket(@prefix).files.size).to eq(1)

app_js_path = files.select{ |f| f.key =~ app_js_regex }.first

expect(app_js_path).not_to be_nil
app_js = files.get( app_js_path.key )
expect(app_js.content_type).to eq("text/javascript")
expect(app_js.content_encoding).to eq("gzip")
expect(app_js.cache_control).to match("public")
expect(app_js.key).to match(/\.js$/)
end

end
Expand Down
26 changes: 24 additions & 2 deletions spec/unit/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@


it 'should correctly set expire date' do
local_files = ['file1.jpg', 'file1-1234567890abcdef1234567890abcdef.jpg']
local_files += ['dir1/dir2/file2.jpg', 'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg']
local_files = ['file1.jpg', 'file1-1234567890abcdef1234567890abcdef.jpg', 'file1-1234567890abcdef1234567890abcdef.jpg.gz']
local_files += ['dir1/dir2/file2.jpg', 'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg', 'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg.gz']
remote_files = []
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(local_files)
Expand All @@ -99,7 +99,9 @@ def check_file(file)
when 'dir1/dir2/file2.jpg'
!expect(file).not_to include(:cache_control, :expires)
when 'file1-1234567890abcdef1234567890abcdef.jpg'
when 'file1-1234567890abcdef1234567890abcdef.jpg.gz'
when 'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg'
when 'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg.gz'
expect(file).to include(:cache_control, :expires)
else
fail
Expand Down Expand Up @@ -191,4 +193,24 @@ def check_file(file)
#Object.send(:remove_const, :MIME) if defined?(MIME)
end
end

describe '#compress_files' do
before(:each) do
@config = AssetSync::Config.new
@config.public_path = 'public'
end

it 'should compress text files' do
local_files = ['jquery.js', 'file1.jpg', 'application.css', 'logo.png', 'dir']
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(local_files)
allow(File).to receive(:file?).and_return(true)
allow(File).to receive(:open).and_return(nil)

expect(Zlib::GzipWriter).to receive(:open).with('public/application.css.gz', anything).and_return(nil)
expect(Zlib::GzipWriter).to receive(:open).with('public/jquery.js.gz', anything).and_return(nil)
storage.compress_files
end

end
end