From 571be2c4ff7add4fe74ab7904bd4b833b70e229b Mon Sep 17 00:00:00 2001
From: Marc Wolf <71905-m4rcw0lf@users.noreply.drupalcode.org>
Date: Thu, 3 Apr 2025 10:14:26 +0000
Subject: [PATCH] Issue #3514883 by m4rcw0lf, kevinb623, marcoscano: Assertion
 fails when using external stream wrappers (e.g. s3fs) in
 PublicFileIntegration

---
 .../PublicFileIntegration.php                       | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/UrlToEntityIntegrations/PublicFileIntegration.php b/src/UrlToEntityIntegrations/PublicFileIntegration.php
index c1b9bbc..464013b 100644
--- a/src/UrlToEntityIntegrations/PublicFileIntegration.php
+++ b/src/UrlToEntityIntegrations/PublicFileIntegration.php
@@ -3,7 +3,6 @@
 namespace Drupal\entity_usage\UrlToEntityIntegrations;
 
 use Drupal\Core\Entity\EntityTypeManagerInterface;
-use Drupal\Core\StreamWrapper\LocalStream;
 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
 use Drupal\entity_usage\Events\Events;
 use Drupal\entity_usage\Events\UrlToEntityEvent;
@@ -25,8 +24,16 @@ class PublicFileIntegration implements EventSubscriberInterface {
     #[Autowire(service: 'stream_wrapper.public')]
     StreamWrapperInterface $publicStream,
   ) {
-    assert($publicStream instanceof LocalStream);
-    $this->publicFilePattern = '{^/?' . $publicStream->getDirectoryPath() . '/}';
+    $baseUrl = $publicStream->getExternalUrl();
+    $parsed = parse_url($baseUrl);
+
+    if (isset($parsed['path'])) {
+      $this->publicFilePattern = '{^' . preg_quote($parsed['path'], '{}') . '/}';
+    }
+    else {
+      throw new \LogicException('The public stream wrapper does not provide a valid external URL.');
+    }
+
   }
 
   /**
-- 
GitLab