mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Expose chunked downloads
This commit is contained in:
parent
9cf3347899
commit
f66a69076f
3 changed files with 19 additions and 6 deletions
|
@ -56,8 +56,8 @@ class ActiveStorage::Blob < ActiveRecord::Base
|
||||||
service.upload(key, io, checksum: checksum)
|
service.upload(key, io, checksum: checksum)
|
||||||
end
|
end
|
||||||
|
|
||||||
def download
|
def download(&block)
|
||||||
service.download key
|
service.download key, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,8 @@ class ActiveStorage::Service::DiskService < ActiveStorage::Service
|
||||||
def download(key)
|
def download(key)
|
||||||
if block_given?
|
if block_given?
|
||||||
instrument :streaming_download, key do
|
instrument :streaming_download, key do
|
||||||
File.open(path_for(key)) do |file|
|
File.open(path_for(key), 'rb') do |file|
|
||||||
while data = file.binread(64.kilobytes)
|
while data = file.read(64.kilobytes)
|
||||||
yield data
|
yield data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -55,7 +55,7 @@ class ActiveStorage::Service::DiskService < ActiveStorage::Service
|
||||||
instrument :url, key do |payload|
|
instrument :url, key do |payload|
|
||||||
verified_key_with_expiration = ActiveStorage::VerifiedKeyWithExpiration.encode(key, expires_in: expires_in)
|
verified_key_with_expiration = ActiveStorage::VerifiedKeyWithExpiration.encode(key, expires_in: expires_in)
|
||||||
|
|
||||||
generated_url =
|
generated_url =
|
||||||
if defined?(Rails) && defined?(Rails.application)
|
if defined?(Rails) && defined?(Rails.application)
|
||||||
Rails.application.routes.url_helpers.rails_disk_blob_path(verified_key_with_expiration, disposition: disposition, filename: filename)
|
Rails.application.routes.url_helpers.rails_disk_blob_path(verified_key_with_expiration, disposition: disposition, filename: filename)
|
||||||
else
|
else
|
||||||
|
@ -63,7 +63,7 @@ class ActiveStorage::Service::DiskService < ActiveStorage::Service
|
||||||
end
|
end
|
||||||
|
|
||||||
payload[:url] = generated_url
|
payload[:url] = generated_url
|
||||||
|
|
||||||
generated_url
|
generated_url
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,6 +12,19 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
|
||||||
assert_equal Digest::MD5.base64digest(data), blob.checksum
|
assert_equal Digest::MD5.base64digest(data), blob.checksum
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "download yields chunks" do
|
||||||
|
blob = create_blob data: 'a' * 75.kilobytes
|
||||||
|
chunks = []
|
||||||
|
|
||||||
|
blob.download do |chunk|
|
||||||
|
chunks << chunk
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal 2, chunks.size
|
||||||
|
assert_equal 'a' * 64.kilobytes, chunks.first
|
||||||
|
assert_equal 'a' * 11.kilobytes, chunks.second
|
||||||
|
end
|
||||||
|
|
||||||
test "urls expiring in 5 minutes" do
|
test "urls expiring in 5 minutes" do
|
||||||
blob = create_blob
|
blob = create_blob
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue