Defect #29259 » allow-preview-for-any-text-files.diff
app/models/attachment.rb (working copy) | ||
---|---|---|
235 | 235 |
end |
236 | 236 | |
237 | 237 |
def is_text? |
238 |
Redmine::MimeType.is_type?('text', filename) |
|
238 |
if Redmine::MimeType.is_type?('text', filename) |
|
239 |
true |
|
240 |
else |
|
241 |
data = File.read(diskfile, 4096) rescue false |
|
242 |
! Redmine::Utils.binary?(data) |
|
243 |
end |
|
239 | 244 |
end |
240 | 245 | |
241 | 246 |
def is_image? |
lib/redmine/scm/adapters/abstract_adapter.rb (working copy) | ||
---|---|---|
425 | 425 | |
426 | 426 |
module ScmData |
427 | 427 |
def self.binary?(data) |
428 |
unless data.empty? |
|
429 |
data.count( "^ -~", "^\r\n" ).fdiv(data.size) > 0.3 || data.index( "\x00" ) |
|
430 |
end |
|
428 |
Redmine::Utils.binary?(data) |
|
431 | 429 |
end |
432 | 430 |
end |
433 | 431 |
end |
lib/redmine/utils.rb (working copy) | ||
---|---|---|
61 | 61 |
end |
62 | 62 |
end |
63 | 63 |
end |
64 | ||
65 |
def binary?(data) |
|
66 |
unless data.empty? |
|
67 |
data.count( "^ -~", "^\r\n" ).fdiv(data.size) > 0.3 || data.index( "\x00" ) |
|
68 |
end |
|
69 |
end |
|
64 | 70 |
end |
65 | 71 | |
66 | 72 |
module Shell |
test/fixtures/files/hello.go (working copy) | ||
---|---|---|
1 |
package main |
|
2 |
|
|
3 |
import "fmt" |
|
4 |
|
|
5 |
func main() { |
|
6 |
fmt.Println("Hello, world!") |
|
7 |
} |
test/unit/attachment_test.rb (working copy) | ||
---|---|---|
444 | 444 |
puts '(ImageMagick convert not available)' |
445 | 445 |
end |
446 | 446 | |
447 |
def test_is_text |
|
448 |
files = [ |
|
449 |
{:name => 'testfile.txt', :type => 'text/plain'}, |
|
450 |
{:name => 'hello.go', :type => 'text/plain'}, |
|
451 |
{:name => '2006/07/060719210727_archive.zip', :type => 'application/octet-stream'} |
|
452 |
] |
|
453 | ||
454 |
a_text, a_go, a_bin = files.map do |f| |
|
455 |
a = Attachment.new(:container => Issue.find(1), |
|
456 |
:file => uploaded_test_file(f[:name], f[:type]), |
|
457 |
:author => User.find(1)) |
|
458 |
a.save! |
|
459 |
a |
|
460 |
end |
|
461 | ||
462 |
assert a_text.is_text? |
|
463 |
assert a_go.is_text? |
|
464 |
assert_not a_bin.is_text? |
|
465 |
end |
|
466 | ||
447 | 467 |
end |