You are here

public function DownloadController::download in Ubercart 8.4

Handles file downloading and error states.

Parameters

int $fid: The fid of the file specified to download.

\Symfony\Component\HttpFoundation\Request $request: The request of the page.

1 string reference to 'DownloadController::download'
uc_file.routing.yml in uc_file/uc_file.routing.yml
uc_file/uc_file.routing.yml

File

uc_file/src/Controller/DownloadController.php, line 207

Class

DownloadController
Handles administrative view of files that may be purchased and downloaded.

Namespace

Drupal\uc_file\Controller

Code

public function download($fid, Request $request) {
  $user = $this
    ->currentUser();

  // Error messages for various failed download states.
  $admin_message = $this
    ->t('Please contact the site administrator if this message has been received in error.');
  $error_messages = [
    UC_FILE_ERROR_NOT_A_FILE => $this
      ->t('The file you requested does not exist.'),
    UC_FILE_ERROR_TOO_MANY_BOGUS_REQUESTS => $this
      ->t('You have attempted to download an incorrect file URL too many times.'),
    UC_FILE_ERROR_INVALID_DOWNLOAD => $this
      ->t('The following URL is not a valid download link.') . ' ',
    UC_FILE_ERROR_TOO_MANY_LOCATIONS => $this
      ->t('You have downloaded this file from too many different locations.'),
    UC_FILE_ERROR_TOO_MANY_DOWNLOADS => $this
      ->t('You have reached the download limit for this file.'),
    UC_FILE_ERROR_EXPIRED => $this
      ->t('This file download has expired.') . ' ',
    UC_FILE_ERROR_HOOK_ERROR => $this
      ->t('A hook denied your access to this file.') . ' ',
  ];
  $ip = $request
    ->getClientIp();
  if ($user
    ->hasPermission('view all downloads')) {
    $file_download = uc_file_get_by_id($fid);
  }
  else {
    $file_download = uc_file_get_by_uid($user
      ->id(), $fid);
  }
  if (isset($file_download->filename)) {
    $file_download->full_path = uc_file_qualify_file($file_download->filename);
  }
  else {
    throw new AccessDeniedHttpException();
  }

  // If it's ok, we push the file to the user.
  $status = UC_FILE_ERROR_OK;
  if (!$user
    ->hasPermission('view all downloads')) {
    $status = $this
      ->validateDownload($file_download, $user, $ip);
  }
  if ($status == UC_FILE_ERROR_OK) {
    $this
      ->transferDownload($file_download, $ip);
  }
  else {
    $this
      ->messenger()
      ->addError($error_messages[$status] . $admin_message);

    // Kick 'em to the curb. >:)
    $this
      ->redirectDownload($user
      ->id());
  }
  drupal_exit();
}