You are here

public static function AcquiaDAMStreamWrapper::getMimeType in Media: Acquia DAM 7

Get the mimetype of the file.

Overridden to work with the Acquia DAM uri structure.

Overrides DrupalRemoteStreamWrapper::getMimeType

File

includes/AcquiaDAMStreamWrapper.inc, line 116
Create an Acquia DAM Stream Wrapper class for the Media/Resource module.

Class

AcquiaDAMStreamWrapper
Provides a remote stream wrapper for Acquia DAM assets.

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 DRUPAL_ROOT . '/includes/file.mimetypes.inc';
    $mapping = file_mimetype_mapping();
  }
  if ($target = file_uri_target($uri)) {
    $extension = '';
    $file_parts = explode('.', drupal_basename($target));
    $extensions = $mapping['extensions'];
    $mimetypes = $mapping['mimetypes'];
    $parsed_overrides = static::getMimetypeOverrides();

    // 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 = strtolower($additional_part . ($extension ? '.' . $extension : ''));
      if (!empty($parsed_overrides[$extension])) {
        $extension = $parsed_overrides[$extension];
      }
      if (!empty($mimetypes[$extensions[$extension]])) {
        return $mimetypes[$extensions[$extension]];
      }
    }
  }
  return 'application/octet-stream';
}