write CaaS cat as a temporary file
this will make it eaiser to set the content-type correctly
This commit is contained in:
parent
54a3de77e2
commit
b247971b7e
21
app.rb
21
app.rb
@ -499,19 +499,28 @@ get "/bytes/:size" do
|
||||
end
|
||||
|
||||
get "/kitty" do
|
||||
caas_host = ENV.fetch "CAAS_SERVICE_HOST"
|
||||
caas_port = ENV.fetch "CAAS_SERVICE_PORT"
|
||||
caas_host = ENV.fetch "CAAS_SERVICE_HOST", nil
|
||||
caas_port = ENV.fetch "CAAS_SERVICE_PORT", nil
|
||||
url = "http://#{caas_host}:#{caas_port}/"
|
||||
response = HTTParty.head(url)
|
||||
content_type response.headers["content-type"]
|
||||
stream do |out|
|
||||
unless caas_host && caas_port
|
||||
# fail-safe url
|
||||
url = "https://i.imgur.com/epzPw7L.jpeg"
|
||||
end
|
||||
path = Tempfile.open binmode: true do |f|
|
||||
# f.chmod 0o644
|
||||
response = HTTParty.get(url, stream_body: true) do |fragment|
|
||||
if [301, 302].include? fragment.code
|
||||
print "skip writing for redirect"
|
||||
elsif fragment.code == 200
|
||||
out << fragment
|
||||
f.write fragment
|
||||
else
|
||||
raise StandardError, "non-success status code while streaming #{fragment.code}"
|
||||
end
|
||||
end
|
||||
|
||||
content_type response.headers["content-type"]
|
||||
f.path
|
||||
end
|
||||
send_file path
|
||||
File.delete path
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user