rumblelabs/asset_sync — Issue #14: Automagic asset compression
Author: stouset
Created: 2011-10-23 23:15:07+00:00
State: open

I've written a monkeypatch for `AssetSync::Storage#upload_file` that automagically replaces assets with gzipped versions. You may want to consider implementing its functionality (although perhaps in a user-configurable manner).

```ruby
# automagically compress assets
if defined?(AssetSync)
gem 'asset_sync', '= 0.1.8' # die early if asset_sync may have changed

class AssetSync::Storage
def upload_file(f)
STDERR.puts "Uploading: #{f}"

gzip = File.exist?("#{path}/#{f}.gz") # enable gzip compression if a gz asset exists
name = gzip ? f + '.gz' : f # use the gzipped asset's contents
mime = Mime::Type.lookup_by_extension File.extname(f)[1..-1] # detect MIME type

file = bucket.files.create(
:key => "#{f}",
:body => File.open("#{path}/#{name}"),
:public => true,
:cache_control => "max-age=31557600",
:content_type => mime, # explicitly provide content-type for assets AWS isn't aware of
:content_encoding => gzip ? 'gzip' : nil # gzipped-assets require Content-Encoding header
)
end
end
end
```