Skip to content

Commit

Permalink
re-enable ActionController::Live (#3441)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
johrstrom authored Mar 21, 2024
1 parent c535463 commit b4ccb35
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion apps/dashboard/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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'
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ GEM
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.6.13)
zip_tricks (5.6.0)
zip_kit (6.2.0)

PLATFORMS
ruby
Expand Down Expand Up @@ -315,7 +315,7 @@ DEPENDENCIES
sprockets-rails (>= 2.0.0)
timecop (~> 0.9)
webrick
zip_tricks (~> 5.5)
zip_kit (~> 6.2)

BUNDLED WITH
2.3.6
33 changes: 18 additions & 15 deletions apps/dashboard/app/controllers/files_controller.rb
Original file line number Diff line number Diff line change
@@ -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]

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b4ccb35

Please sign in to comment.