You are here

public function VendorFileDownloadController::download in Vendor Stream Wrapper 8

Handles vendor file transfers.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object.

Return value

\Symfony\Component\HttpFoundation\BinaryFileResponse The transferred file as response.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Thrown when the requested file does not exist.

Overrides VendorFileDownloadControllerInterface::download

1 string reference to 'VendorFileDownloadController::download'
vendor_stream_wrapper.routing.yml in ./vendor_stream_wrapper.routing.yml
vendor_stream_wrapper.routing.yml

File

src/Controller/VendorFileDownloadController.php, line 65

Class

VendorFileDownloadController
Vendor Stream Wrapper file controller.

Namespace

Drupal\vendor_stream_wrapper\Controller

Code

public function download(Request $request) {
  $filepath = str_replace(':', '/', $request
    ->get('filepath'));
  $scheme = 'vendor';
  $uri = $scheme . '://' . $filepath;
  $mime_type = '';
  try {
    $mime_type = $this->mimeTypeGuesser
      ->guess($uri);
  } catch (\Exception $e) {
    $this->logger
      ->error('Vendor file download error: %message', [
      '%message' => $e
        ->getMessage(),
    ]);
  }
  if (!empty($mime_type)) {
    $headers = [
      'Content-Type' => $mime_type,
    ];
    try {
      return new BinaryFileResponse($uri, 200, $headers, TRUE);
    } catch (FileNotFoundException $e) {
      throw new NotFoundHttpException();
    }
  }
  throw new NotFoundHttpException();
}