Sha256: 08423b06cbd64ab906059f4bb0061e7d3b19b4cce69bf1545e981ff3c8e12995

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'mongrel'


class MerbUploadHandler < Mongrel::HttpHandler

  def initialize(options = {})
    @path_info      = Array(options[:path_info])
    @frequency      = options[:frequency] || 3
    @request_notify = true
    if options[:drb]
      require 'drb'
      DRb.start_service
      Mongrel.const_set :Uploads, DRbObject.new(nil, "druby://#{Merb::Server.config[:host]}:#{Merb::Server.config[:session]}").upload_progress
    else
      require File.dirname(__FILE__)+'/merb_upload_progress'
      Mongrel.const_set :Uploads, Merb::UploadProgress.new
    end
    Mongrel::Uploads.debug = true if options[:debug]
  end

  def request_begins(params)
    upload_notify(:add, params, params[Mongrel::Const::CONTENT_LENGTH].to_i)
  end

  def request_progress(params, clen, total)
    upload_notify(:mark, params, clen)
  end

  def process(request, response)
    upload_notify(:finish, request.params)
  end

  private
    def upload_notify(action, params, *args)
      return unless @path_info.include?(params['PATH_INFO']) &&
        params[Mongrel::Const::REQUEST_METHOD] == 'POST' &&
        upload_id = Mongrel::HttpRequest.query_parse(params['QUERY_STRING'])['upload_id']
      if action == :mark
        last_checked_time = Mongrel::Uploads.last_checked(upload_id)
        return unless last_checked_time && Time.now - last_checked_time > @frequency
      end
      Mongrel::Uploads.send(action, upload_id, *args) 
      Mongrel::Uploads.update_checked_time(upload_id) unless action == :finish
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
merb-0.0.8 lib/merb/merb_upload_handler.rb