Added disposition to HTTP headers for sending files
This commit is contained in:
parent
13cb4a1356
commit
ad4c289ec5
2 changed files with 18 additions and 6 deletions
|
@ -51,13 +51,25 @@ class ApplicationController < ActionController::Base
|
|||
'.zip' => 'application/zip'
|
||||
} unless defined? FILE_TYPES
|
||||
|
||||
def content_type_header(file)
|
||||
FILE_TYPES[File.extname(file)] || 'application/octet-stream'
|
||||
DISPOSITION = {
|
||||
'application/octet-stream' => 'attachment',
|
||||
'image/gif' => 'inline',
|
||||
'image/jpeg' => 'inline',
|
||||
'application/pdf' => 'inline',
|
||||
'image/png' => 'inline',
|
||||
'text/plain' => 'inline',
|
||||
'application/zip' => 'attachment'
|
||||
} unless defined? DISPOSITION
|
||||
|
||||
def determine_file_options_for(file_name, original_options = {})
|
||||
original_options[:type] ||= (FILE_TYPES[File.extname(file_name)] or 'application/octet-stream')
|
||||
original_options[:disposition] ||= (DISPOSITION[original_options[:type]] or 'attachment')
|
||||
original_options[:stream] ||= false
|
||||
original_options
|
||||
end
|
||||
|
||||
def send_file(file, options = {})
|
||||
options[:type] = content_type_header(file)
|
||||
options[:stream] = false
|
||||
determine_file_options_for(file, options)
|
||||
super(file, options)
|
||||
end
|
||||
|
||||
|
|
|
@ -19,11 +19,11 @@ class FileController < ApplicationController
|
|||
render
|
||||
end
|
||||
else
|
||||
(render(:status => 404, :text => 'Unspecified file name') and return) unless @file_name
|
||||
render(:status => 404, :text => 'Unspecified file name') and return unless @file_name
|
||||
# no form supplied, this is a request to download the file
|
||||
file = WikiFile.find_by_file_name(@file_name)
|
||||
if file
|
||||
send_data(file.content, :filename => @file_name, :type => content_type_header(@file_name))
|
||||
send_data(file.content, determine_file_options_for(@file_name, :filename => @file_name))
|
||||
else
|
||||
@file = WikiFile.new(:file_name => @file_name)
|
||||
render
|
||||
|
|
Loading…
Reference in a new issue