1 file_example_session_streams.inc public static FileExampleSessionStreamWrapper::getMimeType($uri, $mapping = NULL)

Implements getMimeType().

Overrides BackdropStreamWrapperInterface::getMimeType

File

modules/examples/file_example/file_example_session_streams.inc, line 124
Provides a demonstration session:// stream-wrapper.

Class

FileExampleSessionStreamWrapper
Example stream wrapper class to handle session:// streams.

Code

public static function getMimeType($uri, $mapping = NULL) {
  if (!isset($mapping)) {
    // The default file map, defined in file.mimetypes.inc is quite big.
    // We only load it when necessary.
    include_once BACKDROP_ROOT . '/core/includes/file.mimetypes.inc';
    $mapping = file_mimetype_mapping();
  }

  $extension = '';
  $file_parts = explode('.', basename($uri));

  // Remove the first part: a full filename should not match an extension.
  array_shift($file_parts);

  // Iterate over the file parts, trying to find a match.
  // For my.awesome.image.jpeg, we try:
  // - jpeg
  // - image.jpeg, and
  // - awesome.image.jpeg
  while ($additional_part = array_pop($file_parts)) {
    $extension = backdrop_strtolower($additional_part . ($extension ? '.' . $extension : ''));
    if (isset($mapping['extensions'][$extension])) {
      return $mapping['mimetypes'][$mapping['extensions'][$extension]];
    }
  }

  return 'application/octet-stream';
}