123456789_123456789_123456789_123456789_123456789_

Class: ActiveStorage::Downloader

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activestorage/lib/active_storage/downloader.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(service) ⇒ Downloader

[ GitHub ]

  
# File 'activestorage/lib/active_storage/downloader.rb', line 7

def initialize(service)
  @service = service
end

Instance Attribute Details

#service (readonly)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/downloader.rb', line 5

attr_reader :service

Instance Method Details

#download(key, file) (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/downloader.rb', line 30

def download(key, file)
  file.binmode
  service.download(key) { |chunk| file.write(chunk) }
  file.flush
  file.rewind
end

#open(key, checksum: nil, verify: true, name: "ActiveStorage-", tmpdir: nil)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/downloader.rb', line 11

def open(key, checksum: nil, verify: true, name: "ActiveStorage-", tmpdir: nil)
  open_tempfile(name, tmpdir) do |file|
    download key, file
    verify_integrity_of(file, checksum: checksum) if verify
    yield file
  end
end

#open_tempfile(name, tmpdir = nil) (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/downloader.rb', line 20

def open_tempfile(name, tmpdir = nil)
  file = Tempfile.open(name, tmpdir)

  begin
    yield file
  ensure
    file.close!
  end
end

#verify_integrity_of(file, checksum:) (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/downloader.rb', line 37

def verify_integrity_of(file, checksum:)
  unless OpenSSL::Digest::MD5.file(file).base64digest == checksum
    raise ActiveStorage::IntegrityError
  end
end