You are here

protected function TextimageDownloadController::returnBinary in Textimage 8.3

Same name and namespace in other branches
  1. 8.4 src/Controller/TextimageDownloadController.php \Drupal\textimage\Controller\TextimageDownloadController::returnBinary()

Returns the image file at URI.

Parameters

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

string $uri: The URI of the file to be returned.

Return value

\Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\Response The transferred file as response or some error response.

2 calls to TextimageDownloadController::returnBinary()
TextimageDownloadController::deferredDelivery in src/Controller/TextimageDownloadController.php
Deliver a Textimage from a deferred request.
TextimageDownloadController::urlDeliver in src/Controller/TextimageDownloadController.php
Deliver directly a Textimage from the URL request.

File

src/Controller/TextimageDownloadController.php, line 211

Class

TextimageDownloadController
Defines a controller to serve image styles.

Namespace

Drupal\textimage\Controller

Code

protected function returnBinary(Request $request, $uri) {

  // Don't try to send file if it is missing.
  if (!file_exists($uri)) {
    $this->logger
      ->notice("Textimage image at '%source_image_path' not found.", [
      '%source_image_path' => $uri,
    ]);
    return new Response($this
      ->t('Error downloading a textimage.'), 404);
  }
  if (($scheme = $this->fileSystem
    ->uriScheme($uri)) == 'private') {

    // If using the private scheme, defer control to FileDownloadController.
    $request->query
      ->set('file', file_uri_target($uri));
    return parent::download($request, $scheme);
  }
  else {

    // Get the image and transfer to client.
    $image = $this->imageFactory
      ->get($uri);
    $uri = $image
      ->getSource();
    $headers = [
      'Content-Type' => $image
        ->getMimeType(),
      'Content-Length' => $image
        ->getFileSize(),
    ];
    return new BinaryFileResponse($uri, 200, $headers);
  }
}