Extracted storage of files functionality from controller to an object called FileYard.
There is one file yard per web.
This commit is contained in:
parent
0d81292168
commit
1d82582c3b
7 changed files with 112 additions and 54 deletions
|
@ -7,16 +7,20 @@ class FileController < ApplicationController
|
|||
layout 'default'
|
||||
|
||||
def file
|
||||
sanitize_file_name
|
||||
raise Instiki::ValidationError.new("Invalid path: no file name") unless @file_name
|
||||
raise Instiki::ValidationError.new("Invalid path: no web name") unless @web_name
|
||||
raise Instiki::ValidationError.new("Invalid path: unknown web name") unless @web
|
||||
|
||||
file_yard = @wiki.file_yard(@web)
|
||||
if @params['file']
|
||||
# form supplied
|
||||
upload_file
|
||||
file_yard.upload(@file_name, @params['file'])
|
||||
flash[:info] = "File '#{@file_name}' successfully uploaded"
|
||||
return_to_last_remembered
|
||||
elsif have_file?
|
||||
send_file(file_path)
|
||||
elsif file_yard.has_file?(@file_name)
|
||||
send_file(file_yard.file_path(@file_name))
|
||||
else
|
||||
logger.debug("File not found: #{file_path}")
|
||||
logger.debug("File not found: #{file_yard.files_path}/#{@file_name}")
|
||||
# go to the template, which is a file upload form
|
||||
end
|
||||
end
|
||||
|
@ -25,42 +29,4 @@ class FileController < ApplicationController
|
|||
return_to_last_remembered
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def have_file?
|
||||
File.file?(file_path)
|
||||
end
|
||||
|
||||
def upload_file
|
||||
if @params['file'].kind_of?(Tempfile)
|
||||
@params['file'].close
|
||||
FileUtils.mv(@params['file'].path, file_path)
|
||||
elsif @params['file'].kind_of?(IO)
|
||||
File.open(file_path, 'wb') { |f| f.write(@params['file'].read) }
|
||||
else
|
||||
raise 'File to be uploaded is not an IO object'
|
||||
end
|
||||
end
|
||||
|
||||
SANE_FILE_NAME = /[-_\.A-Za-z0-9]{1,255}/
|
||||
|
||||
def sanitize_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, dots, underscores and dashes are accepted.")
|
||||
end
|
||||
end
|
||||
|
||||
def file_area
|
||||
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
|
||||
end
|
||||
|
||||
def file_path
|
||||
"#{file_area}/#{@file_name}"
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue