You are here

class HttpMimeTypeGuesser in Remote Stream Wrapper 8

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

Hierarchy

  • class \Drupal\remote_stream_wrapper\File\MimeType\HttpMimeTypeGuesser implements \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface uses HttpClientTrait

Expanded class hierarchy of HttpMimeTypeGuesser

1 string reference to 'HttpMimeTypeGuesser'
remote_stream_wrapper.services.yml in ./remote_stream_wrapper.services.yml
remote_stream_wrapper.services.yml
1 service uses HttpMimeTypeGuesser
file.mime_type.guesser.http in ./remote_stream_wrapper.services.yml
Drupal\remote_stream_wrapper\File\MimeType\HttpMimeTypeGuesser

File

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

Namespace

Drupal\remote_stream_wrapper\File\MimeType
View source
class HttpMimeTypeGuesser implements MimeTypeGuesserInterface {
  use HttpClientTrait;

  /**
   * The file system.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * The extension guesser.
   *
   * @var \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface
   */
  protected $extensionGuesser;

  /**
   * Constructs a new HttpMimeTypeGuesser.
   *
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system.
   * @param \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface $extension_guesser
   *   The extension guesser.
   */
  public function __construct(FileSystemInterface $file_system, MimeTypeGuesserInterface $extension_guesser) {
    $this->fileSystem = $file_system;
    $this->extensionGuesser = $extension_guesser;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Parse a file name from a URI.
   *
   * This also requires the filename to have an extension.
   *
   * @param string $uri
   *   The URI.
   *
   * @return string|false
   *   The filename if it could be parsed from the URI, or FALSE otherwise.
   */
  public function parseFileNameFromUrl($uri) {

    // Extract the path part from the URL, ignoring query strings or fragments.
    if ($path = parse_url($uri, PHP_URL_PATH)) {
      $filename = $this->fileSystem
        ->basename($path);

      // Filename must contain a period in order to find a valid extension.
      // If the filename does not contain an extension, then guess() will
      // always return the default 'application/octet-stream' value.
      if (strpos($filename, '.') > 0) {
        return $filename;
      }
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpClientTrait::$httpClient protected property The HTTP client.
HttpClientTrait::getHttpClient public function Returns the HTTP client.
HttpClientTrait::requestTryHeadLookingForHeader public function Perform a HEAD/GET request looking for a specific header.
HttpClientTrait::setHttpClient public function Sets the HTTP client.
HttpMimeTypeGuesser::$extensionGuesser protected property The extension guesser.
HttpMimeTypeGuesser::$fileSystem protected property The file system.
HttpMimeTypeGuesser::guess public function Guesses the mime type of the file with the given path.
HttpMimeTypeGuesser::parseFileNameFromUrl public function Parse a file name from a URI.
HttpMimeTypeGuesser::__construct public function Constructs a new HttpMimeTypeGuesser.