Skip to content

Commit

Permalink
Gzip option to compress files before S3 deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Aug 17, 2013
1 parent 6eff6d8 commit 1684fca
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/awestruct/deploy/s3_deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,57 @@ class S3Deploy < Base
def initialize( site_config, deploy_config )
@site_path = site_config.output_dir
@bucket = deploy_config['bucket']
@gzip = deploy_config['gzip']
end

def publish_site
compress_site
$LOG.info "Syncing #{@site_path} to bucket #{@bucket}" if $LOG.info?
s3cmd = RubyS3Cmd::S3Cmd.new
s3cmd.sync("#{@site_path}/", @bucket)
$LOG.info "DONE" if $LOG.info?
end

def compress_site
if @gzip
require 'zlib'
Dir.glob("#{@site_path}/**/*") do |item|
next if item == '.' or item == '..'
ext = File.extname(item)
if !ext.empty?
ext_sym = ext[1..-1].to_sym
case ext_sym
when :css, :js, :html
gzip_file item
end
end
end
end
end

def gzip_file( filename )
if !is_gzipped filename
$LOG.debug "Gzipping File #{filename}"
Zlib::GzipWriter.open("#{filename}.gz") do |gz|
gz.mtime = File.mtime(filename)
gz.orig_name = filename
gz.write IO.binread(filename)
end
File.rename("#{filename}.gz", "#{filename}")
end
end

def is_gzipped( filename )
begin
File.open("#{filename}") do |f|
Zlib::GzipReader.new(f)
true
end
rescue
false
end
end

end
end
end
Expand Down

0 comments on commit 1684fca

Please sign in to comment.