diff options
author | George Claghorn <george.claghorn@gmail.com> | 2017-07-25 21:03:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-25 21:03:48 -0400 |
commit | 5492c4efa9d869f207ea702d0b328f26c047b75c (patch) | |
tree | fd875fe29ac0012dac6b4c22dfc5d0f9c0b243ec /lib/active_storage | |
parent | 1907f465bc7a3385fa53fb2a2466372f96990615 (diff) | |
download | rails-5492c4efa9d869f207ea702d0b328f26c047b75c.tar.gz rails-5492c4efa9d869f207ea702d0b328f26c047b75c.tar.bz2 rails-5492c4efa9d869f207ea702d0b328f26c047b75c.zip |
Add direct upload support to the disk service
Diffstat (limited to 'lib/active_storage')
-rw-r--r-- | lib/active_storage/service/disk_service.rb | 29 |
1 files changed, 28 insertions, 1 deletions
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 |