From dd9bfadb074c23b6532e8684f4a673a280450ac4 Mon Sep 17 00:00:00 2001 From: AlexTrandafir09 Date: Wed, 18 Feb 2026 15:20:01 +0200 Subject: [PATCH] refs #34750 - force stage_file_proxy to retrieve documents for edw_document --- src/Controller/FileController.php | 65 +++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/src/Controller/FileController.php b/src/Controller/FileController.php index c07b269..cf0cab9 100644 --- a/src/Controller/FileController.php +++ b/src/Controller/FileController.php @@ -2,10 +2,12 @@ namespace Drupal\edw_document\Controller; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\PathProcessor\InboundPathProcessorInterface; +use Drupal\Core\StreamWrapper\StreamWrapperManager; use Drupal\Core\Url; use Drupal\edw_document\Response\CacheableBinaryFileResponse; use Drupal\edw_document\Services\DocumentManager; @@ -14,6 +16,9 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\stage_file_proxy\DownloadManagerInterface; + /** * Controller for files. @@ -41,6 +46,21 @@ class FileController extends ControllerBase implements ContainerInjectionInterfa */ protected $pathProcessor; + /** + * The config factory service. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** + * The Stage File Proxy download manager service. + * + * @var \Drupal\stage_file_proxy\DownloadManagerInterface + */ + protected $downloadManager; + + /** * Constructs a new FileController object. * @@ -50,21 +70,32 @@ class FileController extends ControllerBase implements ContainerInjectionInterfa * The document manager service. * @param \Drupal\Core\PathProcessor\InboundPathProcessorInterface $path_processor * The inbound path processor. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory service. + * @param \Drupal\stage_file_proxy\DownloadManagerInterface $download_manager + * The Stage File Proxy download manager service. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager, DocumentManager $document_manager, InboundPathProcessorInterface $path_processor) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, DocumentManager $document_manager, InboundPathProcessorInterface $path_processor, ConfigFactoryInterface $config_factory, ?DownloadManagerInterface $download_manager = NULL) { $this->fileStorage = $entity_type_manager->getStorage('file'); $this->documentManager = $document_manager; $this->pathProcessor = $path_processor; + $this->configFactory = $config_factory; + $this->downloadManager = $download_manager; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { + $download_manager = $container->has('stage_file_proxy.download_manager') + ? $container->get('stage_file_proxy.download_manager') + : NULL; return new static( $container->get('entity_type.manager'), $container->get('edw_document.document.manager'), - $container->get('path_processor_manager') + $container->get('path_processor_manager'), + $container->get('config.factory'), + $download_manager ); } @@ -97,7 +128,35 @@ public function serveFile(Request $request, $uuid) { } $uri = $file->getFileUri(); if (!file_exists($uri)) { - return throw new NotFoundHttpException(); + if (!$this->downloadManager || !str_starts_with($uri, 'public://')) { + throw new NotFoundHttpException(); + } + + $config = $this->configFactory->get('stage_file_proxy.settings'); + $server = (string) $config->get('origin'); + if ($server === '') { + throw new NotFoundHttpException(); + } + $server = rtrim($server, "/ \t\n\r\0\x0B"); + + $originDir = trim((string) ($config->get('origin_dir') ?? '')); + $remoteFileDir = $originDir !== '' ? $originDir : $this->downloadManager->filePublicPath(); + + $relativePath = StreamWrapperManager::getTarget($uri); + + $queryParameters = UrlHelper::filterQueryParameters($request->query->all()); + + $options = [ + 'verify' => $config->get('verify'), + 'query' => $queryParameters, + 'headers' => [], + ]; + + $this->downloadManager->fetch($server, $remoteFileDir, $relativePath, $options); + + if (!file_exists($uri)) { + throw new NotFoundHttpException(); + } } $headers = [ 'Content-Type' => $file->getMimeType(),