2005-11-04 06:23:34 +01:00
|
|
|
# Controller responsible for serving files and pictures.
|
2005-04-03 06:22:46 +02:00
|
|
|
|
2006-03-23 08:14:51 +01:00
|
|
|
require 'zip/zip'
|
2007-10-07 19:59:20 +02:00
|
|
|
require 'sanitize'
|
2006-03-23 08:14:51 +01:00
|
|
|
|
2005-01-24 19:52:04 +01:00
|
|
|
class FileController < ApplicationController
|
|
|
|
|
|
|
|
layout 'default'
|
|
|
|
|
|
|
|
before_filter :check_allow_uploads
|
|
|
|
|
|
|
|
def file
|
2006-03-24 08:53:20 +01:00
|
|
|
@file_name = params['id']
|
2007-05-08 00:46:00 +02:00
|
|
|
if params['file']
|
2007-10-07 19:59:20 +02:00
|
|
|
unless (request.post? || ENV["RAILS_ENV"] == "test")
|
|
|
|
headers['Allow'] = 'POST'
|
|
|
|
render(:status => 405, :text => 'You must use an HTTP POST')
|
|
|
|
return
|
|
|
|
end
|
2005-01-24 19:52:04 +01:00
|
|
|
# form supplied
|
2007-05-08 00:46:00 +02:00
|
|
|
new_file = @web.wiki_files.create(params['file'])
|
2005-11-13 14:37:47 +01:00
|
|
|
if new_file.valid?
|
|
|
|
flash[:info] = "File '#{@file_name}' successfully uploaded"
|
|
|
|
return_to_last_remembered
|
|
|
|
else
|
2005-11-14 09:38:37 +01:00
|
|
|
# pass the file with errors back into the form
|
|
|
|
@file = new_file
|
|
|
|
render
|
2005-11-13 14:37:47 +01:00
|
|
|
end
|
2005-11-14 09:38:37 +01:00
|
|
|
else
|
2005-11-13 14:37:47 +01:00
|
|
|
# no form supplied, this is a request to download the file
|
|
|
|
file = WikiFile.find_by_file_name(@file_name)
|
|
|
|
if file
|
2006-03-12 05:53:39 +01:00
|
|
|
send_data(file.content, determine_file_options_for(@file_name, :filename => @file_name))
|
2005-11-14 09:38:37 +01:00
|
|
|
else
|
|
|
|
@file = WikiFile.new(:file_name => @file_name)
|
|
|
|
render
|
2005-11-13 14:37:47 +01:00
|
|
|
end
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cancel_upload
|
|
|
|
return_to_last_remembered
|
|
|
|
end
|
|
|
|
|
2005-01-27 04:55:19 +01:00
|
|
|
def import
|
2007-05-08 00:46:00 +02:00
|
|
|
if params['file']
|
2005-01-27 05:14:41 +01:00
|
|
|
@problems = []
|
2005-01-27 04:55:19 +01:00
|
|
|
import_file_name = "#{@web.address}-import-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.zip"
|
2007-05-08 00:46:00 +02:00
|
|
|
import_from_archive(params['file'].path)
|
2005-01-27 05:14:41 +01:00
|
|
|
if @problems.empty?
|
2005-01-27 04:55:19 +01:00
|
|
|
flash[:info] = 'Import successfully finished'
|
|
|
|
else
|
2005-11-14 09:38:37 +01:00
|
|
|
flash[:error] = 'Import finished, but some pages were not imported:<li>' +
|
2005-01-27 05:14:41 +01:00
|
|
|
@problems.join('</li><li>') + '</li>'
|
2005-01-27 04:55:19 +01:00
|
|
|
end
|
|
|
|
return_to_last_remembered
|
|
|
|
else
|
|
|
|
# to template
|
|
|
|
end
|
|
|
|
end
|
2005-01-24 19:52:04 +01:00
|
|
|
|
|
|
|
protected
|
2005-04-03 06:22:46 +02:00
|
|
|
|
2005-01-24 19:52:04 +01:00
|
|
|
def check_allow_uploads
|
2007-05-08 00:46:00 +02:00
|
|
|
render(:status => 404, :text => "Web #{params['web'].inspect} not found") and return false unless @web
|
2005-11-13 14:37:47 +01:00
|
|
|
if @web.allow_uploads?
|
|
|
|
return true
|
|
|
|
else
|
2007-10-07 19:59:20 +02:00
|
|
|
@hide_navigation = true
|
|
|
|
render(:status => 403, :text => 'File uploads are blocked by the webmaster', :layout => true)
|
2005-01-24 19:52:04 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2005-11-14 09:38:37 +01:00
|
|
|
|
2005-01-24 19:52:04 +01:00
|
|
|
private
|
|
|
|
|
2005-01-27 05:14:41 +01:00
|
|
|
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
|
2007-10-07 19:59:20 +02:00
|
|
|
if !page_content.is_utf8?
|
|
|
|
logger.info "Page '#{page_name}' contains non-utf8 character data. Skipping."
|
|
|
|
next
|
|
|
|
end
|
2005-01-30 05:50:41 +01:00
|
|
|
existing_page = @wiki.read_page(@web.address, page_name)
|
|
|
|
if existing_page
|
|
|
|
if existing_page.content == page_content
|
|
|
|
logger.info "Page '#{page_name}' with the same content already exists. Skipping."
|
|
|
|
next
|
|
|
|
else
|
|
|
|
logger.info "Page '#{page_name}' already exists. Adding a new revision to it."
|
2006-03-23 08:14:51 +01:00
|
|
|
wiki.revise_page(@web.address, page_name, page_content, Time.now, @author, PageRenderer.new)
|
2005-01-30 05:50:41 +01:00
|
|
|
end
|
2005-01-27 05:14:41 +01:00
|
|
|
else
|
2006-03-23 08:14:51 +01:00
|
|
|
wiki.write_page(@web.address, page_name, page_content, Time.now, @author, PageRenderer.new)
|
2005-01-27 05:14:41 +01:00
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
logger.error(e)
|
|
|
|
@problems << "#{page_name} : #{e.message}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
logger.info "Import from #{archive} finished"
|
|
|
|
end
|
|
|
|
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|