Skip to content

Commit b70a810

Browse files
committed
Redirect blob request to attachment_url
1 parent edb8ccb commit b70a810

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lib/active_storage/service/database_service.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,34 @@ def exist?(key)
3333

3434
private
3535

36+
def private_url(key, expires_in:, filename:, content_type:, disposition:, **)
37+
content_disposition = content_disposition_with(type: disposition, filename: filename)
38+
verified_key_with_expiration = ActiveStorage.verifier.generate(
39+
{
40+
key: key,
41+
disposition: content_disposition,
42+
content_type: content_type,
43+
service_name: name
44+
},
45+
expires_in: expires_in,
46+
purpose: :blob_key
47+
)
48+
49+
if url_options.blank?
50+
raise ArgumentError, "Cannot generate URL for #{filename} using Database service, please set ActiveStorage::Current.url_options."
51+
end
52+
53+
url_helpers.attachment_url(verified_key_with_expiration, filename: filename, **url_options)
54+
end
55+
56+
def url_helpers
57+
@url_helpers ||= Rails.application.routes.url_helpers
58+
end
59+
60+
def url_options
61+
ActiveStorage::Current.url_options
62+
end
63+
3664
def service_name
3765
'Database'
3866
end

0 commit comments

Comments
 (0)