stream caas file in chunks
This commit is contained in:
		
							
								
								
									
										16
									
								
								app.rb
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user