You are here

function feeds_file_download in Feeds 8.3

Same name and namespace in other branches
  1. 8.2 feeds.module \feeds_file_download()
  2. 7.2 feeds.module \feeds_file_download()

Implements hook_file_download().

File

./feeds.module, line 148
Feeds hook implementations.

Code

function feeds_file_download($uri) {

  // Get the file record based on the URI. If not in the database just return.

  /** @var \Drupal\file\FileInterface[] $files */
  $files = \Drupal::entityTypeManager()
    ->getStorage('file')
    ->loadByProperties([
    'uri' => $uri,
  ]);
  foreach ($files as $item) {

    // Since some database servers sometimes use a case-insensitive comparison
    // by default, double check that the filename is an exact match.
    if ($item
      ->getFileUri() === $uri) {
      $file = $item;
      break;
    }
  }
  if (!isset($file)) {
    return;
  }

  // Check if this file belongs to Feeds.
  $usage = \Drupal::service('file.usage')
    ->listUsage($file);
  if (!isset($usage['feeds'])) {
    return;
  }
  $feeds = \Drupal::entityTypeManager()
    ->getStorage('feeds_feed')
    ->loadByProperties([
    'source' => $uri,
  ]);
  foreach ($feeds as $feed) {
    if ($feed
      ->getSource() === $uri && $feed
      ->access('import')) {
      return file_get_content_headers($file);
    }
  }
  return -1;
}