21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/active_storage_encryption/private_url_policy.rb', line 21
def private_url_for_streaming_via_controller(key, expires_in:, filename:, content_type:, disposition:, encryption_key:, blob_byte_size:)
if private_url_policy == :disable
raise ActiveStorageEncryption::StreamingDisabled, " Requested a signed GET URL for \#{key.inspect} on service \#{name}. This service\n has disabled presigned URLs (private_url_policy: disable), you have to use `Blob#download` instead.\n EOS\n end\n # This method requires the \"blob_byte_size\" because it is needed for HTTP ranges (you need to know the range of a resource),\n # The ActiveStorage::ProxyController retrieves the blob from the DB for that, but we can embed it right in the token.\n content_disposition = content_disposition_with(type: disposition, filename: filename)\n verified_key_with_expiration = ActiveStorageEncryption.token_encryptor.encrypt_and_sign(\n {\n key: key,\n disposition: content_disposition,\n encryption_key_sha256: Digest::SHA256.base64digest(encryption_key),\n content_type: content_type,\n service_name: name,\n blob_byte_size: blob_byte_size,\n encryption_key: Base64.strict_encode64(encryption_key)\n },\n expires_in: expires_in,\n purpose: :encrypted_get\n )\n\n # Both url_helpers and url_options are on the DiskService, but we need them here for other Services too\n url_helpers = ActiveStorageEncryption::Engine.routes.url_helpers\n url_options = ActiveStorage::Current.url_options\n\n if url_options.blank?\n raise ArgumentError, \"Cannot generate URL for \#{filename} because ActiveStorage::Current.url_options is not set\"\n end\n\n url_helpers.encrypted_blob_streaming_get_url(verified_key_with_expiration, filename: filename, **url_options)\nend\n"
|