2014-10-31 54 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