You are here

public function HttpMimeTypeGuesser::guess in Remote Stream Wrapper 8

File

src/File/MimeType/HttpMimeTypeGuesser.php, line 47

Class

HttpMimeTypeGuesser
Makes possible to guess the MIME type of a remote file.

Namespace

Drupal\remote_stream_wrapper\File\MimeType

Code

public function guess($path) {

  // Ignore non-external URLs.
  if (!UrlHelper::isExternal($path)) {
    return NULL;
  }

  // Attempt to parse out the mime type if the URL contains a filename.
  if ($filename = $this
    ->parseFileNameFromUrl($path)) {
    $mimetype = $this->extensionGuesser
      ->guess($filename);
    if ($mimetype != 'application/octet-stream') {

      // Only return the guessed mime type if it found a valid match
      // instead of returning the default mime type.
      return $mimetype;
    }
  }
  try {
    $response = $this
      ->requestTryHeadLookingForHeader($path, 'Content-Type');
    if ($response
      ->hasHeader('Content-Type')) {
      return $response
        ->getHeaderLine('Content-Type');
    }
  } catch (\Exception $exception) {
    watchdog_exception('remote_stream_wrapper', $exception);
  }
  return NULL;
}