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'
1 service uses HttpMimeTypeGuesser
File
- src/
File/ MimeType/ HttpMimeTypeGuesser.php, line 13
Namespace
Drupal\remote_stream_wrapper\File\MimeTypeView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HttpClientTrait:: |
protected | property | The HTTP client. | |
HttpClientTrait:: |
public | function | Returns the HTTP client. | |
HttpClientTrait:: |
public | function | Perform a HEAD/GET request looking for a specific header. | |
HttpClientTrait:: |
public | function | Sets the HTTP client. | |
HttpMimeTypeGuesser:: |
protected | property | The extension guesser. | |
HttpMimeTypeGuesser:: |
protected | property | The file system. | |
HttpMimeTypeGuesser:: |
public | function | Guesses the mime type of the file with the given path. | |
HttpMimeTypeGuesser:: |
public | function | Parse a file name from a URI. | |
HttpMimeTypeGuesser:: |
public | function | Constructs a new HttpMimeTypeGuesser. |