Module: DownloadUtils
- Included in:
- Service::CloudinaryService
- Defined in:
- lib/active_storage/service/download_utils.rb
Instance Method Summary collapse
Instance Method Details
#download_range(source, range) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/active_storage/service/download_utils.rb', line 29 def download_range(source, range) url = URI.parse(source) http, req = setup_connection(url) req.range = range chunk = http.start { |agent| agent.request(req).body } chunk.force_encoding(Encoding::BINARY) end |
#stream_download(source, chunk_size = 5_242_880) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/active_storage/service/download_utils.rb', line 5 def stream_download(source, chunk_size = 5_242_880) url = URI.parse(source) http, req = setup_connection(url) content_length = http.request_head(url).content_length upper_limit = content_length + (content_length % chunk_size) offset = 0 http.start do |agent| while offset < upper_limit lim = (offset + chunk_size) # QUESTION: is it relevant to set the last chunk # to the exact remaining bytes # lim = content_length if lim > content_length req.range = (offset..lim) chunk = agent.request(req).body yield chunk.force_encoding(Encoding::BINARY) offset += chunk_size + 1 end end end |