Skip to content

Commit

Permalink
Cleaner (?) multiple compression support
Browse files Browse the repository at this point in the history
  • Loading branch information
fengb committed May 15, 2023
1 parent 1e6a576 commit 92ad198
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 93 deletions.
20 changes: 11 additions & 9 deletions lib/asset_sync/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,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 :brotli_compression
attr_accessor :compression
attr_accessor :manifest
attr_accessor :fail_silently
attr_accessor :log_silently
Expand Down Expand Up @@ -95,8 +94,7 @@ def initialize
self.fog_region = nil
self.fog_public = true
self.existing_remote_files = 'keep'
self.gzip_compression = false
self.brotli_compression = false
self.compression = nil
self.manifest = false
self.fail_silently = false
self.log_silently = true
Expand Down Expand Up @@ -125,11 +123,15 @@ def manifest_path
end

def gzip?
self.gzip_compression
self.compression == 'gz'
end

def brotli?
self.brotli_compression
def gzip_compression= bool
if bool
self.compression = 'gz'
else
self.compression = nil
end
end

def existing_remote_files?
Expand Down Expand Up @@ -248,8 +250,8 @@ def load_yml!
self.google_storage_access_key_id = yml["google_storage_access_key_id"] if yml.has_key?("google_storage_access_key_id")
self.google_json_key_string = yml["google_json_key_string"] if yml.has_key?("google_json_key_string")
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.brotli_compression = yml["brotli_compression"] if yml.has_key?("brotli_compression")
self.compression = yml["compression"] if yml.has_key?("compression")
self.compression = 'gz' if yml.has_key?("gzip_compression") and 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.log_silently = yml["log_silently"] if yml.has_key?("log_silently")
Expand Down
4 changes: 2 additions & 2 deletions lib/asset_sync/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class Engine < Rails::Engine
config.enabled = (ENV['ASSET_SYNC_ENABLED'] == 'true') if ENV.has_key?('ASSET_SYNC_ENABLED')

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.brotli_compression = (ENV['ASSET_SYNC_BROTLI_COMPRESSION'] == 'true') if ENV.has_key?('ASSET_SYNC_BROTLI_COMPRESSION')
config.compression = 'gz' if ENV.has_key?('ASSET_SYNC_GZIP_COMPRESSION') and ENV['ASSET_SYNC_GZIP_COMPRESSION'] == 'true'
config.compression = ENV['ASSET_SYNC_COMPRESSION'] if ENV.has_key?('ASSET_SYNC_COMPRESSION')
config.manifest = (ENV['ASSET_SYNC_MANIFEST'] == 'true') if ENV.has_key?('ASSET_SYNC_MANIFEST')
config.prefix = ENV['ASSET_SYNC_PREFIX'] if ENV.has_key?('ASSET_SYNC_PREFIX')
config.include_manifest = (ENV['ASSET_SYNC_INCLUDE_MANIFEST'] == 'true') if ENV.has_key?('ASSET_SYNC_INCLUDE_MANIFEST')
Expand Down
100 changes: 36 additions & 64 deletions lib/asset_sync/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class Storage
REGEXP_FINGERPRINTED_FILES = /\A(.*)\/(.+)-[^\.]+\.([^\.]+)\z/m
REGEXP_ASSETS_TO_CACHE_CONTROL = /-[0-9a-fA-F]{32,}$/

CONTENT_ENCODING = {
'gz' => 'gzip',
'br' => 'br',
}.freeze

class BucketNotFound < StandardError;
end

Expand Down Expand Up @@ -211,8 +216,7 @@ def upload_file(f)
one_year = 31557600
ext = File.extname(f)[1..-1]
mime = MultiMime.lookup(ext)
gzip_file_handle = nil
brotli_file_handle = nil
compressed_file_handle = nil
file_handle = File.open("#{path}/#{f}")
file = {
:key => f,
Expand Down Expand Up @@ -256,75 +260,44 @@ def upload_file(f)
end


gzipped = "#{path}/#{f}.gz"
brotlied = "#{path}/#{f}.br"
ignore = false

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.
log "Ignoring: #{f}"
ignore = true
elsif config.brotli? && File.extname(f) == ".br"
# Don't bother uploading brotli assets if we are in brotli_compression mode
# as we will overwrite file.css with file.css.br if it exists.
log "Ignoring: #{f}"
ignore = true
elsif config.gzip? && File.exist?(gzipped)
original_size = File.size("#{path}/#{f}")
gzipped_size = File.size(gzipped)

if gzipped_size < original_size
percentage = ((gzipped_size.to_f/original_size.to_f)*100).round(2)
gzip_file_handle = File.open(gzipped)
file.merge!({
:key => f,
:body => gzip_file_handle,
:content_encoding => 'gzip'
})
log "Uploading: #{gzipped} in place of #{f} saving #{percentage}%"
else
percentage = ((original_size.to_f/gzipped_size.to_f)*100).round(2)
log "Uploading: #{f} instead of #{gzipped} (compression increases this file by #{percentage}%)"
end
elsif config.brotli? && File.exist?(brotlied)
original_size = File.size("#{path}/#{f}")
brotlied_size = File.size(brotlied)

if brotlied_size < original_size
percentage = ((brotlied_size.to_f/original_size.to_f)*100).round(2)
brotli_file_handle = File.open(brotlied)
file.merge!({
:key => f,
:body => brotli_file_handle,
:content_encoding => 'br'
})
log "Uploading: #{brotlied} in place of #{f} saving #{percentage}%"
else
percentage = ((original_size.to_f/brotlied_size.to_f)*100).round(2)
log "Uploading: #{f} instead of #{brotlied} (compression increases this file by #{percentage}%)"
if config.compression
compressed_name = "#{path}/#{f}.#{config.compression}"

if File.extname(f) == config.compression
# Don't bother uploading compressed assets if we are in compression mode
# as we will overwrite file.css with file.css.gz if it exists.
log "Ignoring: #{f}"
ignore = true
elsif File.exist?(compressed)
original_size = File.size("#{path}/#{f}")
compressed_size = File.size(compressed_name)

if compressed_size < original_size
percentage = ((compressed_size.to_f/original_size.to_f)*100).round(2)
compressed_file_handle = File.open(compressed_name)
file.merge!({
:key => f,
:body => compressed_file_handle,
:content_encoding => CONTENT_ENCODING[config.compression]
})
log "Uploading: #{compressed_name} in place of #{f} saving #{percentage}%"
else
percentage = ((original_size.to_f/compressed_size.to_f)*100).round(2)
log "Uploading: #{f} instead of #{compressed_name} (compression increases this file by #{percentage}%)"
end
end
else
if !config.gzip? && File.extname(f) == ".gz"
# set content encoding for gzipped files this allows cloudfront to properly handle requests with Accept-Encoding
# http://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html
uncompressed_filename = f[0..-4]
ext = File.extname(uncompressed_filename)[1..-1]
mime = MultiMime.lookup(ext)
file.merge!({
:content_type => mime,
:content_encoding => 'gzip'
})
end
if !config.brotli? && File.extname(f) == ".br"
# set content encoding for brotlied files this allows cloudfront to properly handle requests with Accept-Encoding
compressed_encoding = CONTENT_ENCODING[File.extname(f).delete('.')]
if compressed_encoding
# set content encoding for compressed files this allows cloudfront to properly handle requests with Accept-Encoding
# http://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html
uncompressed_filename = f[0..-4]
ext = File.extname(uncompressed_filename)[1..-1]
mime = MultiMime.lookup(ext)
file.merge!({
:content_type => mime,
:content_encoding => 'br'
:content_encoding => compressed_encoding
})
end
log "Uploading: #{f}"
Expand All @@ -338,8 +311,7 @@ def upload_file(f)

bucket.files.create( file ) unless ignore
file_handle.close
gzip_file_handle.close if gzip_file_handle
brotli_file_handle.close if brotli_file_handle
compressed_file_handle.close if compressed_file_handle
end

def upload_files
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/asset_sync_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
expect(AssetSync.config.existing_remote_files).to eq("keep")
end

it "should default gzip_compression to false" do
expect(AssetSync.config.gzip_compression).to be_falsey
it "should default compression to nil" do
expect(AssetSync.config.compression).to be_nil
end

it "should default manifest to false" do
Expand Down Expand Up @@ -139,8 +139,8 @@
expect(AssetSync.config.existing_remote_files).to eq("keep")
end

it "should default gzip_compression to false" do
expect(AssetSync.config.gzip_compression).to be_falsey
it "should default compression to nil" do
expect(AssetSync.config.compression).to be_nil
end

it "should default manifest to false" do
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/azure_rm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
expect(AssetSync.config.existing_remote_files).to eq("keep")
end

it "should default gzip_compression to false" do
expect(AssetSync.config.gzip_compression).to be_falsey
it "should default compression to nil" do
expect(AssetSync.config.compression).to be_nil
end

it "should default manifest to false" do
Expand Down Expand Up @@ -71,8 +71,8 @@
expect(AssetSync.config.existing_remote_files).to eq("keep")
end

it "should default gzip_compression to false" do
expect(AssetSync.config.gzip_compression).to be_falsey
it "should default compression to nil" do
expect(AssetSync.config.compression).to be_nil
end

it "should default manifest to false" do
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/backblaze_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
expect(AssetSync.config.existing_remote_files).to eq("keep")
end

it "should default gzip_compression to false" do
expect(AssetSync.config.gzip_compression).to be_falsey
it "should default compression to nil" do
expect(AssetSync.config.compression).to be_nil
end

it "should default manifest to false" do
Expand Down Expand Up @@ -79,8 +79,8 @@
expect(AssetSync.config.existing_remote_files).to eq("keep")
end

it "should default gzip_compression to false" do
expect(AssetSync.config.gzip_compression).to be_falsey
it "should default compression to nil" do
expect(AssetSync.config.compression).to be_nil
end

it "should default manifest to false" do
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/google_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
expect(AssetSync.config.existing_remote_files).to eq("keep")
end

it "should default gzip_compression to false" do
expect(AssetSync.config.gzip_compression).to be_falsey
it "should default compression to nil" do
expect(AssetSync.config.compression).to be_nil
end

it "should default manifest to false" do
Expand Down Expand Up @@ -174,8 +174,8 @@
expect(AssetSync.config.existing_remote_files).to eq("keep")
end

it "should default gzip_compression to false" do
expect(AssetSync.config.gzip_compression).to be_falsey
it "should default compression to nil" do
expect(AssetSync.config.compression).to be_nil
end

it "should default manifest to false" do
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/railsless_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
expect(AssetSync.config.existing_remote_files).to eq("keep")
end

it "should default gzip_compression to false" do
expect(AssetSync.config.gzip_compression).to be_falsey
it "should default compression to nil" do
expect(AssetSync.config.compression).to be_nil
end

it "should default manifest to false" do
Expand Down

0 comments on commit 92ad198

Please sign in to comment.