Feature #44362 » 0001-Preview-Adobe-Illustrator-files-saved-in-the-PDF-com.patch
| app/controllers/attachments_controller.rb | ||
|---|---|---|
|
render :action => 'diff'
|
||
|
elsif @attachment.is_image?
|
||
|
render :action => 'image'
|
||
|
elsif @attachment.is_pdf?
|
||
|
elsif @attachment.pdf_previewable?
|
||
|
render :action => 'pdf'
|
||
|
elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
|
||
|
@content = File.read(@attachment.diskfile, :mode => "rb")
|
||
| ... | ... | |
|
content_type =
|
||
|
Redmine::MimeType.of(attachment.filename).presence ||
|
||
|
"application/octet-stream"
|
||
|
elsif Marcel::Magic.child?(content_type, "application/pdf")
|
||
|
# Send PDF compatible files, such as Illustrator files, as PDF so that
|
||
|
# browsers display them inline
|
||
|
content_type = "application/pdf"
|
||
|
end
|
||
|
if is_thumb && !content_type.start_with?("image/")
|
||
| app/models/attachment.rb | ||
|---|---|---|
|
Redmine::MimeType.of(filename) == "application/pdf"
|
||
|
end
|
||
|
# Returns true for attachments that can be previewed as PDF files,
|
||
|
# including PDF compatible files such as Adobe Illustrator
|
||
|
# (application/illustrator)
|
||
|
def pdf_previewable?
|
||
|
if content_type.blank? || content_type == 'application/octet-stream'
|
||
|
# The type of attachments created before Redmine started detecting the
|
||
|
# content type from the file contents can only be guessed from the name
|
||
|
is_pdf?
|
||
|
else
|
||
|
!!Marcel::Magic.child?(content_type, 'application/pdf')
|
||
|
end
|
||
|
end
|
||
|
def is_video?
|
||
|
Redmine::MimeType.is_type?('video', filename)
|
||
|
end
|
||
| test/functional/attachments_controller_test.rb | ||
|---|---|---|
|
assert_match %r{\Aattachment}, @response.headers['Content-Disposition']
|
||
|
end
|
||
|
def test_show_pdf_compatible_illustrator_file_should_be_previewed
|
||
|
set_tmp_attachments_directory
|
||
|
attachment = Attachment.create!(
|
||
|
:file => mock_file_with_options(
|
||
|
:original_filename => 'logo.ai',
|
||
|
:content => File.binread("#{Rails.root}/test/fixtures/files/hello.pdf")
|
||
|
),
|
||
|
:author_id => 2,
|
||
|
:container => Issue.find(1)
|
||
|
)
|
||
|
assert_equal 'application/illustrator', attachment.content_type
|
||
|
get(:show, :params => {:id => attachment.id})
|
||
|
assert_response :success
|
||
|
assert_select 'div.filecontent.pdf object[type=?]', 'application/pdf'
|
||
|
end
|
||
|
def test_download_pdf_compatible_illustrator_file_should_be_sent_inline_as_pdf
|
||
|
set_tmp_attachments_directory
|
||
|
attachment = Attachment.create!(
|
||
|
:file => mock_file_with_options(
|
||
|
:original_filename => 'logo.ai',
|
||
|
:content => File.binread("#{Rails.root}/test/fixtures/files/hello.pdf")
|
||
|
),
|
||
|
:author_id => 2,
|
||
|
:container => Issue.find(1)
|
||
|
)
|
||
|
get(:download, :params => {:id => attachment.id})
|
||
|
assert_response :success
|
||
|
# Sent as PDF so that browsers display it inline
|
||
|
assert_equal 'application/pdf', @response.media_type
|
||
|
assert_match %r{\Ainline}, @response.headers['Content-Disposition']
|
||
|
end
|
||
|
def test_download_version_file_with_issue_tracking_disabled
|
||
|
Project.find(1).disable_module! :issue_tracking
|
||
|
get(:download, :params => {:id => 9})
|
||
| test/unit/attachment_test.rb | ||
|---|---|---|
|
assert_equal expected, Attachment.new(:filename => filename).is_pdf?, filename
|
||
|
end
|
||
|
end
|
||
|
def test_pdf_previewable
|
||
|
to_test = {
|
||
|
['application/pdf', 'report.pdf'] => true,
|
||
|
# PDF compatible Illustrator files are detected as application/illustrator
|
||
|
['application/illustrator', 'logo.ai'] => true,
|
||
|
# Illustrator files saved in the PostScript format are not PDF files
|
||
|
['application/postscript', 'logo.ai'] => false,
|
||
|
# The type of attachments created before Redmine started detecting the
|
||
|
# content type from the file contents can only be guessed from the name
|
||
|
['application/octet-stream', 'report.pdf'] => true,
|
||
|
['application/octet-stream', 'logo.ai'] => false,
|
||
|
}
|
||
|
to_test.each do |(content_type, filename), expected|
|
||
|
attachment = Attachment.new(:content_type => content_type, :filename => filename)
|
||
|
assert_equal expected, attachment.pdf_previewable?, "#{content_type} (#{filename})"
|
||
|
end
|
||
|
end
|
||
|
end
|
||