enable smush cache
This commit is contained in:
parent
54c01fe32d
commit
81a8192d14
|
@ -1,58 +1,35 @@
|
||||||
require 'json'
|
require "smusher"
|
||||||
require 'open-uri'
|
require "middleman/builder"
|
||||||
|
|
||||||
begin
|
|
||||||
require 'httpclient'
|
|
||||||
rescue LoadError
|
|
||||||
puts "httpclient not available. Install it with: gem install httpclient"
|
|
||||||
end
|
|
||||||
|
|
||||||
module Middleman
|
module Middleman
|
||||||
module SmushPngs
|
class Builder
|
||||||
def self.included(base)
|
alias_method :pre_smush_after_run, :after_run
|
||||||
base.supported_formats << "png"
|
def after_run
|
||||||
end
|
pre_smush_after_run
|
||||||
|
smush_dir = File.join(Middleman::Base.build_dir, Middleman::Base.images_dir)
|
||||||
|
|
||||||
def render_path(file)
|
# Read cache
|
||||||
if File.extname(file) == ".png"
|
cache_file = File.join(Middleman::Base.root, ".smush-cache")
|
||||||
file = File.join(options.public, file)
|
cache_data = if File.exists?(cache_file)
|
||||||
optimized = optimized_image_data_for(file)
|
Marshal.restore(File.read(cache_file))
|
||||||
|
|
||||||
begin
|
|
||||||
raise "Error: got larger" if size(file) < optimized.size
|
|
||||||
raise "Error: empty file downloaded" if optimized.size < 20
|
|
||||||
|
|
||||||
optimized
|
|
||||||
rescue
|
|
||||||
File.read(file)
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
super
|
{}
|
||||||
|
end
|
||||||
|
|
||||||
|
::Smusher.class_eval do
|
||||||
|
images_in_folder(smush_dir).each do |file|
|
||||||
|
original_file_size = size(file)
|
||||||
|
return if original_file_size.zero?
|
||||||
|
return if cache_data[file] && cache_data[file] == original_file_size
|
||||||
|
|
||||||
|
with_logging(file, true) do
|
||||||
|
write_optimized_data(file)
|
||||||
|
cache_data[file] = size(file) # Add or update cache
|
||||||
|
File.open(cache_file, "w") { |f| f.write Marshal.dump(cache_data) } # Write cache
|
||||||
|
say "<%= color('#{"[SMUSHED]".rjust(12)}', :yellow) %> " + file.gsub(Middleman::Base.build_dir+"/", '')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
|
||||||
def size(file)
|
|
||||||
File.exist?(file) ? File.size(file) : 0
|
|
||||||
end
|
|
||||||
|
|
||||||
def optimized_image_data_for(file)
|
|
||||||
# I leave these urls here, just in case it stops working again...
|
|
||||||
# url = "http://smush.it/ws.php" # original, redirects to somewhere else..
|
|
||||||
url = 'http://ws1.adq.ac4.yahoo.com/ysmush.it/ws.php'
|
|
||||||
# url = "http://developer.yahoo.com/yslow/smushit/ws.php" # official but does not work
|
|
||||||
# url = "http://smushit.com/ysmush.it/ws.php" # used at the new page but does not hande uploads
|
|
||||||
# url = "http://smushit.eperf.vip.ac4.yahoo.com/ysmush.it/ws.php" # used at the new page but does not hande uploads
|
|
||||||
response = HTTPClient.post url, { 'files[]' => File.new(file) }
|
|
||||||
response = JSON.parse(response.body.content)
|
|
||||||
raise "smush.it: #{response['error']}" if response['error']
|
|
||||||
image_url = response['dest']
|
|
||||||
raise "no dest path found" unless image_url
|
|
||||||
open(image_url) { |source| source.read() }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Base
|
|
||||||
include Middleman::SmushPngs
|
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -14,12 +14,13 @@ end
|
||||||
|
|
||||||
class Templater::Actions::Template
|
class Templater::Actions::Template
|
||||||
def render
|
def render
|
||||||
|
@@rack_test ||= Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base))
|
||||||
|
|
||||||
@render_cache ||= begin
|
@render_cache ||= begin
|
||||||
# The default render just requests the page over Rack and writes the response
|
# The default render just requests the page over Rack and writes the response
|
||||||
request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "")
|
request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "")
|
||||||
browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base))
|
@@rack_test.get(request_path)
|
||||||
browser.get(request_path)
|
@@rack_test.last_response.body
|
||||||
browser.last_response.body
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in a new issue