Module: ZipTricks::RailsStreaming

Defined in:
lib/zip_tricks/rails_streaming.rb

Overview

Should be included into a Rails controller (together with ActionController::Live) for easy ZIP output from any action.

Instance Method Summary collapse

Instance Method Details

#zip_tricks_stream(**zip_streamer_options) {|Streamer| ... } ⇒ ZipTricks::OutputEnumerator

Opens a Streamer and yields it to the caller. The output of the streamer gets automatically forwarded to the Rails response stream. When the output completes, the Rails response stream is going to be closed automatically.

Parameters:

  • zip_streamer_options (Hash)

    options that will be passed to the Streamer. See Streamer#initialize for the full list of options.

Yields:

  • (Streamer)

    the streamer that can be written to

Returns:



13
14
15
16
17
18
19
20
# File 'lib/zip_tricks/rails_streaming.rb', line 13

def zip_tricks_stream(**zip_streamer_options, &zip_streaming_blk)
  # Set a reasonable content type
  response.headers['Content-Type'] = 'application/zip'
  # Make sure nginx buffering is suppressed - see https://github.com/WeTransfer/zip_tricks/issues/48
  response.headers['X-Accel-Buffering'] = 'no'
  response.sending_file = true
  self.response_body = ZipTricks::OutputEnumerator.new(**zip_streamer_options, &zip_streaming_blk)
end