You are here

function data_policy_export_file_download in Data Policy 8

Implements hook_file_download().

File

modules/data_policy_export/data_policy_export.module, line 13
The Data Policy Export module.

Code

function data_policy_export_file_download($uri) {
  $scheme = \Drupal::service('file_system')
    ->uriScheme($uri);
  $target = file_uri_target($uri);

  // Get the file to see who the owner is.
  $query = \Drupal::entityQuery('file');
  $query
    ->condition('uri', $uri);
  $fid = $query
    ->execute();

  /* @var \Drupal\file\FileInterface $file */
  $file = File::load(reset($fid));
  $access = FALSE;

  // Allow access to users with correct permission or file owner.
  if (\Drupal::currentUser()
    ->hasPermission('administer users') || \Drupal::currentUser()
    ->id() === $file
    ->get('uid')
    ->getString()) {
    $access = TRUE;
  }

  // The pattern should match all the declared file patterns from
  // the `generateFilePath()` methods in export bulk actions plugins.
  if ($scheme === 'private' && $access && preg_match('/^csv\\/export-data-policies-([a-f0-9]{12})\\.csv$/i', $target)) {
    return [
      'Content-disposition' => 'attachment; filename="' . basename($target) . '"',
    ];
  }
}