Sha256: 8630ed0f134699775760d74095c3cf07fa885c5386c1870be1bfcb137514a8a2

Contents?: true

Size: 1.6 KB

Versions: 20

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module Bulkrax
  module DownloadBehavior
    # The following download code is based on
    # https://github.com/samvera/hydra-head/blob/main/hydra-core/app/controllers/concerns/hydra/controller/download_behavior.rb

    def file
      @file ||= File.open(file_path, 'r')
    end

    # Override this if you'd like a different filename
    # @return [String] the filename
    def file_name
      file_path.split('/').last
    end

    def download_content_type
      'application/zip'
    end

    def send_content
      response.headers['Accept-Ranges'] = 'bytes'
      if request.head?
        content_head
      else
        send_file_contents
      end
    end

    # Create some headers for the datastream
    def content_options
      { disposition: 'attachment', type: download_content_type, filename: file_name }
    end

    # render an HTTP HEAD response
    def content_head
      response.headers['Content-Length'] = file.size
      head :ok, content_type: download_content_type
    end

    def send_file_contents
      self.status = 200
      prepare_file_headers
      stream_body file.read
    end

    def prepare_file_headers
      send_file_headers! content_options
      response.headers['Content-Type'] = download_content_type
      response.headers['Content-Length'] ||= file.size.to_s
      # Prevent Rack::ETag from calculating a digest over body
      response.headers['Last-Modified'] = File.mtime(file_path).utc.strftime("%a, %d %b %Y %T GMT")
      self.content_type = download_content_type
    end

    def stream_body(iostream)
      self.response_body = iostream
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
bulkrax-3.2.0 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.1.2 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.1.1 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.1.0 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.0.1 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.0.0 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.0.0.beta8 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.0.0.beta7 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.0.0.beta6 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.0.0.beta5 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.0.0.beta4 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.0.0.beta3 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.0.0.beta2 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-3.0.0.beta1 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-2.3.0 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-2.2.4 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-2.2.3 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-2.2.2 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-2.2.1 app/models/concerns/bulkrax/download_behavior.rb
bulkrax-2.2.0 app/models/concerns/bulkrax/download_behavior.rb