Created
January 10, 2022 07:52
-
-
Save s1monj/654c7af6df2f1a8b323df71eaccef103 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map "/tar" do | |
use RailsEnvConfigMiddleware | |
use Rack::Auth::Basic, "Private data" do |username, password| | |
User.auth(username,password) | |
end | |
run StreamTar | |
end | |
StreamTar = ->(env) { | |
hijack_proc = ->(socket) { | |
IO.popen("tar --to-stdout -c #{env['tar_path']}", external_encoding: Encoding::BINARY) do |tar_io| | |
loop do | |
break if tar_io.eof? | |
tar_chunk = tar_io.read(64*1024) | |
socket.write(tar_chunk) if tar_chunk | |
end | |
end | |
socket.close unless socket.closed? | |
} | |
return [200, { | |
'Content-Disposition'=>"attachment;filename=#{env['filename']}", | |
'Content-Type'=>'application/octet-stream', | |
'Connection'=>'keep-alive', | |
'Content-Transfer-Encoding'=>'binary', | |
'rack.hijack' => hijack_proc | |
}, []] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment