From 83c8f9a2ce526a113db18074378f78ef71000b22 Mon Sep 17 00:00:00 2001 From: Egor Date: Sat, 2 Feb 2013 00:26:58 +0400 Subject: [PATCH] Fix huge files regression (for undetected mime) Note that binary_bytes is an array of bytes that never never appears in text. Array taken from encoding.c source from 'file' utility (http://www.darwinsys.com/file/). --- middleman-core/lib/middleman-core/util.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index 46854994..fad57ca3 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -24,7 +24,14 @@ module Middleman ext = ".#{ext}" unless ext.to_s[0] == ?. mime = ::Rack::Mime.mime_type(ext, nil) - return false unless mime + unless mime + binary_bytes = [0, 1, 2, 3, 4, 5, 6, 11, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31] + s = File.read(filename, File.stat(filename).blksize) || '' + s.each_byte do |c| + return true if binary_bytes.include?(c) + end + return false + end return false if mime.start_with?('text/') return false if mime.include?('xml') return false if mime.include?('json')