Sha256: 7d0753d83e4c5efd4419e2e11029e634e2bda0fba09b9bea2baf7f4767538ba7

Contents?: true

Size: 1.42 KB

Versions: 8

Compression:

Stored size: 1.42 KB

Contents

require 'zlib'
require 'condenser/utils'

# Generates a `.gz` file using the zlib algorithm built into
# Ruby's standard library.
class Condenser::ZlibWriter

  # What mime types should we compress? This list comes from:
  # https://www.fastly.com/blog/new-gzip-settings-and-deciding-what-compress
  COMPRESSALBE_TYPES = %w( text/html application/x-javascript text/css
    application/javascript text/javascript text/plain text/xml
    application/json application/vnd.ms-fontobject application/x-font-opentype
    application/x-font-truetype application/x-font-ttf application/xml font/eot
    font/opentype font/otf image/svg+xml image/vnd.microsoft.icon image/x-icon)

  ADDED_MIME_TYPES = ['application/gzip']
  
  attr_reader :mime_types, :added_mime_types
  
  def initialize(mime_types: nil, added_mime_types: nil)
    @mime_types = mime_types || COMPRESSALBE_TYPES
    @added_mime_types = added_mime_types || ADDED_MIME_TYPES
  end
  
  def path(asset)
    "#{asset.path}.gz"
  end
  
  def exist?(asset)
    ::File.exist?(path(asset))
  end

  def call(output_directory, asset)
    filename = File.join(output_directory, "#{asset.path}.gz")
    FileUtils.mkdir_p(File.dirname(filename))
    Condenser::Utils.atomic_write(filename) do |file|
      gz = Zlib::GzipWriter.new(file, Zlib::BEST_COMPRESSION)
      gz.write(asset.source)
      gz.close
      # File.utime(mtime, mtime, file.path)
    end
    
    ["#{asset.filename}.gz"]
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
condenser-0.3 lib/condenser/writers/zlib_writer.rb
condenser-0.2 lib/condenser/writers/zlib_writer.rb
condenser-0.1 lib/condenser/writers/zlib_writer.rb
condenser-0.0.12 lib/condenser/writers/zlib_writer.rb
condenser-0.0.11 lib/condenser/writers/zlib_writer.rb
condenser-0.0.10 lib/condenser/writers/zlib_writer.rb
condenser-0.0.9 lib/condenser/writers/zlib_writer.rb
condenser-0.0.8 lib/condenser/writers/zlib_writer.rb