Feature #44362 » 0002-Preview-Adobe-Illustrator-files-saved-in-the-PDF-com.patch
| app/controllers/attachments_controller.rb | ||
|---|---|---|
| 296 | 296 |
def detect_content_type(attachment, is_thumb = false) |
| 297 | 297 |
content_type = Redmine::MimeType.infer_type(attachment.content_type, attachment.filename) |
| 298 | 298 | |
| 299 |
if Marcel::Magic.child?(content_type, "application/pdf") |
|
| 300 |
# Send PDF compatible files, such as Illustrator files, as PDF so that |
|
| 301 |
# browsers display them inline |
|
| 302 |
content_type = "application/pdf" |
|
| 303 |
end |
|
| 304 | ||
| 299 | 305 |
if is_thumb && !content_type.start_with?("image/")
|
| 300 | 306 |
# Thumbnails of non-image files are stored in PNG format |
| 301 | 307 |
content_type = "image/png" |
| app/models/attachment.rb | ||
|---|---|---|
| 322 | 322 |
/\.(patch|diff)$/i.match?(filename) |
| 323 | 323 |
end |
| 324 | 324 | |
| 325 |
# Returns true for PDF files, including PDF compatible files such as Adobe |
|
| 326 |
# Illustrator (application/illustrator) |
|
| 325 | 327 |
def is_pdf? |
| 326 |
Redmine::MimeType.of(filename) == "application/pdf" |
|
| 328 |
type = Redmine::MimeType.infer_type(content_type, filename) |
|
| 329 |
!!Marcel::Magic.child?(type, 'application/pdf') |
|
| 327 | 330 |
end |
| 328 | 331 | |
| 329 | 332 |
def is_video? |
| test/functional/attachments_controller_test.rb | ||
|---|---|---|
| 406 | 406 |
assert_match %r{\Aattachment}, @response.headers['Content-Disposition']
|
| 407 | 407 |
end |
| 408 | 408 | |
| 409 |
def test_show_pdf_compatible_illustrator_file_should_be_previewed |
|
| 410 |
set_tmp_attachments_directory |
|
| 411 |
attachment = Attachment.create!( |
|
| 412 |
:file => mock_file_with_options( |
|
| 413 |
:original_filename => 'logo.ai', |
|
| 414 |
:content => File.binread("#{Rails.root}/test/fixtures/files/hello.pdf")
|
|
| 415 |
), |
|
| 416 |
:author_id => 2, |
|
| 417 |
:container => Issue.find(1) |
|
| 418 |
) |
|
| 419 |
assert_equal 'application/illustrator', attachment.content_type |
|
| 420 | ||
| 421 |
get(:show, :params => {:id => attachment.id})
|
|
| 422 |
assert_response :success |
|
| 423 |
assert_select 'div.filecontent.pdf object[type=?]', 'application/pdf' |
|
| 424 |
end |
|
| 425 | ||
| 426 |
def test_download_pdf_compatible_illustrator_file_should_be_sent_inline_as_pdf |
|
| 427 |
set_tmp_attachments_directory |
|
| 428 |
attachment = Attachment.create!( |
|
| 429 |
:file => mock_file_with_options( |
|
| 430 |
:original_filename => 'logo.ai', |
|
| 431 |
:content => File.binread("#{Rails.root}/test/fixtures/files/hello.pdf")
|
|
| 432 |
), |
|
| 433 |
:author_id => 2, |
|
| 434 |
:container => Issue.find(1) |
|
| 435 |
) |
|
| 436 | ||
| 437 |
get(:download, :params => {:id => attachment.id})
|
|
| 438 |
assert_response :success |
|
| 439 |
# Sent as PDF so that browsers display it inline |
|
| 440 |
assert_equal 'application/pdf', @response.media_type |
|
| 441 |
assert_match %r{\Ainline}, @response.headers['Content-Disposition']
|
|
| 442 |
end |
|
| 443 | ||
| 409 | 444 |
def test_download_version_file_with_issue_tracking_disabled |
| 410 | 445 |
Project.find(1).disable_module! :issue_tracking |
| 411 | 446 |
get(:download, :params => {:id => 9})
|
| test/unit/attachment_test.rb | ||
|---|---|---|
| 829 | 829 | |
| 830 | 830 |
def test_is_pdf |
| 831 | 831 |
to_test = {
|
| 832 |
'report.pdf' => true, |
|
| 833 |
# Illustrator files are not PDF files, even though some of them are |
|
| 834 |
# PDF compatible |
|
| 835 |
'logo.ai' => false, |
|
| 832 |
['application/pdf', 'report.pdf'] => true, |
|
| 833 |
# PDF compatible Illustrator files are detected as application/illustrator |
|
| 834 |
['application/illustrator', 'logo.ai'] => true, |
|
| 835 |
# Illustrator files saved in the PostScript format are not PDF files |
|
| 836 |
['application/postscript', 'logo.ai'] => false, |
|
| 837 |
# Attachments whose content type is undetermined fall back to the filename |
|
| 838 |
['application/octet-stream', 'report.pdf'] => true, |
|
| 836 | 839 |
} |
| 837 |
to_test.each do |filename, expected| |
|
| 838 |
assert_equal expected, Attachment.new(:filename => filename).is_pdf?, filename |
|
| 840 |
to_test.each do |(content_type, filename), expected| |
|
| 841 |
attachment = Attachment.new(:content_type => content_type, :filename => filename) |
|
| 842 |
assert_equal expected, attachment.is_pdf?, "#{content_type} (#{filename})"
|
|
| 839 | 843 |
end |
| 840 | 844 |
end |
| 841 | 845 |
end |