Sha256: d01e08ab07fb6a83336d3a5eca5311bee0fa15d6f1da2b2ce2908919a03e6ea7

Contents?: true

Size: 956 Bytes

Versions: 3

Compression:

Stored size: 956 Bytes

Contents

# frozen_string_literal: true

module ActiveStorage
  class Downloader #:nodoc:
    def initialize(blob, tempdir: nil)
      @blob    = blob
      @tempdir = tempdir
    end

    def download_blob_to_tempfile
      open_tempfile do |file|
        download_blob_to file
        verify_integrity_of file
        yield file
      end
    end

    private
      attr_reader :blob, :tempdir

      def open_tempfile
        file = Tempfile.open([ "ActiveStorage-#{blob.id}-", blob.filename.extension_with_delimiter ], tempdir)

        begin
          yield file
        ensure
          file.close!
        end
      end

      def download_blob_to(file)
        file.binmode
        blob.download { |chunk| file.write(chunk) }
        file.flush
        file.rewind
      end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activestorage-6.0.0.beta3 lib/active_storage/downloader.rb
activestorage-6.0.0.beta2 lib/active_storage/downloader.rb
activestorage-6.0.0.beta1 lib/active_storage/downloader.rb