Class: Sprockets::Utils::Gzip
- Inherits:
-
Object
- Object
- Sprockets::Utils::Gzip
- Defined in:
- lib/sprockets/utils/gzip.rb
Constant Summary collapse
- COMPRESSABLE_MIME_TYPES =
What non-text mime types should we compress? This list comes from: www.fastly.com/blog/new-gzip-settings-and-deciding-what-compress
{ "application/vnd.ms-fontobject" => true, "application/x-font-opentype" => true, "application/x-font-ttf" => true, "image/x-icon" => true, "image/svg+xml" => true }
Instance Method Summary collapse
-
#can_compress?(mime_types) ⇒ Boolean
Private: Returns whether or not an asset can be compressed.
-
#cannot_compress?(mime_types) ⇒ Boolean
Private: Opposite of ‘can_compress?`.
-
#compress(target) ⇒ Object
Private: Generates a gzipped file based off of reference asset.
-
#initialize(asset) ⇒ Gzip
constructor
Private: Generates a gzipped file based off of reference file.
Constructor Details
#initialize(asset) ⇒ Gzip
Private: Generates a gzipped file based off of reference file.
5 6 7 8 9 |
# File 'lib/sprockets/utils/gzip.rb', line 5 def initialize(asset) @content_type = asset.content_type @source = asset.source @charset = asset.charset end |
Instance Method Details
#can_compress?(mime_types) ⇒ Boolean
Private: Returns whether or not an asset can be compressed.
We want to compress any file that is text based. You do not want to compress binary files as they may already be compressed and running them through a compression algorithm would make them larger.
Return Boolean.
29 30 31 32 33 34 35 36 |
# File 'lib/sprockets/utils/gzip.rb', line 29 def can_compress?(mime_types) # The "charset" of a mime type is present if the value is # encoded text. We can check this value to see if the asset # can be compressed. # # We also check against our list of non-text compressible mime types @charset || COMPRESSABLE_MIME_TYPES.include?(@content_type) end |
#cannot_compress?(mime_types) ⇒ Boolean
Private: Opposite of ‘can_compress?`.
Returns Boolean.
41 42 43 |
# File 'lib/sprockets/utils/gzip.rb', line 41 def cannot_compress?(mime_types) !can_compress?(mime_types) end |
#compress(target) ⇒ Object
Private: Generates a gzipped file based off of reference asset.
Compresses the target asset’s contents and puts it into a file with the same name plus a ‘.gz` extension in the same folder as the original. Does not modify the target asset.
Returns nothing.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sprockets/utils/gzip.rb', line 52 def compress(target) mtime = PathUtils.stat(target).mtime PathUtils.atomic_write("#{target}.gz") do |f| gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION) gz.mtime = mtime gz.write(@source) gz.close File.utime(mtime, mtime, f.path) end nil end |