You are here

function content_file_download in Content Synchronization 8.2

Same name and namespace in other branches
  1. 8 content_sync.module \content_file_download()
  2. 3.0.x content_sync.module \content_file_download()

Implements hook_file_download().

File

./content_sync.module, line 131
Allows site administrators to modify content.

Code

function content_file_download($uri) {
  $scheme = \Drupal::service('stream_wrapper_manager')
    ->getScheme($uri);
  $target = \Drupal::service('stream_wrapper_manager')
    ->getTarget($uri);
  if ($scheme == 'temporary' && $target == 'content.tar.gz') {
    if (\Drupal::currentUser()
      ->hasPermission('export content')) {
      $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 = 'content-' . $hostname . '-' . $date_string . '.tar.gz';
      $disposition = 'attachment; filename="' . $filename . '"';
      return [
        'Content-disposition' => $disposition,
      ];
    }
    return -1;
  }
}