Index: app/models/attachment.rb =================================================================== --- app/models/attachment.rb (revision 17462) +++ app/models/attachment.rb (working copy) @@ -235,7 +235,12 @@ end def is_text? - Redmine::MimeType.is_type?('text', filename) + if Redmine::MimeType.is_type?('text', filename) + true + else + data = File.read(diskfile, 4096) rescue false + ! Redmine::Utils.binary?(data) + end end def is_image? Index: lib/redmine/scm/adapters/abstract_adapter.rb =================================================================== --- lib/redmine/scm/adapters/abstract_adapter.rb (revision 17462) +++ lib/redmine/scm/adapters/abstract_adapter.rb (working copy) @@ -425,9 +425,7 @@ module ScmData def self.binary?(data) - unless data.empty? - data.count( "^ -~", "^\r\n" ).fdiv(data.size) > 0.3 || data.index( "\x00" ) - end + Redmine::Utils.binary?(data) end end end Index: lib/redmine/utils.rb =================================================================== --- lib/redmine/utils.rb (revision 17462) +++ lib/redmine/utils.rb (working copy) @@ -61,6 +61,12 @@ end end end + + def binary?(data) + unless data.empty? + data.count( "^ -~", "^\r\n" ).fdiv(data.size) > 0.3 || data.index( "\x00" ) + end + end end module Shell Index: test/fixtures/files/hello.go =================================================================== --- test/fixtures/files/hello.go (nonexistent) +++ test/fixtures/files/hello.go (working copy) @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello, world!") +} Index: test/unit/attachment_test.rb =================================================================== --- test/unit/attachment_test.rb (revision 17462) +++ test/unit/attachment_test.rb (working copy) @@ -444,4 +444,24 @@ puts '(ImageMagick convert not available)' end + def test_is_text + files = [ + {:name => 'testfile.txt', :type => 'text/plain'}, + {:name => 'hello.go', :type => 'text/plain'}, + {:name => '2006/07/060719210727_archive.zip', :type => 'application/octet-stream'} + ] + + a_text, a_go, a_bin = files.map do |f| + a = Attachment.new(:container => Issue.find(1), + :file => uploaded_test_file(f[:name], f[:type]), + :author => User.find(1)) + a.save! + a + end + + assert a_text.is_text? + assert a_go.is_text? + assert_not a_bin.is_text? + end + end