From f301f399937f9ff62dc41a9cd8e1ea45b43baa6b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 12 Nov 2009 13:32:56 -0800 Subject: Split out Params and StatusMethods StatusMethods will be reusable in subclasses like the UprStatus ActiveRecord class. Params parsing will be common to both InputWrapper and also to the upcoming Streaming class. --- lib/upr.rb | 1 + lib/upr/input_wrapper.rb | 11 ++++------- lib/upr/params.rb | 23 +++++++++++++++++++++++ lib/upr/status.rb | 11 ++--------- lib/upr/status_methods.rb | 12 ++++++++++++ 5 files changed, 42 insertions(+), 16 deletions(-) create mode 100644 lib/upr/params.rb create mode 100644 lib/upr/status_methods.rb diff --git a/lib/upr.rb b/lib/upr.rb index e1fc4c0..151e3a8 100644 --- a/lib/upr.rb +++ b/lib/upr.rb @@ -8,6 +8,7 @@ module Upr VERSION = '0.1.0' autoload :Monitor, 'upr/monitor' + autoload :Params, 'upr/params' autoload :InputWrapper, 'upr/input_wrapper' # Initializes a new instance of Upr::InputWrapper. Usage in config.ru: diff --git a/lib/upr/input_wrapper.rb b/lib/upr/input_wrapper.rb index 2070d38..5268498 100644 --- a/lib/upr/input_wrapper.rb +++ b/lib/upr/input_wrapper.rb @@ -9,6 +9,8 @@ module Upr :input, :pos, :seen, :content_length, :upload_id, :mtime) + include Params + def initialize(app, options = {}) super(app, Array(options[:path_info] || nil), @@ -38,12 +40,7 @@ module Upr length = env["CONTENT_LENGTH"] and length = length.to_i env["TRANSFER_ENCODING"] =~ %r{\Achunked\z}i and length = nil if length.nil? || length > 0 - req = Rack::Request.new(env) - - # can't blindly parse params here since we don't want to read - # the POST body if there is one, so only parse stuff in the - # query string... - if uid = req.GET["upload_id"] + if uid = extract_upload_id(env) return dup._call(env, uid, length) end end @@ -52,7 +49,7 @@ module Upr end def _call(env, uid, length) - self.upload_id = env["upr.upload_id"] = uid + self.upload_id = uid self.mtime = self.pos = self.seen = 0 self.input = env["rack.input"] env["rack.input"] = self diff --git a/lib/upr/params.rb b/lib/upr/params.rb new file mode 100644 index 0000000..3babdc0 --- /dev/null +++ b/lib/upr/params.rb @@ -0,0 +1,23 @@ +require 'rack' + +module Upr + + module Params + + # we'll add compatibility for existing upload progress modules + # we find here, but under no circumstances will we help + # proliferate new and subtly incompatible mechanisms. + # X-Progress-ID is used in both lighttpd and nginx (3rd party module) + # "upload_id" is used by mongrel_upload_progress + def extract_upload_id(env) + upid = env['HTTP_X_PROGRESS_ID'] and return upid + + # can't blindly parse params here since we don't want to read + # the POST body if there is one, so only parse stuff in the + # query string... + params = Rack::Request.new(env).GET + env["upr.upload_id"] = params["X-Progress-ID"] || params["upload_id"] + end + + end +end diff --git a/lib/upr/status.rb b/lib/upr/status.rb index e3bb356..443933a 100644 --- a/lib/upr/status.rb +++ b/lib/upr/status.rb @@ -1,15 +1,8 @@ +require 'upr/status_methods' module Upr # this is what we store in the Moneta-backed monitor class Status < Struct.new(:seen, :length) - - def error? - seen == -1 - end - - def done? - length && seen >= length - end - + include StatusMethods end end diff --git a/lib/upr/status_methods.rb b/lib/upr/status_methods.rb new file mode 100644 index 0000000..e82a0f0 --- /dev/null +++ b/lib/upr/status_methods.rb @@ -0,0 +1,12 @@ +module Upr + # mixin module for both Upr::Status and UprStatus (AR example module) + module StatusMethods + def error? + seen < 0 + end + + def done? + length && seen >= length + end + end +end -- cgit v1.2.3-24-ge0c7