From 5492c4efa9d869f207ea702d0b328f26c047b75c Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Tue, 25 Jul 2017 21:03:48 -0400 Subject: Add direct upload support to the disk service --- lib/active_storage/service/disk_service.rb | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/active_storage/service/disk_service.rb b/lib/active_storage/service/disk_service.rb index 7e2079385f..d473771563 100644 --- a/lib/active_storage/service/disk_service.rb +++ b/lib/active_storage/service/disk_service.rb @@ -58,7 +58,7 @@ class ActiveStorage::Service::DiskService < ActiveStorage::Service verified_key_with_expiration = ActiveStorage.verifier.generate(key, expires_in: expires_in, purpose: :blob_key) generated_url = - if defined?(Rails) && defined?(Rails.application) + if defined?(Rails.application) Rails.application.routes.url_helpers.rails_disk_blob_path \ verified_key_with_expiration, disposition: disposition, filename: filename, content_type: content_type @@ -72,6 +72,32 @@ class ActiveStorage::Service::DiskService < ActiveStorage::Service end end + def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) + instrument :url, key do |payload| + verified_token_with_expiration = ActiveStorage.verifier.generate( + { + key: key, + content_type: content_type, + content_length: content_length, + checksum: checksum + }, + expires_in: expires_in, + purpose: :blob_token + ) + + generated_url = + if defined?(Rails.application) + Rails.application.routes.url_helpers.update_rails_disk_blob_path verified_token_with_expiration + else + "/rails/active_storage/disk/#{verified_token_with_expiration}" + end + + payload[:url] = generated_url + + generated_url + end + end + private def path_for(key) File.join root, folder_for(key), key @@ -87,6 +113,7 @@ class ActiveStorage::Service::DiskService < ActiveStorage::Service def ensure_integrity_of(key, checksum) unless Digest::MD5.file(path_for(key)).base64digest == checksum + delete key raise ActiveStorage::IntegrityError end end -- cgit v1.2.3