Sha256: 5a0b879f07518f165bcb816cb53a88ee94b4519c01e71efbbe48b74b29bee3f5

Contents?: true

Size: 940 Bytes

Versions: 73

Compression:

Stored size: 940 Bytes

Contents

# frozen_string_literal: true

module ActiveStorage
  class Downloader #:nodoc:
    attr_reader :service

    def initialize(service)
      @service = service
    end

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

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

        begin
          yield file
        ensure
          file.close!
        end
      end

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

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

Version data entries

73 entries across 73 versions & 7 rubygems

Version Path
activestorage-6.1.7.10 lib/active_storage/downloader.rb
activestorage-6.1.7.9 lib/active_storage/downloader.rb
activestorage-6.1.7.8 lib/active_storage/downloader.rb
activestorage-6.1.7.7 lib/active_storage/downloader.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-6.1.6.1/lib/active_storage/downloader.rb
activestorage-6.1.7.6 lib/active_storage/downloader.rb
activestorage-6.1.7.5 lib/active_storage/downloader.rb
activestorage-6.1.7.4 lib/active_storage/downloader.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activestorage-6.1.6.1/lib/active_storage/downloader.rb
activestorage-6.1.7.3 lib/active_storage/downloader.rb
activestorage-6.1.7.2 lib/active_storage/downloader.rb
activestorage-6.1.7.1 lib/active_storage/downloader.rb
activestorage-6.0.6.1 lib/active_storage/downloader.rb
activestorage-6.1.7 lib/active_storage/downloader.rb
activestorage-6.0.6 lib/active_storage/downloader.rb
activestorage-6.1.6.1 lib/active_storage/downloader.rb
activestorage-6.0.5.1 lib/active_storage/downloader.rb
activestorage-6.0.5 lib/active_storage/downloader.rb
activestorage-6.1.6 lib/active_storage/downloader.rb
activestorage-6.1.5.1 lib/active_storage/downloader.rb