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 28

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

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

[ GitHub ]

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

def open(key, checksum: nil, verify: true, name: "ActiveStorage-", tmpdir: nil, &block)
  tempfile = Tempfile.new(name, tmpdir, binmode: true)
  download(key, tempfile)
  verify_integrity_of(tempfile, checksum: checksum) if verify

  if block_given?
    begin
      yield tempfile
    ensure
      tempfile.close!
    end
  else
    tempfile
  end
end

#verify_integrity_of(file, checksum:) (private)

[ GitHub ]

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

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