Skip to content

Commit

Permalink
adds progressbar to downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
aaparmeggiani committed Mar 9, 2017
1 parent 4b745e5 commit 7f6d1f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/s3find/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'open-uri'
require 'active_support/core_ext/hash'
require 'ruby-progressbar'

module S3find
class Base
Expand Down Expand Up @@ -50,12 +51,29 @@ def count(result=[])

def download(item)
if item.size > 0
d = open(@bucket_uri + item.key)
IO.copy_stream(d, "./#{item.filename}")
pbar = SafeProgressBar.new(title: item.filename, total: nil, format: '%t: |%B| %p%% (%e )')
open(@bucket_uri + item.key,
content_length_proc: ->(bytes) { pbar.total = bytes },
progress_proc: ->(bytes) { pbar.progress = bytes }) do |io|
IO.copy_stream(io, "./#{item.filename}")
end
end
end


private

class SafeProgressBar < ProgressBar::Base
def progress=(new_progress)
self.total = new_progress if total <= new_progress
super
end

def total=(new_total)
super if new_total && new_total > 0
end
end

def endpoint(resource)
return resource if resource.start_with? 'http'
return resource.gsub('file://','') if resource.start_with? 'file://'
Expand Down
1 change: 1 addition & 0 deletions s3find.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ Gem::Specification.new do |spec|

spec.add_dependency "activesupport", "~> 4.2"
spec.add_dependency "actionview", "~> 4.2"
spec.add_dependency "ruby-progressbar", "~> 1.8"

end

0 comments on commit 7f6d1f8

Please sign in to comment.