2014-10-31 53 views
0

我有一个执行分块上传的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 

回答

1

您在每次迭代中添加当前offsetprogress。想象一下,你有一个10K的文件并以10个块的形式上传它。在第一次迭代中,我们的offset0,在接下来的1中,在第三个2中,然后是3。由于您总结了offsets,您的progress将显示60%,尽管它只完成了40%

,而不是添加offsetprogress的,只需设置progress到当前offset

upload_progress_bar.progress = offset 

或者更正确的,因为偏移告诉上传当前块之前发生了什么上传。

upload_progress_bar.progress = offset + default_chunk_size 
+0

哇,我不能相信我不只是想把偏移量看做是进步,现在感觉非常愚蠢。谢谢! :) – 2014-10-31 13:41:35