You are here

function demo_file_download in Demonstration site (Sandbox / Snapshot) 8

Implements hook_file_download().

File

./demo.module, line 912

Code

function demo_file_download($uri) {
  $scheme = file_uri_scheme($uri);
  $target = \Drupal::service('stream_wrapper_manager')
    ->getTarget($uri);
  if ($scheme == 'private' && 0 === strpos($target, 'demo/')) {
    if (\Drupal::currentUser()
      ->hasPermission('export configuration')) {
      $request = \Drupal::request();
      $date = DateTime::createFromFormat('U', $request->server
        ->get('REQUEST_TIME'));
      $date_string = $date
        ->format('Y-m-d-H-i');
      $hostname = str_replace('.', '-', $request
        ->getHttpHost());
      $filename = 'config' . '-' . $hostname . '-' . $date_string . '.tar.gz';
      $disposition = 'attachment; filename="' . $filename . '"';
      return [
        'Content-disposition' => $disposition,
      ];
    }
    return -1;
  }
}