From fbb2a355d705b293e5df3b7ef23c436d7540baaf Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sun, 30 Dec 2012 20:36:06 -0800 Subject: [PATCH] Copy binary files instead of rendering through MM. Closes #643. Closes #699 --- CHANGELOG.md | 2 ++ .../lib/middleman-core/cli/build.rb | 20 +++++++++++-------- .../middleman-core/core_extensions/request.rb | 3 +++ .../lib/middleman-core/sitemap/resource.rb | 8 ++++++++ 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df382813..790a281f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ Master === +* Directly send binary files in preview and copy them in build, avoiding reading large binary files into memory for rendering. #643 #699 + 3.0.7 ==== * Turn html5 boilerplate into a layout diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index 6b9dcb5a..ddbb6f1b 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -117,16 +117,20 @@ module Middleman::Cli build_dir = self.class.shared_instance.config[:build_dir] output_file = File.join(build_dir, resource.destination_path) - begin - response = self.class.shared_rack.get(URI.escape(resource.destination_path)) + if resource.binary? + copy_file(resource.source_file, output_file) + else + begin + response = self.class.shared_rack.get(URI.escape(resource.destination_path)) - if response.status == 200 - create_file(output_file, response.body) - else - handle_error(output_file, response.body) + if response.status == 200 + create_file(output_file, response.body) + else + handle_error(output_file, response.body) + end + rescue => e + handle_error(output_file, "#{e}\n#{e.backtrace.join("\n")}", e) end - rescue => e - handle_error(output_file, "#{e}\n#{e.backtrace.join("\n")}", e) end output_file diff --git a/middleman-core/lib/middleman-core/core_extensions/request.rb b/middleman-core/lib/middleman-core/core_extensions/request.rb index 9a104064..dd6c7685 100644 --- a/middleman-core/lib/middleman-core/core_extensions/request.rb +++ b/middleman-core/lib/middleman-core/core_extensions/request.rb @@ -245,6 +245,9 @@ module Middleman # Return 404 if not in sitemap return not_found(res, request_path) unless resource && !resource.ignored? + # If this path is a binary file, send it immediately + return send_file(resource.source_file, env, res) if resource.binary? + current_path = resource.destination_path # Set a HTTP content type based on the request's extensions diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 22ccd2f9..6c14fca5 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -146,6 +146,14 @@ module Middleman end File.join(app.respond_to?(:http_prefix) ? app.http_prefix : '/', url_path) end + + # Whether the source file is binary. + # + # @retrun [Boolean] + def binary? + s = (File.read(source_file, File.stat(source_file).blksize) || "").split(//) + ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30 + end end end end