bit better error handling in file/import

This commit is contained in:
Alexey Verkhovsky 2005-01-27 04:14:41 +00:00
parent 06daadc7df
commit 785276efc1

View file

@ -10,7 +10,6 @@ class FileController < ApplicationController
def file def file
check_path check_path
if @params['file'] if @params['file']
# form supplied # form supplied
file_yard.upload_file(@file_name, @params['file']) file_yard.upload_file(@file_name, @params['file'])
@ -48,35 +47,15 @@ class FileController < ApplicationController
def import def import
check_authorization check_authorization
if @params['file'] if @params['file']
logger.info 'Importing pages from a file' @problems = []
import_file_name = "#{@web.address}-import-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.zip" import_file_name = "#{@web.address}-import-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.zip"
file_yard.upload_file(import_file_name, @params['file']) file_yard.upload_file(import_file_name, @params['file'])
zip = Zip::ZipInputStream.open(file_yard.file_path(import_file_name)) import_from_archive(file_yard.file_path(import_file_name))
problems = [] if @problems.empty?
while (entry = zip.get_next_entry) do
ext_length = File.extname(entry.name).length
page_name = entry.name[0..-(ext_length + 1)]
page_content = entry.get_input_stream.read
logger.info "Processing page '#{page_name}'"
begin
if @wiki.read_page(@web.address, page_name)
logger.info "Page '#{page_name}' already exists. Adding a new revision to it."
wiki.revise_page(@web.address, page_name, page_content, Time.now, 'Importer')
else
wiki.write_page(@web.address, page_name, page_content, Time.now, 'Importer')
end
rescue Instiki::ValidationError => e
logger.error(e)
problems << e.message
end
end
logger.info 'Import finished'
if problems.empty?
flash[:info] = 'Import successfully finished' flash[:info] = 'Import successfully finished'
else else
flash[:info] = "Import finished, but some pages were not imported:<li>" + flash[:info] = "Import finished, but some pages were not imported:<li>" +
problems.join('</li><li>') + '</li>' @problems.join('</li><li>') + '</li>'
end end
return_to_last_remembered return_to_last_remembered
else else
@ -105,5 +84,28 @@ class FileController < ApplicationController
def file_yard def file_yard
@wiki.file_yard(@web) @wiki.file_yard(@web)
end end
def import_from_archive(archive)
logger.info "Importing pages from #{archive}"
zip = Zip::ZipInputStream.open(archive)
while (entry = zip.get_next_entry) do
ext_length = File.extname(entry.name).length
page_name = entry.name[0..-(ext_length + 1)]
page_content = entry.get_input_stream.read
logger.info "Processing page '#{page_name}'"
begin
if @wiki.read_page(@web.address, page_name)
logger.info "Page '#{page_name}' already exists. Adding a new revision to it."
wiki.revise_page(@web.address, page_name, page_content, Time.now, @author)
else
wiki.write_page(@web.address, page_name, page_content, Time.now, @author)
end
rescue => e
logger.error(e)
@problems << "#{page_name} : #{e.message}"
end
end
logger.info "Import from #{archive} finished"
end
end end