From 33fe02794bb9f3429dc3f94cc56e47b498e8e696 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Tue, 12 Apr 2016 10:57:44 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Don=E2=80=99t=20force=20download=20of=20PDF?= =?UTF-8?q?s,=20but=20show=20in=20browser=20preview?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 10 +++++++++- app/controllers/repositories_controller.rb | 10 +++++++++- app/models/attachment.rb | 4 ++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 14025cb..ea45397 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -59,7 +59,7 @@ class AttachmentsController < ApplicationController # images are sent inline send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), :type => detect_content_type(@attachment), - :disposition => (@attachment.image? ? 'inline' : 'attachment') + :disposition => disposition(@attachment) end end @@ -191,4 +191,12 @@ class AttachmentsController < ApplicationController end content_type.to_s end + + def disposition(attachment) + if attachment.is_image? || attachment.is_pdf? + 'inline' + else + 'attachment' + end + end end diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 9c39ad0..c963ee0 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -173,7 +173,7 @@ class RepositoriesController < ApplicationController send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) } send_type = Redmine::MimeType.of(@path) send_opt[:type] = send_type.to_s if send_type - send_opt[:disposition] = (Redmine::MimeType.is_type?('image', @path) ? 'inline' : 'attachment') + send_opt[:disposition] = disposition(@path) send_data @repository.cat(@path, @rev), send_opt else if !@entry.size || @entry.size <= Setting.file_max_size_displayed.to_i.kilobyte @@ -441,4 +441,12 @@ class RepositoriesController < ApplicationController ) graph.burn end + + def disposition(path) + if Redmine::MimeType.is_type?('image', @path) || Redmine::MimeType.of(@path) == "application/pdf" + 'inline' + else + 'attachment' + end + end end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 7d577dc..4bc674f 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -245,6 +245,10 @@ class Attachment < ActiveRecord::Base self.filename =~ /\.(patch|diff)$/i end + def is_pdf? + Redmine::MimeType.of(filename) == "application/pdf" + end + # Returns true if the file is readable def readable? File.readable?(diskfile) -- 2.8.0