You are here

filemime.module in File MIME 8

Same filename and directory in other branches
  1. 6 filemime.module
  2. 7 filemime.module

Enables site admins to configure the MIME type mapping for uploaded files.

File

filemime.module
View source
<?php

/**
 * @file
 * Enables site admins to configure the MIME type mapping for uploaded files.
 */

/**
 * Alter the MIME type mapping based on the mime.types file and/or string.
 */
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;
      }
    }
  }
}

Functions

Namesort descending Description
filemime_file_mimetype_mapping_alter Alter the MIME type mapping based on the mime.types file and/or string.