Implement streaming GET for Cellar

This commit is contained in:
Frederic Merizen 2018-02-14 16:38:22 +01:00
parent 0e74c55d48
commit e2c583480d

View file

@ -12,13 +12,27 @@ module ActiveStorage
end
def download(key)
instrument :download, key: key do
http_start do |http|
request = Net::HTTP::Get.new(URI::join(@endpoint, "/#{key}"))
sign(request, key)
response = http.request(request)
if response.is_a?(Net::HTTPSuccess)
response.body
if block_given?
instrument :streaming_download, key: key do
http_start do |http|
request = Net::HTTP::Get.new(URI::join(@endpoint, "/#{key}"))
sign(request, key)
http.request(request) do |response|
response.read_body do |chunk|
yield(chunk.force_encoding(Encoding::BINARY))
end
end
end
end
else
instrument :download, key: key do
http_start do |http|
request = Net::HTTP::Get.new(URI::join(@endpoint, "/#{key}"))
sign(request, key)
response = http.request(request)
if response.is_a?(Net::HTTPSuccess)
response.body.force_encoding(Encoding::BINARY)
end
end
end
end