Module: Cyborg::GZIP

Defined in:
lib/cyborg/command/compress.rb

Constant Summary collapse

ZIP_TYPES =
/\.(?:css|html|js|otf|svg|txt|xml)$/

Instance Method Summary collapse

Instance Method Details

#compress(glob) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cyborg/command/compress.rb', line 5

def compress(glob)
  Dir["#{Cyborg.config[:paths][:output]}/#{glob}"].each do |f|
    next unless f =~ ZIP_TYPES

    mtime = File.mtime(f)
    gz_file = "#{f}.gz"
    next if File.exist?(gz_file) && File.mtime(gz_file) >= mtime

    File.open(gz_file, "wb") do |dest|
      gz = Zlib::GzipWriter.new(dest, Zlib::BEST_COMPRESSION)
      gz.mtime = mtime.to_i
      IO.copy_stream(open(f), gz)
      gz.close
    end

    File.utime(mtime, mtime, gz_file)
  end
end