You are here

private function WebformProtectedDownloadsController::sendProtectedFileResponse in Webform Protected Downloads 8

Gets the file from fid and creates a download http response.

1 call to WebformProtectedDownloadsController::sendProtectedFileResponse()
WebformProtectedDownloadsController::protectedFileDownload in src/Controller/WebformProtectedDownloadsController.php
Protected file download controller.

File

src/Controller/WebformProtectedDownloadsController.php, line 98

Class

WebformProtectedDownloadsController
Class WebformProtectedDownloadsController.

Namespace

Drupal\webform_protected_downloads\Controller

Code

private function sendProtectedFileResponse($fid) {

  // Get all the needed parameters.
  $file = File::load($fid);
  if (!$file) {
    throw new Exception\NotFoundHttpException();
  }
  $uri = $file
    ->getFileUri($file);
  $mimeTypeGuesser = \Drupal::service('file.mime_type.guesser');

  // Set HTTP header parameters.
  $headers = array(
    'Content-Type' => $mimeTypeGuesser
      ->guess($uri) . '; name="' . Unicode::mimeHeaderEncode(basename($uri)) . '"',
    'Content-Length' => filesize($uri),
    'Content-Disposition' => 'attachment; filename="' . Unicode::mimeHeaderEncode($file
      ->getFilename()) . '"',
    'Cache-Control' => 'private',
  );
  return new BinaryFileResponse($uri, 200, $headers);
}