diff --git a/lib/asset_sync/config.rb b/lib/asset_sync/config.rb index 7461635..3f30b0d 100644 --- a/lib/asset_sync/config.rb +++ b/lib/asset_sync/config.rb @@ -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 @@ -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 @@ -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? @@ -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") diff --git a/lib/asset_sync/engine.rb b/lib/asset_sync/engine.rb index 28245f5..94292c7 100644 --- a/lib/asset_sync/engine.rb +++ b/lib/asset_sync/engine.rb @@ -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') diff --git a/lib/asset_sync/storage.rb b/lib/asset_sync/storage.rb index da28a49..dc08a81 100644 --- a/lib/asset_sync/storage.rb +++ b/lib/asset_sync/storage.rb @@ -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 @@ -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, @@ -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}" @@ -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 diff --git a/spec/unit/asset_sync_spec.rb b/spec/unit/asset_sync_spec.rb index 273a8d7..9364e48 100644 --- a/spec/unit/asset_sync_spec.rb +++ b/spec/unit/asset_sync_spec.rb @@ -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 @@ -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 diff --git a/spec/unit/azure_rm_spec.rb b/spec/unit/azure_rm_spec.rb index 2c7c4de..a3a0e92 100644 --- a/spec/unit/azure_rm_spec.rb +++ b/spec/unit/azure_rm_spec.rb @@ -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 @@ -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 diff --git a/spec/unit/backblaze_spec.rb b/spec/unit/backblaze_spec.rb index c961eb5..1b71026 100644 --- a/spec/unit/backblaze_spec.rb +++ b/spec/unit/backblaze_spec.rb @@ -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 @@ -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 diff --git a/spec/unit/google_spec.rb b/spec/unit/google_spec.rb index f24f1ea..8f8a89e 100644 --- a/spec/unit/google_spec.rb +++ b/spec/unit/google_spec.rb @@ -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 @@ -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 diff --git a/spec/unit/railsless_spec.rb b/spec/unit/railsless_spec.rb index a27d794..cea69ba 100644 --- a/spec/unit/railsless_spec.rb +++ b/spec/unit/railsless_spec.rb @@ -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