class File in Helper 8
Provides helpers for working with files.
Hierarchy
- class \Drupal\helper\File
Expanded class hierarchy of File
1 file declares its use of File
- HelperExtension.php in src/
Twig/ HelperExtension.php
1 string reference to 'File'
1 service uses File
File
- src/
File.php, line 13
Namespace
Drupal\helperView source
class File {
/**
* The file storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $fileStorage;
/**
* The mime type guesser.
*
* @var \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface
*/
protected $mimeTypeGuesser;
/**
* File constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface $mime_type_guesser
* The mime type guesser.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, MimeTypeGuesserInterface $mime_type_guesser) {
$this->fileStorage = $entity_type_manager
->getStorage('file');
$this->mimeTypeGuesser = $mime_type_guesser;
}
/**
* Creates or reuses a file object based on a URI.
*
* @param string $uri
* A string containing the URI.
* @param bool $reuse_existing
* If TRUE will try to reuse an existing file with the same URI.
* If FALSE will always create a new file.
*
* @return \Drupal\file\FileInterface
* A file object.
*/
public function createOrReuseFromUri($uri, $reuse_existing = TRUE) {
if ($reuse_existing) {
// Check if this file already exists, and if so, return that.
$files = $this->fileStorage
->loadByProperties([
'uri' => $uri,
]);
if ($valid_files = array_filter($files, [
$this,
'filterValidFiles',
])) {
return reset($valid_files);
}
}
// If an existing file could not be found, create a new file.
return $this->fileStorage
->create([
'uri' => $uri,
'uid' => \Drupal::currentUser()
->id(),
]);
}
/**
* Filter callback; Permanent files or temporary files owned by current user.
*
* @param \Drupal\file\FileInterface $file
* The file object.
*
* @return bool
* TRUE if the file is valid, or FALSE otherwise.
*/
public static function filterValidFiles(FileInterface $file) {
return $file
->isPermanent() || $file
->getOwnerId() == \Drupal::currentUser()
->id();
}
/**
* Converts a file URL into a data URI.
*
* @param string $uri
* The file URI.
* @param bool $base_64_encode
* TRUE to return the data URI as base-64 encoded content.
* @param string|null $mimetype
* The optional mime type to provide for the data URI. If not provided
* the mime type guesser service will be used.
*
* @return string
* The image data URI for use in a src attribute.
*
* @throws \Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException
* If the file cannot be read.
*/
public function getDataUri($uri, $base_64_encode = TRUE, $mimetype = NULL) {
if (!isset($mimetype)) {
$mimetype = $this->mimeTypeGuesser
->guess($uri);
}
$contents = file_get_contents($uri);
if ($contents === FALSE) {
throw new AccessDeniedException($uri);
}
if ($base_64_encode) {
$contents = base64_encode($contents);
}
return 'data:' . $mimetype . ($base_64_encode ? ';base64' : '') . ',' . $contents;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
File:: |
protected | property | The file storage. | |
File:: |
protected | property | The mime type guesser. | |
File:: |
public | function | Creates or reuses a file object based on a URI. | |
File:: |
public static | function | Filter callback; Permanent files or temporary files owned by current user. | |
File:: |
public | function | Converts a file URL into a data URI. | |
File:: |
public | function | File constructor. |