Project

General

Profile

Feature #44400 » 0001-Send-PDF-compatible-Illustrator-files-as-application.patch

Go MAEDA, 2026-10-05 10:23

View differences:

app/controllers/attachments_controller.rb
79 79
    end
80 80
    if stale?(:etag => @attachment.digest, :template => false)
81
      # PDF compatible files, such as Illustrator files, must be sent as PDF
82
      # when displayed inline because browsers do not display them otherwise
83
      is_inline_pdf = disposition(@attachment) == 'inline' && @attachment.pdf_previewable?
81 84
      send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
82
                                      :type => detect_content_type(@attachment),
85
                                      :type => is_inline_pdf ? 'application/pdf' : detect_content_type(@attachment),
83 86
                                      :disposition => disposition(@attachment)
84 87
    end
85 88
  end
......
298 301
      content_type =
299 302
        Redmine::MimeType.of(attachment.filename).presence ||
300 303
        "application/octet-stream"
301
    elsif Marcel::Magic.child?(content_type, "application/pdf")
302
      # Send PDF compatible files, such as Illustrator files, as PDF so that
303
      # browsers display them inline
304
      content_type = "application/pdf"
305 304
    end
306 305
    if is_thumb && !content_type.start_with?("image/")
......
315 314
  # Inline disposition can be requested only for PDF compatible files (used
316 315
  # by the PDF preview), to prevent XSS with e.g. HTML or SVG files
317 316
  def disposition(attachment)
318
    if params[:disposition] == 'inline' && detect_content_type(attachment) == 'application/pdf'
317
    if params[:disposition] == 'inline' && attachment.pdf_previewable?
319 318
      'inline'
320 319
    else
321 320
      'attachment'
test/functional/attachments_controller_test.rb
469 469
    assert_select 'div.filecontent.pdf object[type=?]', 'application/pdf'
470 470
  end
471
  def test_download_pdf_compatible_illustrator_file_with_inline_disposition_param_should_be_sent_inline_as_pdf
471
  def test_download_pdf_compatible_illustrator_file_should_be_sent_as_pdf_only_with_inline_disposition_param
472 472
    set_tmp_attachments_directory
473 473
    attachment = Attachment.create!(
474 474
      :file => mock_file_with_options(
......
480 480
      :container => Issue.find(1)
481 481
    )
482
    get(:download, :params => {:id => attachment.id})
483
    assert_response :success
484
    assert_equal 'application/illustrator', @response.media_type
485
    assert_match %r{\Aattachment}, @response.headers['Content-Disposition']
486

  
482 487
    get(:download, :params => {:id => attachment.id, :disposition => 'inline'})
483 488
    assert_response :success
484 489
    assert_equal 'application/pdf', @response.media_type
(4-4/4)