From 6cb08dc197424a0049eea42ffba32973e4697d23 Mon Sep 17 00:00:00 2001 From: Jeff Ohrstrom Date: Thu, 21 Mar 2024 13:43:49 -0400 Subject: [PATCH] re-enable ActionController::Live (#3441) This works to re-enable ActionController::Live. It replaces zip_tricks which is now deprecated with zip_kit which is the replacement. It also fixes the issue with streaming zip files so ActionController::Live can be used in the downloading of zip files. --- apps/dashboard/Gemfile | 2 +- apps/dashboard/Gemfile.lock | 4 +-- .../app/controllers/files_controller.rb | 33 ++++++++++--------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/apps/dashboard/Gemfile b/apps/dashboard/Gemfile index 6cd4f78423..088d3f3df3 100644 --- a/apps/dashboard/Gemfile +++ b/apps/dashboard/Gemfile @@ -48,7 +48,7 @@ gem 'mocha', '~> 2.1', group: :test gem 'autoprefixer-rails', '~> 10.2.5' gem 'dotiw' gem 'local_time', '~> 1.0.3' -gem 'zip_tricks', '~> 5.5' +gem 'zip_kit', '~> 6.2' gem 'rss', '~> 0.2' gem 'jsbundling-rails', '~> 1.0' diff --git a/apps/dashboard/Gemfile.lock b/apps/dashboard/Gemfile.lock index d351187edf..edda5d9174 100644 --- a/apps/dashboard/Gemfile.lock +++ b/apps/dashboard/Gemfile.lock @@ -270,7 +270,7 @@ GEM xpath (3.2.0) nokogiri (~> 1.8) zeitwerk (2.6.12) - zip_tricks (5.6.0) + zip_kit (6.2.2) PLATFORMS ruby @@ -306,7 +306,7 @@ DEPENDENCIES sinatra-contrib timecop (~> 0.9) webrick - zip_tricks (~> 5.5) + zip_kit (~> 6.2) BUNDLED WITH 2.3.6 diff --git a/apps/dashboard/app/controllers/files_controller.rb b/apps/dashboard/app/controllers/files_controller.rb index b71e581d59..621577f3ba 100644 --- a/apps/dashboard/app/controllers/files_controller.rb +++ b/apps/dashboard/app/controllers/files_controller.rb @@ -1,6 +1,6 @@ # The controller for all the files pages /dashboard/files class FilesController < ApplicationController - include ZipTricks::RailsStreaming + include ActionController::Live before_action :strip_sendfile_headers, only: [:fs] @@ -58,22 +58,25 @@ def fs response.sending_file = true response.cache_control[:public] ||= false - # FIXME: strategy 1: is below, use zip_tricks - # strategy 2: use actual zip command (likely much faster) and ActionController::Live - zip_tricks_stream do |zip| - @path.files_to_zip.each do |file| - begin - next unless File.readable?(file.realpath) - - if File.file?(file.realpath) - zip.write_deflated_file(file.relative_path.to_s) do |zip_file| - IO.copy_stream(file.realpath, zip_file) + zip_headers = ZipKit::OutputEnumerator.new.streaming_http_headers + response.headers.merge!(zip_headers) + + send_stream(filename: zipname) do |stream| + ZipKit::Streamer.open(stream) do |zip| + @path.files_to_zip.each do |file| + begin + next unless File.readable?(file.realpath) + + if File.file?(file.realpath) + zip.write_deflated_file(file.relative_path.to_s) do |zip_file| + IO.copy_stream(file.realpath, zip_file) + end + else + zip.add_empty_directory(dirname: file.relative_path.to_s) end - else - zip.add_empty_directory(dirname: file.relative_path.to_s) + rescue => e + logger.warn("error writing file #{file.path} to zip: #{e.message}") end - rescue => e - logger.warn("error writing file #{file.path} to zip: #{e.message}") end end end