fringd / zipline

A gem that lets you stream a zip file from rails
MIT License
289 stars 68 forks source link

How can I use zipline on workers #89

Closed ghost closed 9 months ago

ghost commented 1 year ago

I'm trying to use zipline on a worker but seems that is not posible doue to the following error:

NameError: undefined local variable or method `headers'
julik commented 9 months ago

What would you like to do on the workers? Do you need to generate a ZIP file and store it on the worker filesystem? I would suspect zipline is built for adding it to a controller - so for serving ZIPs from the web. The zipline method expects the API of the object it gets called on to support all kinds of ActionController methods, i.e. the ones for setting headers

julik commented 9 months ago

For reference, if you want to use it in a background job, you need to go one abstraction layer down, like so:

Tempfile.open("temp.zip") do |tf|
  tf.binmode
  zip_generator = Zipline::ZipGenerator.new(files)
  zip_generator.each { |bytes| tf.write(bytes) }
  tf.rewind
  # Do something with your tempfile, as it gets removed when you exit `Tempfile.open`
end