From 9869e9834669808cbe9ea532cc6ed856bb0a6582 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Mon, 10 Mar 2025 23:31:14 -0500 Subject: [PATCH] stream caas file in chunks --- app.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app.rb b/app.rb index 7a9818a..55bad0a 100644 --- a/app.rb +++ b/app.rb @@ -20,6 +20,7 @@ $LOAD_PATH.unshift File.dirname(__FILE__) + "/lib" require "config" +CHUNK_SIZE = 1024**2 SESSION_SECRET_HEX_LENGTH = 64 JWT_SECRET_HEX_LENGTH = 64 @@ -506,7 +507,7 @@ get "/kitty" do # fail-safe url url = "https://i.imgur.com/epzPw7L.jpeg" end - path = Tempfile.open binmode: true do |f| + tmp_file = Tempfile.open binmode: true do |f| # f.chmod 0o644 response = HTTParty.get(url, stream_body: true) do |fragment| if [301, 302].include? fragment.code @@ -519,8 +520,15 @@ get "/kitty" do end content_type response.headers["content-type"] - f.path + f + end + + tmp_file.open do |f| + stream do |out| + out << f.read + until f.eof? + out << f.read(CHUNK_SIZE) + end + end end - send_file path - File.delete path end