Class: Upr::InputWrapper
- Inherits:
-
Struct
- Object
- Struct
- Upr::InputWrapper
- Includes:
- Params
- Defined in:
- lib/upr/input_wrapper.rb
Overview
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#backend ⇒ Object
Returns the value of attribute backend.
-
#content_length ⇒ Object
Returns the value of attribute content_length.
-
#frequency ⇒ Object
Returns the value of attribute frequency.
-
#input ⇒ Object
Returns the value of attribute input.
-
#mtime ⇒ Object
Returns the value of attribute mtime.
-
#path_info ⇒ Object
Returns the value of attribute path_info.
-
#pos ⇒ Object
Returns the value of attribute pos.
-
#seen ⇒ Object
Returns the value of attribute seen.
-
#upload_id ⇒ Object
Returns the value of attribute upload_id.
Instance Method Summary collapse
- #_call(env, uid, length) ⇒ Object
- #_finish ⇒ Object
- #_incr(nr) ⇒ Object
- #call(env) ⇒ Object
- #each(&block) ⇒ Object
- #gets ⇒ Object
-
#initialize(app, options = {}) ⇒ InputWrapper
constructor
A new instance of InputWrapper.
- #read(*args) ⇒ Object
- #rewind ⇒ Object
- #size ⇒ Object
Methods included from Params
Constructor Details
#initialize(app, options = {}) ⇒ InputWrapper
Returns a new instance of InputWrapper.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/upr/input_wrapper.rb', line 14 def initialize(app, = {}) super(app, Array([:path_info] || nil), [:frequency] || 1, [:backend]) # support :drb for compatibility with mongrel_upload_progress if [:drb] backend and raise ArgumentError, ":backend and :drb are incompatible" require 'drb' DRb.start_service self.backend = DRbObject.new(nil, [:drb]) elsif String === backend # allow people to use strings in case their backend gets # lazy-loaded (like an ActiveRecord model) self.backend = eval(backend) else self.backend ||= Upr::Monitor.new end end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app
8 9 10 |
# File 'lib/upr/input_wrapper.rb', line 8 def app @app end |
#backend ⇒ Object
Returns the value of attribute backend
8 9 10 |
# File 'lib/upr/input_wrapper.rb', line 8 def backend @backend end |
#content_length ⇒ Object
Returns the value of attribute content_length
8 9 10 |
# File 'lib/upr/input_wrapper.rb', line 8 def content_length @content_length end |
#frequency ⇒ Object
Returns the value of attribute frequency
8 9 10 |
# File 'lib/upr/input_wrapper.rb', line 8 def frequency @frequency end |
#input ⇒ Object
Returns the value of attribute input
8 9 10 |
# File 'lib/upr/input_wrapper.rb', line 8 def input @input end |
#mtime ⇒ Object
Returns the value of attribute mtime
8 9 10 |
# File 'lib/upr/input_wrapper.rb', line 8 def mtime @mtime end |
#path_info ⇒ Object
Returns the value of attribute path_info
8 9 10 |
# File 'lib/upr/input_wrapper.rb', line 8 def path_info @path_info end |
#pos ⇒ Object
Returns the value of attribute pos
8 9 10 |
# File 'lib/upr/input_wrapper.rb', line 8 def pos @pos end |
#seen ⇒ Object
Returns the value of attribute seen
8 9 10 |
# File 'lib/upr/input_wrapper.rb', line 8 def seen @seen end |
#upload_id ⇒ Object
Returns the value of attribute upload_id
8 9 10 |
# File 'lib/upr/input_wrapper.rb', line 8 def upload_id @upload_id end |
Instance Method Details
#_call(env, uid, length) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/upr/input_wrapper.rb', line 51 def _call(env, uid, length) self.upload_id = uid self.mtime = self.pos = self.seen = 0 self.input = env["rack.input"] env["rack.input"] = self self.content_length = length backend.start(upload_id, length) app.call(env) end |
#_finish ⇒ Object
72 73 74 75 |
# File 'lib/upr/input_wrapper.rb', line 72 def _finish self.seen = backend.finish(upload_id).seen self.content_length ||= self.seen end |
#_incr(nr) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/upr/input_wrapper.rb', line 62 def _incr(nr) self.pos += nr _finish if content_length && pos >= content_length if (nr = pos - seen) > 0 && mtime <= (Time.now.to_i - frequency) backend.incr(upload_id, nr) self.seen = pos self.mtime = Time.now.to_i end end |
#call(env) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/upr/input_wrapper.rb', line 35 def call(env) if path_info.empty? || path_info.include?(env["PATH_INFO"]) # benefit curl users... /\A100-continue\z/i =~ env['HTTP_EXPECT'] and return [ 100, {}, [] ] length = env["CONTENT_LENGTH"] and length = length.to_i chunked = env["TRANSFER_ENCODING"] =~ %r{\Achunked\z}i and length = nil if chunked || (length && length > 0) if uid = extract_upload_id(env) return dup._call(env, uid, length) end end end app.call(env) end |
#each(&block) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/upr/input_wrapper.rb', line 105 def each(&block) input.each do |chunk| # usually just a line _incr(chunk.size) yield chunk end _finish end |
#gets ⇒ Object
93 94 95 96 97 |
# File 'lib/upr/input_wrapper.rb', line 93 def gets rv = input.gets rv.nil? ? _finish : _incr(rv.size) rv end |
#read(*args) ⇒ Object
99 100 101 102 103 |
# File 'lib/upr/input_wrapper.rb', line 99 def read(*args) rv = input.read(*args) rv.nil? || rv.size == 0 ? _finish : _incr(rv.size) rv end |
#rewind ⇒ Object
88 89 90 91 |
# File 'lib/upr/input_wrapper.rb', line 88 def rewind self.pos = 0 input.rewind end |
#size ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/upr/input_wrapper.rb', line 77 def size rv = input.size # we had an unknown length and just had to read in everything to get it if content_length.nil? _incr(rv - seen) _finish end rv end |