[BREAKS BUILD] Some work on File uploads, half-done, committing as a backup
This commit is contained in:
parent
a61c11854d
commit
8bdee631f6
13 changed files with 308 additions and 277 deletions
|
@ -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
|
||||
|
|
|
@ -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
57
app/models/wiki_file.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue