Defect #29259 » allow-preview-for-any-text-files.diff
| app/models/attachment.rb (working copy) | ||
|---|---|---|
|
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?
|
||
| lib/redmine/scm/adapters/abstract_adapter.rb (working copy) | ||
|---|---|---|
|
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
|
||
| lib/redmine/utils.rb (working copy) | ||
|---|---|---|
|
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
|
||
| test/fixtures/files/hello.go (working copy) | ||
|---|---|---|
|
package main
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
func main() {
|
||
|
fmt.Println("Hello, world!")
|
||
|
}
|
||
| test/unit/attachment_test.rb (working copy) | ||
|---|---|---|
|
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
|
||