Actions that send files to browser smartly determine content-type HTTP header by the file name extnsion
This commit is contained in:
parent
e9a419c40f
commit
c30989c7eb
6 changed files with 62 additions and 16 deletions
|
@ -4,18 +4,19 @@ require 'instiki_errors'
|
|||
|
||||
class FileController < ApplicationController
|
||||
|
||||
layout 'default', :except => [:rss_feed, :rss_with_headlines, :tex, :export_tex, :export_html]
|
||||
layout 'default'
|
||||
|
||||
def file
|
||||
if have_file?
|
||||
send_file(file_path)
|
||||
else
|
||||
render_text 'form'
|
||||
logger.debug("File not found: #{file_path}")
|
||||
# to template, which is a file upload form
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
|
||||
def have_file?
|
||||
sanitize_file_name
|
||||
File.file?(file_path)
|
||||
|
@ -24,7 +25,7 @@ class FileController < ApplicationController
|
|||
SANE_FILE_NAME = /[-_A-Za-z0-9]{1,255}/
|
||||
|
||||
def sanitize_file_name
|
||||
raise Instiki::ValidationError.new("Invalid path") unless @file_name
|
||||
raise Instiki::ValidationError.new("Invalid path: no file name") unless @file_name
|
||||
unless @file_name =~ SANE_FILE_NAME
|
||||
raise ValidationError.new("Invalid file name: '#{@file_name}'.\n" +
|
||||
"Only latin characters, digits, underscores and dashes are accepted.")
|
||||
|
@ -32,7 +33,7 @@ class FileController < ApplicationController
|
|||
end
|
||||
|
||||
def file_area
|
||||
raise Instiki::ValidationError.new("Invalid path") unless @web_name
|
||||
raise Instiki::ValidationError.new("Invalid path: no web name") unless @web_name
|
||||
file_area = File.expand_path("#{@wiki.storage_path}/#{@web_name}")
|
||||
FileUtils.mkdir_p(file_area) unless File.directory?(file_area)
|
||||
file_area
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue