我有一个执行分块上传的Dropbox CLI api,使用ruby-progressbar作为上传方式的指标。
当文件小于4MB(分块上传的默认块大小)时它可以正常工作但是它有任何问题:
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/ruby-progressbar-1.2.0/lib/ruby-progressbar/components/progressable.rb:45:in `progress='
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/ruby-progressbar-1.2.0/lib/ruby-progressbar/base.rb:138:in `with_progressables'
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/ruby-progressbar-1.2.0/lib/ruby-progressbar/base.rb:45:in `block in progress='
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/ruby-progressbar-1.2.0/lib/ruby-progressbar/base.rb:148:in `with_update'
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/ruby-progressbar-1.2.0/lib/ruby-progressbar/base.rb:45:in `progress='
from /Users/peterso/Projects/slipsquare/lib/slipsquare/middleware/chunked_upload.rb:20:in `block in call'
from /opt/rubies/2.0.0-p451/lib/ruby/gems/2.0.0/gems/dropbox-api-petems-1.1.0/lib/dropbox-api/client/files.rb:50:in `chunked_upload'
我假设我在整体计算方面做了一些愚蠢的事情,即使上传已达到完整尺寸并完成,我仍然会增加进度条的总数。
但是我已经看了一段时间,我似乎找不到从条形图中获取当前进度的方法并且“如果progressbar.current_size + offset> total,则完成进度条”。
代码如下所示:
file_name = env['chunked_upload_file_name']
contents = File.open(env['chunked_upload_file_name'])
total_size = File.size(env['chunked_upload_file_name'])
say "Total Size: #{total_size} bytes"
upload_progress_bar = ProgressBar.create(:title => "Upload progress",
:format => '%a <%B> %p%% %t',
:starting_at => 0,
:total => total_size)
response = env['dropbox-client'].chunked_upload file_name, contents do |offset, upload|
upload_progress_bar.progress += offset
end
答案 0 :(得分:1)
您在每次迭代中将当前offset
添加到progress
。想象一下,你有一个10K的文件,并以10块为单位上传。在第一次迭代中,我们的offset
为0
,在下一个1
,第三个2
,然后是3
。由于您总结offsets
progress
60%
将显示40%
,尽管它只完成了offset
。
不要将progress
添加到progress
,只需将offset
设置为当前upload_progress_bar.progress = offset
:
upload_progress_bar.progress = offset + default_chunk_size
或者更正确,因为偏移量表示在上传当前块之前上传的内容。
{{1}}