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 {|Streamer| ... } ⇒ Object

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.

Yields:

  • (Streamer)

    the streamer that can be written to



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

def zip_tricks_stream
  # 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'
  # Create a wrapper for the write call that quacks like something you
  # can << to, used by ZipTricks
  w = ZipTricks::BlockWrite.new { |chunk| response.stream.write(chunk) }
  ZipTricks::Streamer.open(w) { |z| yield(z) }
ensure
  response.stream.close
end