Issue #3337162 by Spokje, paulocs, _pratik_, smustgrave, alexpott: Use FileRepositoryInterface in \Drupal\file\Upload\FileUploadHandler::loadByUri

merge-requests/3669/head
catch 2023-03-16 10:11:27 +00:00
parent 63bb02d547
commit 283d9be746
2 changed files with 18 additions and 18 deletions

View File

@ -6,7 +6,7 @@ services:
- { name: backend_overridable } - { name: backend_overridable }
file.upload_handler: file.upload_handler:
class: Drupal\file\Upload\FileUploadHandler class: Drupal\file\Upload\FileUploadHandler
arguments: [ '@file_system', '@entity_type.manager', '@stream_wrapper_manager', '@event_dispatcher', '@file.mime_type.guesser', '@current_user', '@request_stack' ] arguments: [ '@file_system', '@entity_type.manager', '@stream_wrapper_manager', '@event_dispatcher', '@file.mime_type.guesser', '@current_user', '@request_stack', '@file.repository' ]
file.repository: file.repository:
class: Drupal\file\FileRepository class: Drupal\file\FileRepository
arguments: [ '@file_system', '@stream_wrapper_manager', '@entity_type.manager', '@module_handler', '@file.usage', '@current_user' ] arguments: [ '@file_system', '@stream_wrapper_manager', '@entity_type.manager', '@module_handler', '@file.usage', '@current_user' ]

View File

@ -12,6 +12,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface; use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\file\Entity\File; use Drupal\file\Entity\File;
use Drupal\file\FileInterface; use Drupal\file\FileInterface;
use Drupal\file\FileRepositoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\File\Exception\CannotWriteFileException; use Symfony\Component\HttpFoundation\File\Exception\CannotWriteFileException;
use Symfony\Component\HttpFoundation\File\Exception\ExtensionFileException; use Symfony\Component\HttpFoundation\File\Exception\ExtensionFileException;
@ -83,6 +84,13 @@ class FileUploadHandler {
*/ */
protected $requestStack; protected $requestStack;
/**
* The file Repository.
*
* @var \Drupal\file\FileRepositoryInterface
*/
protected $fileRepository;
/** /**
* Constructs a FileUploadHandler object. * Constructs a FileUploadHandler object.
* *
@ -100,8 +108,10 @@ class FileUploadHandler {
* The current user. * The current user.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* The request stack. * The request stack.
* @param \Drupal\file\FileRepositoryInterface $fileRepository
* The file repository.
*/ */
public function __construct(FileSystemInterface $fileSystem, EntityTypeManagerInterface $entityTypeManager, StreamWrapperManagerInterface $streamWrapperManager, EventDispatcherInterface $eventDispatcher, MimeTypeGuesserInterface $mimeTypeGuesser, AccountInterface $currentUser, RequestStack $requestStack) { public function __construct(FileSystemInterface $fileSystem, EntityTypeManagerInterface $entityTypeManager, StreamWrapperManagerInterface $streamWrapperManager, EventDispatcherInterface $eventDispatcher, MimeTypeGuesserInterface $mimeTypeGuesser, AccountInterface $currentUser, RequestStack $requestStack, FileRepositoryInterface $fileRepository = NULL) {
$this->fileSystem = $fileSystem; $this->fileSystem = $fileSystem;
$this->entityTypeManager = $entityTypeManager; $this->entityTypeManager = $entityTypeManager;
$this->streamWrapperManager = $streamWrapperManager; $this->streamWrapperManager = $streamWrapperManager;
@ -109,6 +119,11 @@ class FileUploadHandler {
$this->mimeTypeGuesser = $mimeTypeGuesser; $this->mimeTypeGuesser = $mimeTypeGuesser;
$this->currentUser = $currentUser; $this->currentUser = $currentUser;
$this->requestStack = $requestStack; $this->requestStack = $requestStack;
if ($fileRepository === NULL) {
@trigger_error('Calling ' . __METHOD__ . ' without the $fileRepository argument is deprecated in drupal:10.1.5 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3346839', E_USER_DEPRECATED);
$fileRepository = \Drupal::service('file.repository');
}
$this->fileRepository = $fileRepository;
} }
/** /**
@ -333,24 +348,9 @@ class FileUploadHandler {
* *
* @return \Drupal\file\FileInterface|null * @return \Drupal\file\FileInterface|null
* The first file with the matched URI if found, NULL otherwise. * The first file with the matched URI if found, NULL otherwise.
*
* @todo replace with https://www.drupal.org/project/drupal/issues/3223209
*/ */
protected function loadByUri(string $uri): ?FileInterface { protected function loadByUri(string $uri): ?FileInterface {
$fileStorage = $this->entityTypeManager->getStorage('file'); return $this->fileRepository->loadByUri($uri);
/** @var \Drupal\file\FileInterface[] $files */
$files = $fileStorage->loadByProperties(['uri' => $uri]);
if (count($files)) {
foreach ($files as $item) {
// Since some database servers sometimes use a case-insensitive
// comparison by default, double check that the filename is an exact
// match.
if ($item->getFileUri() === $uri) {
return $item;
}
}
}
return NULL;
} }
} }