Changeset 692

Show
Ignore:
Timestamp:
09/25/07 14:38:16 (1 year ago)
Author:
e.@brainspl.at
Message:

make upload handler more flexible, move config into merb.yml, closes #208

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app_generators/merb/templates/config/merb.yml

    r650 r692  
    2424:session_secret_key: <%= key %> 
    2525 
    26 # Uncomment to use the merb upload progress 
    27 #:config: config/upload.conf 
     26# Uncomment to use the merb upload progress. The 'path match' will be treated as 
     27# a regex for any URLs that should be considered for upload monitoring. 
     28#:upload_path_match: /files/\d 
     29#:upload_frequency: 3 
    2830 
    2931# Uncomment to cache templates in dev mode. Templates are cached 
  • trunk/app_generators/merb/templates/config/upload.conf

    r453 r692  
    1 # upload progress conf 
    2 ---  
    3 #:drb: true # uncomment for drb UploadProgress if you run a merb cluster 
    4 :path_info: /files/upload 
    5 :frequency: 2 
  • trunk/lib/merb/server.rb

    r691 r692  
    516516        end 
    517517        mconfig = Mongrel::Configurator.new(mconf_hash) do 
    518           yconfig = Erubis.load_yaml_file(@@merb_opts[:config]) if @@merb_opts[:config] 
    519518          listener do 
    520             uri( "/", :handler => MerbUploadHandler.new(yconfig), :in_front => true) if @@merb_opts[:config
     519            uri( "/", :handler => MerbUploadHandler.new(@@merb_opts), :in_front => true) if @@merb_opts[:upload_path_match
    521520            uri "/", :handler => MerbHandler.new(@@merb_opts[:merb_root]+'/public') 
    522521            uri "/favicon.ico", :handler => Mongrel::Error404Handler.new("")  
  • trunk/lib/merb/upload_handler.rb

    r525 r692  
    2929 
    3030  def initialize(options = {}) 
    31     @path_info      = Array(options[:path_info]) 
    32     @frequency      = options[:frequency] || 3 
     31    @path_match     = Regexp.new(options[:upload_path_match]) 
     32    @frequency      = options[:upload_frequency] || 3 
    3333    @request_notify = true 
    34     if options[:drb] 
     34    if options[:start_drb] 
    3535      require 'drb' 
    3636      DRb.start_service 
    37       Mongrel.const_set :Uploads, DRbObject.new(nil, "druby://#{Merb::Server.config[:host]}:#{Merb::Server.config[:drb_server_port]}").upload_progress 
     37      Mongrel.const_set :Uploads, DRbObject.new(nil, "druby://#{options[:host]}:#{options[:drb_server_port]}").upload_progress 
    3838    else 
    3939      Mongrel.const_set :Uploads, Merb::UploadProgress.new 
     
    7272     
    7373    def valid_upload?(params) 
    74       @path_info.any? { |p| params[Mongrel::Const::PATH_INFO].include?(p) } && 
     74      params[Mongrel::Const::PATH_INFO].match(@path_match) && 
    7575        [Mongrel::Const::POST, Mongrel::Const::PUT].include?(params[Mongrel::Const::REQUEST_METHOD]) && 
    7676        Mongrel::HttpRequest.query_parse(params[Mongrel::Const::QUERY_STRING])[Mongrel::Const::UPLOAD_ID] 
  • trunk/spec/merb/upload_handler_spec.rb

    r684 r692  
    1111    @progress.stub!(:last_checked).and_return Time.now - 10 
    1212    Merb::UploadProgress.stub!(:new).and_return(@progress) 
    13     @handler   = MerbUploadHandler.new(:frequency => 3, :path_info => '/files/') 
     13    @handler   = MerbUploadHandler.new(:upload_frequency => 3, :upload_path_match => '^/files/\d+') 
    1414    @upload_id = '880b7835-8a67-400e-a8f1-dec18691d604' 
    1515    @params    = { 
    1616      'CONTENT_LENGTH' => "10", 
    17       'PATH_INFO' => '/files/', 
     17      'PATH_INFO' => '/files/23', 
    1818      'REQUEST_METHOD' => 'POST', 
    1919      'QUERY_STRING' => "upload_id=#{@upload_id}" 
     
    2828      remove_const :Uploads 
    2929    end 
     30  end 
     31   
     32  it "should consider requests as invalid if the path regex does not match" do 
     33    @handler.send(:valid_upload?, { 
     34      'CONTENT_LENGTH' => "10", 
     35      'PATH_INFO' => '/spoons/', 
     36      'REQUEST_METHOD' => 'POST', 
     37      'QUERY_STRING' => "upload_id=#{@upload_id}" 
     38    }).should == nil 
    3039  end 
    3140