Class: CGI::ProgressIO

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/action_controller/cgi_ext/multipart_progress.rb

Overview

:nodoc:

Constant Summary collapse

MIN_SAVE_INTERVAL =

Number of seconds between session saves

1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(orig_io, progress, session) ⇒ ProgressIO

Returns a new instance of ProgressIO.



57
58
59
60
61
62
63
64
65
66
# File 'lib/action_controller/cgi_ext/multipart_progress.rb', line 57

def initialize(orig_io, progress, session)
  @session = session
  @progress = progress
  
  @start_time = Time.now
  @last_save_time = @start_time
  save_progress
  
  super(orig_io)
end

Instance Attribute Details

#progressObject (readonly)

Returns the value of attribute progress.



55
56
57
# File 'lib/action_controller/cgi_ext/multipart_progress.rb', line 55

def progress
  @progress
end

#sessionObject (readonly)

Returns the value of attribute session.



55
56
57
# File 'lib/action_controller/cgi_ext/multipart_progress.rb', line 55

def session
  @session
end

Instance Method Details

#finishObject



94
95
96
97
# File 'lib/action_controller/cgi_ext/multipart_progress.rb', line 94

def finish
  @session.update
  ActionController::Base.logger.debug("Finished processing multipart upload in #{@progress.elapsed_seconds.to_s}s")
end

#read(*args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/action_controller/cgi_ext/multipart_progress.rb', line 68

def read(*args)
  data = __getobj__.read(*args)

  if data and data.size > 0
    now = Time.now
    elapsed =  now - @start_time
    progress.update!(data.size, elapsed)

    if now - @last_save_time > MIN_SAVE_INTERVAL
      save_progress 
      @last_save_time = now 
    end
  else
    ActionController::Base.logger.debug("CGI::ProgressIO#read returns nothing when it should return nil if IO is finished: [#{args.inspect}], a cancelled upload or old FCGI bindings.  Resetting the upload progress")

    progress.reset!
    save_progress
  end

  data
end

#save_progressObject



90
91
92
# File 'lib/action_controller/cgi_ext/multipart_progress.rb', line 90

def save_progress
  @session.update 
end