ckeditor_controller.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:6k
源码类别:

Ajax

开发平台:

Others

  1. require 'fileutils'
  2. require 'tmpdir'
  3. class CkeditorController < ActionController::Base
  4.   UPLOAD_FOLDER = "/files"
  5.   UPLOAD_ROOT = RAILS_ROOT + "/public" + UPLOAD_FOLDER
  6.   MIME_TYPES = [
  7.     "image/jpg",
  8.     "image/jpeg",
  9.     "image/pjpeg",
  10.     "image/gif",
  11.     "image/png",
  12.     "application/x-shockwave-flash"
  13.   ]
  14.   ##############################################################################
  15.   # XML Response string
  16.   #
  17.   RXML = <<-EOL
  18.   xml.instruct!
  19.     #=> <?xml version="1.0" encoding="utf-8" ?>
  20.   xml.Connector("command" => params[:command], "resourceType" => 'File') do
  21.     xml.CurrentFolder("url" => @ck_url, "path" => params[:currentFolder])
  22.     xml.Folders do
  23.       @folders.each do |folder|
  24.         xml.Folder("name" => folder)
  25.       end
  26.     end if !@folders.nil?
  27.     xml.Files do
  28.       @files.keys.sort.each do |f|
  29.         xml.File("name" => f, "size" => @files[f])
  30.       end
  31.     end if !@files.nil?
  32.     xml.Error("number" => @errorNumber) if !@errorNumber.nil?
  33.   end
  34.   EOL
  35.   ##############################################################################
  36.   # figure out who needs to handle this request
  37.   #
  38.   def command
  39.     if params[:command] == 'CheckAuthentication'
  40.       return true
  41.     end
  42.     if params[:command] == 'GetFoldersAndFiles' || params[:command] == 'GetFolders'
  43.       get_folders_and_files
  44.     elsif params[:command] == 'CreateFolder'
  45.       create_folder
  46.     elsif params[:command] == 'FileUpload'
  47.       upload_file
  48.     end
  49.     render :inline => RXML, :type => :rxml unless params[:command] == 'FileUpload'
  50.   end
  51.   def get_folders_and_files(include_files = true)
  52.     @folders = Array.new
  53.     @files = {}
  54.     begin
  55.       @ck_url = upload_directory_path
  56.       @current_folder = current_directory_path
  57.       Dir.entries(@current_folder).each do |entry|
  58.         next if entry =~ /^./
  59.         path = @current_folder + entry
  60.         @folders.push entry if FileTest.directory?(path)
  61.         @files[entry] = (File.size(path) / 1024) if (include_files and FileTest.file?(path))
  62.       end
  63.     rescue => e
  64.       @errorNumber = 110 if @errorNumber.nil?
  65.     end
  66.   end
  67.   def create_folder
  68.     begin
  69.       @ck_url = current_directory_path
  70.       path = @ck_url + params[:newFolderName]
  71.       if !(File.stat(@ck_url).writable?)
  72.         @errorNumber = 103
  73.       elsif params[:newFolderName] !~ /[wds]+/
  74.         @errorNumber = 102
  75.       elsif FileTest.exists?(path)
  76.         @errorNumber = 101
  77.       else
  78.         Dir.mkdir(path,0775)
  79.         @errorNumber = 0
  80.       end
  81.     rescue => e
  82.       @errorNumber = 110 if @errorNumber.nil?
  83.     end
  84.   end
  85.   def upload_file
  86.     begin      
  87.       load_file_from_params
  88.       Resource.create(:filename => @new_file.original_filename, :mime => @ftype, :created_at => Time.now)    
  89.       copy_tmp_file(@new_file) if mime_types_ok(@ftype)
  90.     rescue => e
  91.       @errorNumber = 110 if @errorNumber.nil?
  92.     end
  93.     render :text => %Q'
  94.     <html>
  95.       <body>
  96.         <script type="text/javascript">
  97.           window.parent.CKEDITOR.tools.callFunction(1, "#{url_for(:controller => "/articles").gsub(%r{/$},'')}/#{uploaded_file_path}");
  98.         </script>
  99.       </body>
  100.     </html>'
  101.   end
  102.   def upload
  103.     self.upload_file
  104.   end
  105.   #################################################################################
  106.   #
  107.   private
  108.   def load_file_from_params
  109.     @new_file = check_file(params[:upload])
  110.     @ck_url  = upload_directory_path
  111.     @ftype     = @new_file.content_type.strip
  112.     log_upload
  113.   end
  114.   ##############################################################################
  115.   # Chek if mime type is included in the MIME_TYPES
  116.   #
  117.   def mime_types_ok(ftype)
  118.     mime_type_ok = MIME_TYPES.include?(ftype) ? true : false
  119.     if mime_type_ok
  120.       @errorNumber = 0
  121.     else
  122.       @errorNumber = 202
  123.       raise_mime_type_and_show_msg(ftype)
  124.     end
  125.     mime_type_ok
  126.   end
  127.   ##############################################################################
  128.   # Raise and exception, log the msg error and show msg
  129.   #
  130.   def raise_mime_type_and_show_msg(ftype)
  131.     msg = "#{ftype} is invalid MIME type"
  132.     puts msg;
  133.     raise msg;
  134.     log msg
  135.   end
  136.   ##############################################################################
  137.   # Copy tmp file to current_directory_path/tmp_file.original_filename
  138.   #
  139.   def copy_tmp_file(tmp_file)
  140.     path = current_directory_path + "/" + tmp_file.original_filename
  141.     File.open(path, "wb", 0664) do |fp|
  142.       FileUtils.copy_stream(tmp_file, fp)
  143.     end
  144.   end
  145.   ##############################################################################
  146.   # Puts a messgae info in the current log, only if RAILS_ENV is 'development'
  147.   #
  148.   def log(str)
  149.     RAILS_DEFAULT_LOGGER.info str if RAILS_ENV == 'development'
  150.   end
  151.   ##############################################################################
  152.   # Puts some data in the current log
  153.   #
  154.   def log_upload
  155.     log "CKEDITOR - #{params[:upload]}"
  156.     log "CKEDITOR - UPLOAD_FOLDER: #{UPLOAD_FOLDER}"
  157.     log "CKEDITOR - #{File.expand_path(RAILS_ROOT)}/public#{UPLOAD_FOLDER}/" +
  158.         "#{@new_file.original_filename}"
  159.   end
  160.   ##############################################################################
  161.   # Returns the filesystem folder with the current folder
  162.   #
  163.   def current_directory_path
  164.     base_dir = "#{UPLOAD_ROOT}/#{params[:type]}"
  165.     Dir.mkdir(base_dir,0775) unless File.exists?(base_dir)
  166.     check_path("#{base_dir}#{params[:currentFolder]}")
  167.   end
  168.   ##############################################################################
  169.   # Returns the upload url folder with the current folder
  170.   #
  171.   def upload_directory_path
  172.     url_root = ::ActionController::Base.relative_url_root.to_s
  173.     uploaded = url_root + "#{UPLOAD_FOLDER}/#{params[:Type]}"
  174.     "#{uploaded}#{params[:currentFolder]}"
  175.   end
  176.   ##############################################################################
  177.   # Current uploaded file path
  178.   #
  179.   def uploaded_file_path
  180.     "#{upload_directory_path}#{@new_file.original_filename}"
  181.   end
  182.   ##############################################################################
  183.   # check that the file is a tempfile object
  184.   #
  185.   def check_file(file)
  186.     log "CKEDITOR ---- CLASS OF UPLOAD OBJECT: #{file.class}"
  187.     unless "#{file.class}" == "Tempfile" || "StringIO"
  188.       @errorNumber = 403
  189.       throw Exception.new
  190.     end
  191.     file
  192.   end
  193.   def check_path(path)
  194.     exp_path = File.expand_path path
  195.     if exp_path !~ %r[^#{File.expand_path(UPLOAD_ROOT)}]
  196.       @errorNumber = 403
  197.       throw Exception.new
  198.     end
  199.     path
  200.   end
  201. end