You are here

function filemime_file_mimetype_mapping_alter in File MIME 8

Same name and namespace in other branches
  1. 7 filemime.module \filemime_file_mimetype_mapping_alter()

Alter the MIME type mapping based on the mime.types file and/or string.

File

./filemime.module, line 11
Enables site admins to configure the MIME type mapping for uploaded files.

Code

function filemime_file_mimetype_mapping_alter(&$mapping) {

  // Build an array of MIME types from the configured file and string.
  $file = \Drupal::config('filemime.settings')
    ->get('file');
  $mimetypes = array_merge(is_readable($file) ? file($file) : [], explode("\n", \Drupal::config('filemime.settings')
    ->get('types')));

  // Split each MIME type into tokens by whitespace.
  foreach ($mimetypes as $mimetype) {
    $tokens = preg_split('/[\\s]+/', $mimetype, -1, PREG_SPLIT_NO_EMPTY);
    foreach ($tokens as $index => $token) {

      // If this token starts with #, consider it a comment and break to the
      // next MIME type.
      if (substr($token, 0, 1) == '#') {
        break;
      }

      // If this is not the first token, it must be an extension. Add it to the
      // extensions array.
      if ($index) {
        $mapping['extensions'][$token] = $tokens[0];
      }
      elseif (isset($tokens[1])) {
        $mapping['mimetypes'][$token] = $token;
      }
    }
  }
}