diff --git a/activestorage/lib/active_storage/service/disk_service.rb b/activestorage/lib/active_storage/service/disk_service.rb index 3b7ef75286..147cfa3ed5 100644 --- a/activestorage/lib/active_storage/service/disk_service.rb +++ b/activestorage/lib/active_storage/service/disk_service.rb @@ -124,6 +124,10 @@ def generate_url(key, expires_in:, filename:, content_type:, disposition:) purpose: :blob_key ) + if current_host.blank? + raise ArgumentError, "Cannot generate URL for #{filename} using Disk service, please set ActiveStorage::Current.host." + end + current_uri = URI.parse(current_host) url_helpers.rails_disk_service_url(verified_key_with_expiration, diff --git a/activestorage/test/service/disk_service_test.rb b/activestorage/test/service/disk_service_test.rb index e7d24b1ce5..8196302a8e 100644 --- a/activestorage/test/service/disk_service_test.rb +++ b/activestorage/test/service/disk_service_test.rb @@ -23,6 +23,16 @@ class ActiveStorage::Service::DiskServiceTest < ActiveSupport::TestCase end end + test "URL generation without ActiveStorage::Current.host set" do + ActiveStorage::Current.host = nil + + error = assert_raises ArgumentError do + @service.url(@key, expires_in: 5.minutes, disposition: :inline, filename: ActiveStorage::Filename.new("avatar.png"), content_type: "image/png") + end + + assert_equal("Cannot generate URL for avatar.png using Disk service, please set ActiveStorage::Current.host.", error.message) + end + test "headers_for_direct_upload generation" do assert_equal({ "Content-Type" => "application/json" }, @service.headers_for_direct_upload(@key, content_type: "application/json")) end