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'
|
'.zip' => 'application/zip'
|
||||||
} unless defined? FILE_TYPES
|
} unless defined? FILE_TYPES
|
||||||
|
|
||||||
def content_type_header(file)
|
DISPOSITION = {
|
||||||
FILE_TYPES[File.extname(file)] || 'application/octet-stream'
|
'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
|
end
|
||||||
|
|
||||||
def send_file(file, options = {})
|
def send_file(file, options = {})
|
||||||
options[:type] = content_type_header(file)
|
determine_file_options_for(file, options)
|
||||||
options[:stream] = false
|
|
||||||
super(file, options)
|
super(file, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -19,11 +19,11 @@ class FileController < ApplicationController
|
||||||
render
|
render
|
||||||
end
|
end
|
||||||
else
|
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
|
# no form supplied, this is a request to download the file
|
||||||
file = WikiFile.find_by_file_name(@file_name)
|
file = WikiFile.find_by_file_name(@file_name)
|
||||||
if file
|
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
|
else
|
||||||
@file = WikiFile.new(:file_name => @file_name)
|
@file = WikiFile.new(:file_name => @file_name)
|
||||||
render
|
render
|
||||||
|
|
Loading…
Add table
Reference in a new issue