Sha256: 2e34686e888c5fc94fd0fd8163a79ebe2f2480cd7f20ef1dc9806cbc8ffb4a1e

Contents?: true

Size: 979 Bytes

Versions: 75

Compression:

Stored size: 979 Bytes

Contents

# frozen_string_literal: true

module ActiveStorage
  class Downloader # :nodoc:
    attr_reader :service

    def initialize(service)
      @service = service
    end

    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

    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 OpenSSL::Digest::MD5.file(file).base64digest == checksum
          raise ActiveStorage::IntegrityError
        end
      end
  end
end

Version data entries

75 entries across 72 versions & 7 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activestorage-7.1.3.4/lib/active_storage/downloader.rb
activestorage-8.0.1 lib/active_storage/downloader.rb
activestorage-8.0.0.1 lib/active_storage/downloader.rb
activestorage-7.2.2.1 lib/active_storage/downloader.rb
activestorage-7.1.5.1 lib/active_storage/downloader.rb
activestorage-7.0.8.7 lib/active_storage/downloader.rb
activestorage-8.0.0 lib/active_storage/downloader.rb
activestorage-7.2.2 lib/active_storage/downloader.rb
activestorage-7.1.5 lib/active_storage/downloader.rb
activestorage-8.0.0.rc2 lib/active_storage/downloader.rb
activestorage-7.2.1.2 lib/active_storage/downloader.rb
activestorage-7.1.4.2 lib/active_storage/downloader.rb
activestorage-7.0.8.6 lib/active_storage/downloader.rb
activestorage-8.0.0.rc1 lib/active_storage/downloader.rb
activestorage-7.2.1.1 lib/active_storage/downloader.rb
activestorage-7.1.4.1 lib/active_storage/downloader.rb
activestorage-7.0.8.5 lib/active_storage/downloader.rb
activestorage-8.0.0.beta1 lib/active_storage/downloader.rb
omg-activestorage-8.0.0.alpha9 lib/active_storage/downloader.rb
omg-activestorage-8.0.0.alpha8 lib/active_storage/downloader.rb