Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions src/Controller/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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.
*
Expand All @@ -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
);
}

Expand Down Expand Up @@ -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(),
Expand Down