文/陈刚 转载请保留出处
1.修改fckeditor_controller.rb,把它那几个private方法修改如下:
private
def current_directory_path
base_dir = "#{rails_root}/public"
#todo 在创建用户时,就建立好目录。这时可以去掉这部份代码,提高运行效率。
("#{params[:uploaded]||uploaded}/#{params[:type]}").split('/').each do |s|
next if s==''
base_dir = '/' s
dir.mkdir(base_dir,0775) unless file.exists?(base_dir)
end
check_path("#{base_dir}#{params[:currentfolder]}")
end
def upload_directory_path
uploaded = @request.relative_url_root.to_s"#{params[:uploaded]||uploaded}/#{params[:type]}"
"#{uploaded}#{params[:currentfolder]}"
end
def check_file(file)
# check that the file is a tempfile object
unless "#{file.class}" == "tempfile" || "#{file.class}" == "stringio"
@errornumber = 403
throw exception.new
end
file
end
def check_path(path)
exp_path = file.expand_path path
if exp_path !~ %r[^#{file.expand_path(rails_root)}/public#{params[:uploaded]||uploaded}]
@errornumber = 403
throw exception.new
end
path
end
另外,它前面的常量uploaded_root也没用了,可以删掉。
2. 在上面的代码中params[:uploaded]是关键,它就是我们动态定义的上传目录。该值来自于fckeditor的一些html页面,它是通过get参数传入的。修改browser.html文件(如下粗体部份),在它的url请求中把我们定义目录加入到get参数列中,这样它就可以传到fckeditor_controller.rb里了。
var sserverpath = geturlparam( 'serverpath' ) ;
if ( sserverpath.length > 0 )
oconnector.connectorurl = 'serverpath=' escape( sserverpath ) '&' ;
var suploaded = geturlparam( 'uploaded' ) ;
if ( suploaded.length > 0 )
oconnector.connectorurl = 'uploaded=' escape( suploaded ) '&' ;
oconnector.resourcetype = geturlparam( 'type' ) ;
oconnector.showalltypes = ( oconnector.resourcetype.length == 0 ) ;
3. 上面的geturlparam( 'uploaded' ) 的值来自于fckcustom.js。修改fckcustom.js(如下粗体部份),把uploaded加入到get参数列中。
// change for apps hosted in subdirectory
fckrelativepath = '';
// don't change these
fckconfig.linkbrowserurl = fckconfig.basepath 'filemanager/browser/default/browser.html?connector='fckrelativepath'/fckeditor/command';
fckconfig.imagebrowserurl = fckconfig.basepath 'filemanager/browser/default/browser.html?uploaded='fckconfig.uploaded'&type=image&connector='fckrelativepath'/fckeditor/command';
fckconfig.flashbrowserurl = fckconfig.basepath 'filemanager/browser/default/browser.html?uploaded='fckconfig.uploaded'&type=flash&connector='fckrelativepath'/fckeditor/command';
fckconfig.linkuploadurl = fckrelativepath'/fckeditor/upload';
fckconfig.imageuploadurl = fckrelativepath'/fckeditor/upload?type=image&uploaded='fckconfig.uploaded;
fckconfig.flashuploadurl = fckrelativepath'/fckeditor/upload?type=flash&uploaded='fckconfig.uploaded;
fckconfig.allowquerystringdebug = false;
fckconfig.spellchecker = 'spellerpages';
// only change below here
fckconfig.skinpath = fckconfig.basepath 'skins/silver/';
fckconfig.autodetectlanguage = false ;
fckconfig.defaultlanguage = 'zh-cn' ;
fckconfig.fontnames = '微软雅黑;宋体;黑体;隶书;楷体_gb2312;arial;comic sans ms;courier new;tahoma;times new roman;verdana' ;
fckconfig.toolbarsets["simple"] = [
['source','-','fitwindow','preview','-','templates'],
['pastetext','pasteword'],
['undo','redo','find','replace'],
'/',
['removeformat','bold','italic','underline','strikethrough'],
['orderedlist','unorderedlist','outdent','indent'],
['justifyleft','justifycenter','justifyright','justifyfull'],
['textcolor','bgcolor'],
['link','unlink','anchor'],
['image','flash','table','rule','smiley'],
'/',
['style','fontformat','fontname','fontsize']
] ;
4. 上面fckconfig.uploaded的值来自于fckeditor.rb。在fckeditor.rb中加入一句(如下粗体所示)。
javascript_tag( "var ofckeditor = new fckeditor('#{id}', '#{width}', '#{height}', '#{toolbarset}');\n"
"ofckeditor.basepath = \"#{base_path}\"\n"
"ofckeditor.config['customconfigurationspath'] = '../../fckcustom.js';\n"
"ofckeditor.config['uploaded'] = '#{options[:path]}';\n"
"ofckeditor.replacetextarea();\n")
5.不过上面ofckeditor.config['uploaded']的值要传到fckcustom.js的fckconfig.uploaded里,还需要修改fckeditorcode_gecko.js和fckeditorcode_ie.js(这两个文件对javascript进行了压缩处理,修改起来较难操作)。我是参考了ofckeditor.config['customconfigurationspath'] 这个参数的载入实现,才找到这种鸟不生蛋的地方。搜索这两个文件的关键字customconfigurationspath,找到如下一行,然后加入一个else if判断(如下粗体所示)。
if (d=='customconfigurationspath') fckconfig[d]=e;else if (d=='uploaded') fckconfig[d]=e;else if (e.tolowercase()=="true") this.pageconfig[d]=true;
6.最后在fckeditor.rb里的#{options[:path]}来自于我们前台的view了。如下粗体所示,把标准的fckeditor_textarea新增加了一个参数,其中params[:user_id]是把用户的id值做为目录名。这样就实现了动态改变fckeditor的上传目录。
<%=fckeditor_textarea(:topic, :content, :ajax => true, :toolbarset => 'simple', :height => '400px', :path => "/uploads/#{params[:user_id]}") %>
修改完后需要重启web服务,最后别忘记把public/javascripts/fckeditor和vendor/plugins/fckeditor/public/javascripts同步一下,原因见http://www.blogjava.net/chengang/archive/2007/09/24/147867.html
]]>