You are here

protected function ExtractedText::isFileIndexable in Search API attachments 9.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/ExtractedText.php \Drupal\search_api_attachments\Plugin\Field\FieldFormatter\ExtractedText::isFileIndexable()

Check if the file is allowed to be indexed.

Parameters

object $file: A file object.

Return value

bool TRUE or FALSE

1 call to ExtractedText::isFileIndexable()
ExtractedText::extractFileContents in src/Plugin/Field/FieldFormatter/ExtractedText.php
Extracts content of given file.

File

src/Plugin/Field/FieldFormatter/ExtractedText.php, line 204

Class

ExtractedText
File formatter displaying text extracted form attachment document.

Namespace

Drupal\search_api_attachments\Plugin\Field\FieldFormatter

Code

protected function isFileIndexable($file) {

  // This method is a copy of
  // Drupal\search_api_attachments\Plugin\search_api\processor\FilesExtractor::isFileIndexable()
  // and differs mostly in the signature. Unfortunately it can't be used here
  // as it requires second argument of type
  // \Drupal\search_api\Item\ItemInterface.
  // File should exist in disc.
  $indexable = file_exists($file
    ->getFileUri());
  if (!$indexable) {
    return FALSE;
  }

  // File should have a mime type that is allowed.
  $excluded_extensions_array = explode(' ', $this
    ->getSetting('excluded_extensions'));
  $all_excluded_mimes = $this->extractFileValidator
    ->getExcludedMimes($excluded_extensions_array);
  $indexable = $indexable && !in_array($file
    ->getMimeType(), $all_excluded_mimes);
  if (!$indexable) {
    return FALSE;
  }

  // File permanent.
  $indexable = $indexable && $file
    ->isPermanent();
  if (!$indexable) {
    return FALSE;
  }

  // File shouldn't exceed configured file size.
  $max_filesize = $this
    ->getSetting('max_filesize');
  $indexable = $indexable && $this->extractFileValidator
    ->isFileSizeAllowed($file, $max_filesize);
  if (!$indexable) {
    return FALSE;
  }

  // Whether a private file can be indexed or not.
  $excluded_private = $this
    ->getSetting('excluded_private');
  $indexable = $indexable && $this->extractFileValidator
    ->isPrivateFileAllowed($file, $excluded_private);
  if (!$indexable) {
    return FALSE;
  }
  $result = $this->moduleHandler
    ->invokeAll('search_api_attachments_indexable', [
    $file,
  ]);
  $indexable = !in_array(FALSE, $result, TRUE);
  return $indexable;
}