THIS IS AN EXPERIMENTAL FEATURE
Which means that it doesn’t yet work on all systems. We‘re still working on full compatibility. It’s thus not advised to use this unless you’ve verified it to work fully on all the systems that is a part of your environment. Consider this an extended preview.
Upload Progress abstracts the progress of an upload. It’s used by the multipart progress IO that keeps track of the upload progress and creating the application depends on. It contians methods to update the progress during an upload and read the statistics such as received_bytes, total_bytes, completed_percent, bitrate, and remaining_seconds
You can get the current Progress object by calling upload_progress instance method in your controller or view.
- bitrate
- completed_percent
- elapsed_seconds
- finished?
- new
- remaining_bytes
- remaining_seconds
- reset!
- stalled?
- started?
| MIN_SAMPLE_TIME | = | 0.150 |
| Number of seconds between bitrate samples. Updates that occur more frequently than MIN_SAMPLE_TIME will not be queued until this time passes. This behavior gives a good balance of accuracy and load for both fast and slow transfers. | ||
| MIN_STALL_TIME | = | 10.0 |
| Number of seconds between updates before giving up to try and calculate bitrate anymore | ||
| MAX_SAMPLES | = | 20 |
| Number of samples used to calculate bitrate | ||
| [R] | last_update_time | The last time the upload history was updated |
| [RW] | message | A message you can set from your controller or view to be rendered in the upload_status_text helper method. If you set a messagein a controller then call session.update to make that message available to your upload_status action. |
| [R] | received_bytes | Number bytes received from the multipart post |
| [R] | total_bytes | Total number of bytes expected from the mutlipart post |
Create a new Progress object passing the expected number of bytes to receive
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 371
371: def initialize(total)
372: @total_bytes = total
373: reset!
374: end
Calculates the bitrate in bytes/second. If the transfer is stalled or just started, the bitrate will be 0
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 435
435: def bitrate
436: history_bytes, history_time = @history.transpose.map { |vals| vals.inject { |sum, v| sum + v } }
437: history_bytes / history_time rescue 0
438: end
Completed percent in integer form from 0..100
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 389
389: def completed_percent
390: (@received_bytes * 100 / @total_bytes).to_i rescue 0
391: end
Number of seconds elapsed since the start of the upload
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 441
441: def elapsed_seconds
442: @last_update_time
443: end
Returns true if there are bytes pending otherwise returns false
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 452
452: def finished?
453: remaining_bytes <= 0
454: end
Number of bytes left for this upload
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 384
384: def remaining_bytes
385: @total_bytes - @received_bytes
386: end
Calculate the seconds remaining based on the current bitrate. Returns O seconds if stalled or if no bytes have been received
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 447
447: def remaining_seconds
448: remaining_bytes / bitrate rescue 0
449: end
Resets the received_bytes, last_update_time, message and bitrate, but but maintains the total expected bytes
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 378
378: def reset!
379: @received_bytes, @last_update_time, @stalled, @message = 0, 0, false, ''
380: reset_history
381: end
Returns true if there has been a delay in receiving bytes. The delay is set by the constant MIN_STALL_TIME
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 463
463: def stalled?
464: @stalled
465: end
Returns true if some bytes have been received
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/upload_progress.rb, line 457
457: def started?
458: @received_bytes > 0
459: end