[BREAKS BUILD] Some work on File uploads, half-done, committing as a backup

This commit is contained in:
Alexey Verkhovsky 2005-11-13 13:37:47 +00:00
parent a61c11854d
commit 8bdee631f6
13 changed files with 308 additions and 277 deletions

View file

@ -32,7 +32,7 @@ class ApplicationController < ActionController::Base
@web_name = @params['web']
@wiki = wiki
if @web_name
@web = @wiki.webs[@web_name]
@web = @wiki.webs[@web_name]
if @web.nil?
render :status => 404, :text => "Unknown web '#{@web_name}'"
return false
@ -53,8 +53,12 @@ class ApplicationController < ActionController::Base
'.zip' => 'application/zip'
} unless defined? FILE_TYPES
def content_type_header(file)
FILE_TYPES[File.extname(file)] || 'application/octet-stream'
end
def send_file(file, options = {})
options[:type] ||= (FILE_TYPES[File.extname(file)] || 'application/octet-stream')
options[:type] = content_type_header(file)
options[:stream] = false
super(file, options)
end

View file

@ -7,41 +7,32 @@ class FileController < ApplicationController
before_filter :check_allow_uploads
def file
check_path
if @params['file']
# form supplied
file_yard.upload_file(@file_name, @params['file'])
flash[:info] = "File '#{@file_name}' successfully uploaded"
return_to_last_remembered
elsif file_yard.has_file?(@file_name)
send_file(file_yard.file_path(@file_name))
else
logger.debug("File not found: #{file_yard.files_path}/#{@file_name}")
# go to the template, which is a file upload form
new_file = upload_file(@file_name, @params['file'])
if new_file.valid?
flash[:info] = "File '#{@file_name}' successfully uploaded"
return_to_last_remembered
else
# FIXME handle validation errors more gracefully
flash[:errors] = new_file.errors.to_s
end
else
# no form supplied, this is a request to download the file
file = WikiFile.find_by_file_name(@file_name)
if file
send_data(file.content, :filename => @file_name, :type => content_type_header(@file_name))
end
end
# if it's neither a supplied form for upload, nor a request for a known file,
# display the file/file.rhtml template (which happens to be an upload form)
end
def cancel_upload
return_to_last_remembered
end
def pic
check_path
if @params['file']
# form supplied
file_yard.upload_file(@file_name, @params['file'])
flash[:info] = "Image '#{@file_name}' successfully uploaded"
return_to_last_remembered
elsif file_yard.has_file?(@file_name)
send_file(file_yard.file_path(@file_name))
else
logger.debug("Image not found: #{file_yard.files_path}/#{@file_name}")
render_action 'file'
end
end
def import
check_authorization
if @params['file']
@problems = []
import_file_name = "#{@web.address}-import-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.zip"
@ -62,25 +53,16 @@ class FileController < ApplicationController
protected
def check_allow_uploads
unless @web.allow_uploads?
if @web.allow_uploads?
return true
else
render :status => 403, :text => 'File uploads are blocked by the webmaster'
return false
end
end
private
def check_path
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
end
def file_yard
@wiki.file_yard(@web)
end
def import_from_archive(archive)
logger.info "Importing pages from #{archive}"
zip = Zip::ZipInputStream.open(archive)

View file

@ -1,5 +1,6 @@
class Web < ActiveRecord::Base
has_many :pages
has_many :wiki_files
def wiki
Wiki.new
@ -40,8 +41,8 @@ class Web < ActiveRecord::Base
Page.count(['web_id = ? AND name = ?', id, name]) > 0
end
def has_file?(name)
wiki.file_yard(self).has_file?(name)
def has_file?(file_name)
WikiFile.find_by_file_name(file_name) != nil
end
def markup
@ -97,6 +98,7 @@ class Web < ActiveRecord::Base
protected
before_save :sanitize_markup
after_save :create_files_directory
before_validation :validate_address
validates_uniqueness_of :address
validates_length_of :color, :in => 3..6
@ -111,4 +113,33 @@ class Web < ActiveRecord::Base
raise Instiki::ValidationError.new("#{self.class.human_attribute_name('address')} #{errors.on(:address)}")
end
end
def create_files_directory
return unless allow_uploads == 1
dummy_file = self.wiki_files.build(:file_name => '0', :description => '0', :content => '0')
dir = File.dirname(dummy_file.content_path)
begin
require 'fileutils'
FileUtils.mkdir_p dir
dummy_file.save
dummy_file.destroy
rescue => e
logger.error("Failed create files directory for #{self.address}: #{e}")
raise "Instiki could not create directory to store uploaded files. " +
"Please make sure that Instiki is allowed to create directory " +
"#{File.expand_path(dir)} and add files to it."
end
end
def default_web?
defined? DEFAULT_WEB and self.address == DEFAULT_WEB
end
def files_path
if default_web?
"#{RAILS_ROOT}/public/#{self.address}/files"
else
"#{RAILS_ROOT}/public/files"
end
end
end

View file

@ -21,10 +21,6 @@ class Wiki
end
end
def file_yard(web)
web.file_yard
end
def edit_web(old_address, new_address, name, markup, color, additional_style, safe_mode = false,
password = nil, published = false, brackets_only = false, count_pages = false,
allow_uploads = true, max_upload_size = nil)

57
app/models/wiki_file.rb Normal file
View file

@ -0,0 +1,57 @@
class WikiFile < ActiveRecord::Base
belongs_to :web
before_save :write_content_to_file
before_destroy :delete_content_file
validates_presence_of %w( web file_name description )
validates_length_of :file_name, :within=>1..50
validates_length_of :description, :within=>1..255
def self.find_by_file_name(file_name)
find(:first, :conditions => ['file_name = ?', file_name])
end
SANE_FILE_NAME = /^[a-zA-Z0-9\-_\. ]*$/
def validate
if file_name
if file_name !~ SANE_FILE_NAME
errors.add("file_name", "is invalid. Only latin characters, digits, dots, underscores, " +
"dashes and spaces are accepted")
elsif file_name == '.' or file_name == '..'
errors.add("file_name", "cannot be '.' or '..'")
end
end
if @web and @content
if (@content.size > @web.max_upload_size.kilobytes)
errors.add("content", "size (#{(@content.size / 1024.0).round} kilobytes) exceeds " +
"the maximum (#{web.max_upload_size} kilobytes) set for this wiki")
end
end
errors.add("content", "is empty") if @content.nil? or @content.empty?
end
def content=(content)
@content = content
end
def content
@content ||= ( File.open(content_path, 'rb') { |f| f.read } )
end
def content_path
web.files_path + '/' + file_name
end
def write_content_to_file
File.open(self.content_path, 'wb') { |f| f.write(@content) }
end
def delete_content_file
require 'fileutils'
FileUtils.rm_f(content_path) if File.exists?(content_path)
end
end