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