module ZipTricks::RailsStreaming

Should be included into a Rails controller for easy ZIP output from any action.

Public Instance Methods

zip_tricks_stream(**zip_streamer_options, &zip_streaming_blk) click to toggle source

Opens a {ZipTricks::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. @param zip_streamer_options options that will be passed to the Streamer.

See {ZipTricks::Streamer#initialize} for the full list of options.

@yield [Streamer] the streamer that can be written to @return [ZipTricks::OutputEnumerator] The output enumerator assigned to the response body

# File lib/zip_tricks/rails_streaming.rb, line 12
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