Skip to content

Commit

Permalink
Incorporate changes from Gregg Kellogg
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderpantsGnome committed Feb 7, 2006
1 parent 7ae2306 commit 09b9854
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 186 deletions.
314 changes: 164 additions & 150 deletions trunk/app/controllers/fckeditor_controller.rb
Original file line number Diff line number Diff line change
@@ -1,152 +1,166 @@
class FckeditorController < ApplicationController
# this may not be very Rubyesque, it's my first run at Ruby code
# I'm sure it will get better over time
# feel free to point out better ways to do thigs in Ruby
# if you can do it without being abusive ;)

def index
if RAILS_ENV != 'development'
render(:nothing => true)
end
end

# figure out who needs to handle this request
def connector
if params[:Command] == 'GetFoldersAndFiles' || params[:Command] == 'GetFolders'
get_folders_and_files(params)
elsif params[:Command] == 'CreateFolder'
create_folder(params)
elsif params[:Command] == 'FileUpload'
upload_file(params)
end
end

# generate a directory listing
def get_folders_and_files(params)
params = appendParams(params)
@files = Array.new
@dirs = Array.new
d = Dir.new(params[:dir])
f_count = 0
d_count = 0

d.each { |x|
# skip . and .. I'm sure there's a better way to handle this
if x != '.' && x != '..'
# actual file system path
full_path = "#{params[:dir]}/#{x}";

# found a directory add it to the list
if File.directory?(full_path)
@dirs[d_count] = x
d_count = d_count.next
#if we only want directories, skip the files
elsif params[:Command] == 'GetFoldersAndFiles' && File.file?(full_path)
size = File.size(full_path)

if size != 0 && size < 1024
size = 1
else
size = File.size(full_path) / 1024
end

@files[f_count] = { :name => x, :size => size };
f_count = f_count.next
end
end
}

render(:template => 'fckeditor/files_and_folders', :layout => false)

rescue
render(:template => 'fckeditor/files_and_folders', :layout => false)

end

# create a new directory
def create_folder(params)
params = appendParams(params)
new_dir = params[:dir] + params[:NewFolderName]
d = Dir.mkdir(new_dir)

render(:template => 'fckeditor/create_folder', :layout => false)

rescue Errno::EEXIST
# directory already exists
@error = 101
render(:template => 'fckeditor/create_folder', :layout => false)
rescue Errno::EACCES
# permission denied
@error = 103
render(:template => 'fckeditor/create_folder', :layout => false)
rescue
# any other error
@error = 110
render(:template => 'fckeditor/create_folder', :layout => false)

end

# upload a new file
# currently the FCKeditor docs only allow for 2 errors here
# file renamed and invalid file name
# not sure how to catch invalid file name yet
# I'm thinking there should be a permission denied error as well
def upload_file(params)
params = appendParams(params)
counter = 1
@file_name = params[:NewFile].original_filename
path = nil
ext = nil

# break it up into file and extension
# we need this to check the types and to build new names if necessary
/^(.*)(\..*)$/.match(@file_name)
path = Regexp.last_match(1)
ext = Regexp.last_match(2)

# check to make sure this extension isn't in deny and is in allow
if ! in_array(FCKEDITOR_FILE_TYPES[params[:Type]]['deny'], ext) &&
in_array(FCKEDITOR_FILE_TYPES[params[:Type]]['allow'], ext)

while File.exist?("#{params[:dir]}#{@file_name}")
@file_name = "#{path}(#{counter})#{ext}"
params[:error] = 201
counter = counter.next
end

File.open("#{params[:dir]}#{@file_name}", 'w') { |f| f.write(params[:NewFile].read) }
else
# invalid file type
params[:error] = 202
end

render(:template => 'fckeditor/upload_file', :layout => false)
end

# helper to setup pathing info that is common to all methods
# is there is a more Rubyesque way to do this?
# somebody let me know
def appendParams(params)
params[:error] = 0
params[:base_dir] = FCKEDITOR_UPLOADS || 'UserFiles'
params[:url] = "#{params[:base_dir]}#{params[:Type]}#{params[:CurrentFolder]}".gsub(/\/+/, '/')
params[:dir] = "#{RAILS_ROOT}/public#{params[:url]}"

return params
end

# helper to see if a value exists in an array
# I'm sure there is a more Rubyesque way to do this
# somebody let me know
def in_array(haystack, needle)
if haystack != nil && needle != nil
haystack.each { |val|
if val == needle
return true
end
}
end

return false
end
# this may not be very Rubyesque, it's my first run at Ruby code
# I'm sure it will get better over time
# feel free to point out better ways to do thigs in Ruby
# if you can do it without being abusive ;)
before_filter :setup, :except => 'index'

def index
if RAILS_ENV != 'development'
render(:nothing => true)
end
end

# figure out who needs to handle this request
def connector
if @params[:Command] == 'GetFoldersAndFiles' || @params[:Command] == 'GetFolders'
get_folders_and_files(@params)
elsif @params[:Command] == 'CreateFolder'
create_folder(params)
elsif @params[:Command] == 'FileUpload'
upload_file(@params)
end
end

private
def setup
defaultParams
status = 200

# Check parameters for correctness
if (@params[:Command] !~ /^(GetFolders|GetFoldersAndFiles|CreateFolder|FileUpload)$/)
render :text => "Command parameter malformed: \"#{@params[:Command]}\"", status => 405
elsif (@params[:Type] !~ /^(File|Image|Flash|Media)$/)
render :text => "Type parameter malformed: \"#{@params[:Type]}\"", status => 405
elsif (@params[:CurrentFolder] !~ /^\/(.+\/)*$/)
render :text => "CurrentFolder parameter malformed: \"#{@params[:CurrentFolder]}\"", status => 405
else
# Make sure necessary directories exist
unless File.directory?(@params[:ServerPath])
Dir.mkdir(@params[:ServerPath])
end
@dir = "#{params[:ServerPath]}#{params[:Type]}#{params[:CurrentFolder]}"
@url = "/UserFiles/#{params[:Type]}#{params[:CurrentFolder]}"
@error = 0
end
end

# generate a directory listing
def get_folders_and_files(params)
@files = Array.new
@dirs = Array.new

d = Dir.new(@dir)

d.each { |x|
# skip . and .. I'm sure there's a better way to handle this
if x != '.' && x != '..'
# actual file system path
full_path = "#{@dir}/#{x}";

# found a directory add it to the list
if File.directory?(full_path)
@dirs << x
#if we only want directories, skip the files
elsif params[:Command] == 'GetFoldersAndFiles' && File.file?(full_path)
size = File.size(full_path)

if size != 0 && size < 1024
size = 1
else
size = File.size(full_path) / 1024
end

@files << { :name => x, :size => size };
end
end
}

render(:template => 'fckeditor/files_and_folders', :layout => false)

rescue
render(:template => 'fckeditor/files_and_folders', :layout => false)

end

# create a new directory
def create_folder(params)
new_dir = @dir + params[:NewFolderName]
d = Dir.mkdir(new_dir)

render(:template => 'fckeditor/create_folder', :layout => false)

rescue Errno::EEXIST
# directory already exists
@error = 101
render(:template => 'fckeditor/create_folder', :layout => false)
rescue Errno::EACCES
# permission denied
@error = 103
render(:template => 'fckeditor/create_folder', :layout => false)
rescue
# any other error
@error = 110
render(:template => 'fckeditor/create_folder', :layout => false)

end

# upload a new file
# currently the FCKeditor docs only allow for 2 errors here
# file renamed and invalid file name
# not sure how to catch invalid file name yet
# I'm thinking there should be a permission denied error as well
def upload_file(params)
counter = 1
@file_name = params[:NewFile].original_filename

# break it up into file and extension
# we need this to check the types and to build new names if necessary
ext = File.extname(@file_name)
path = File.basename(@file_name, ext)

# check to make sure this extension isn't in deny and is in allow
if ! in_array(FCKEDITOR_FILE_TYPES[params[:Type]]['deny'], ext)
in_array(FCKEDITOR_FILE_TYPES[params[:Type]]['allow'], ext)
while File.exist?("#{@dir}#{@file_name}")
@file_name = "#{path}(#{counter})#{ext}"
@error = 201
counter = counter.next
end

n = File.open("#{@dir}#{@file_name}", 'wb') { |f| f.write(params[:NewFile].read) }
else
# invalid file type
@error = 202
end

render(:template => 'fckeditor/upload_file', :layout => false)
end

# helper to setup pathing info that is common to all methods
def defaultParams
if RAILS_ENV == 'production' or !@params.has_key?(:ServerPath)
# Allow destination directory to be overridden, otherwise it must be that which is configured
@params[:ServerPath] = "#{RAILS_ROOT}/public/UserFiles/"
end

# Set defaults
@params[:Type] = "Image" unless @params.has_key?(:Type)
@params[:CurrentFolder] = "/" unless @params.has_key?(:CurrentFolder)
end

# helper to see if a value exists in an array
# I'm sure there is a more Rubyesque way to do this
# somebody let me know
def in_array(haystack, needle)
if haystack != nil && needle != nil
haystack.each { |val|
if val == needle
return true
end
}
end

return false
end
end
22 changes: 11 additions & 11 deletions trunk/app/views/fckeditor/files_and_folders.rxml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
xml.instruct!

xml.Connector( "command" => params[:Command], "resourceType" => params[:Type] ) do
xml.CurrentFolder( "path" => params[:CurrentFolder], "url" => params[:url] )
xml.CurrentFolder( "path" => params[:CurrentFolder], "url" => @url )

xml.Folders do
@dirs.each do |dir|
xml.Folder("name" => dir)
end
end
xml.Folders do
@dirs.each do |dir|
xml.Folder("name" => dir)
end
end

xml.Files do
@files.each do |file|
xml.File("name" => file[:name], "size" => file[:size] )
end
end
xml.Files do
@files.each do |file|
xml.File("name" => file[:name], "size" => file[:size] )
end
end
end

29 changes: 8 additions & 21 deletions trunk/app/views/fckeditor/index.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,19 @@
<body>

<%=
fckeditor( :object, :value, {
:width => '600px',
:height => '500px',

# only need this if it's different than the global
# or the default file browser, currently only default is supported
#:browser => 'mcpuk',

# don't need to pass this, it should be set in environment.rb
# in fact passing this in is currenty ignored
#:base_path => "#{FCKEDITOR_BASE}",

# don't need to pass this, it should be set in environment.rb
# in fact passing this in is currenty ignored
#:user_files_dir => "#{FCKEDITOR_UPLOADS}",

:custom_config => '/javascripts/fckconfig_custom.js'
}
)
fckeditor( :object, :value, {
:width => '600px',
:height => '500px',
:custom_config => '/javascripts/fckconfig_custom.js'
}
)
-%>

<p>
Use these to debug
<form target=_blank" action="/fckeditor/connector?Command=FileUpload&Type=Image&CurrentFolder=/" method="post" enctype="multipart/form-data">
<input type="file" name="NewFile">
<input type="submit">
<input type="file" name="NewFile">
<input type="submit">
</form>
</p>

Expand Down
2 changes: 1 addition & 1 deletion trunk/app/views/fckeditor/upload_file.rhtml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<%= javascript_tag("window.parent.frames['frmUpload'].OnUploadCompleted(#{params[:error]}, '#{@file_name}');") -%>
<%= javascript_tag("window.parent.frames['frmUpload'].OnUploadCompleted(#{@error}, '#{@file_name}');") -%>

Loading

0 comments on commit 09b9854

Please sign in to comment.