You are here

function mimedetect_fileupload_file_validate in MimeDetect 8

Same name and namespace in other branches
  1. 7 mimedetect_fileupload/mimedetect_fileupload.module \mimedetect_fileupload_file_validate()

Implements hook_file_validate().

File

mimedetect_fileupload/mimedetect_fileupload.module, line 16
MimeDetect file upload validation sub-module.

Code

function mimedetect_fileupload_file_validate(FileInterface $file) {
  $errors = [];
  $detected_mime = \Drupal::service('mimedetect')
    ->getMime($file);
  if ($file
    ->getMimeType() != $detected_mime) {
    $extension = pathinfo($file
      ->getFilename(), PATHINFO_EXTENSION);
    $errors[] = t('Detected file MIME type %type does not match the filename extension %extension.', [
      '%type' => $detected_mime,
      '%extension' => $extension,
    ]);
  }
  return $errors;
}