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.0.4.8 lib/active_storage/downloader.rb
activestorage-6.1.5 lib/active_storage/downloader.rb
activestorage-6.1.4.7 lib/active_storage/downloader.rb
activestorage-6.0.4.7 lib/active_storage/downloader.rb
activestorage-6.1.4.6 lib/active_storage/downloader.rb
activestorage-6.0.4.6 lib/active_storage/downloader.rb
activestorage-6.0.4.5 lib/active_storage/downloader.rb
activestorage-6.1.4.5 lib/active_storage/downloader.rb
activestorage-6.1.4.4 lib/active_storage/downloader.rb
activestorage-6.0.4.4 lib/active_storage/downloader.rb
activestorage-6.1.4.3 lib/active_storage/downloader.rb
activestorage-6.0.4.3 lib/active_storage/downloader.rb
activestorage-6.0.4.2 lib/active_storage/downloader.rb
activestorage-6.1.4.2 lib/active_storage/downloader.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activestorage-6.1.4.1/lib/active_storage/downloader.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activestorage-6.1.4.1/lib/active_storage/downloader.rb
activestorage-6.1.4.1 lib/active_storage/downloader.rb
activestorage-6.0.4.1 lib/active_storage/downloader.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/activestorage-6.1.4/lib/active_storage/downloader.rb
activestorage-6.1.4 lib/active_storage/downloader.rb