Skip to content
Snippets Groups Projects
Verified Commit 2e2f1d77 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3490549 by kim.pepper, lendude, larowlan: Add an interface for FileUploadHandler

parent 5b943115
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,7 @@
/**
* An event during file upload that lets subscribers sanitize the filename.
*
* @see \Drupal\file\Upload\FileUploadHandler::handleFileUpload()
* @see \Drupal\file\Upload\FileUploadHandlerInterface::handleFileUpload()
* @see \Drupal\file\Plugin\rest\resource\FileUploadResource::prepareFilename()
* @see \Drupal\system\EventSubscriber\SecurityFileUploadEventSubscriber::sanitizeName()
*/
......
......@@ -15,7 +15,7 @@
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\editor\Entity\Editor;
use Drupal\file\Upload\FileUploadHandler;
use Drupal\file\Upload\FileUploadHandlerInterface;
use Drupal\file\Upload\FormUploadedFile;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
......@@ -39,7 +39,7 @@ class CKEditor5ImageController extends ControllerBase {
*
* @param \Drupal\Core\File\FileSystemInterface $fileSystem
* The file system service.
* @param \Drupal\file\Upload\FileUploadHandler $fileUploadHandler
* @param \Drupal\file\Upload\FileUploadHandlerInterface $fileUploadHandler
* The file upload handler.
* @param \Drupal\Core\Lock\LockBackendInterface $lock
* The lock service.
......@@ -48,7 +48,7 @@ class CKEditor5ImageController extends ControllerBase {
*/
public function __construct(
protected FileSystemInterface $fileSystem,
protected FileUploadHandler $fileUploadHandler,
protected FileUploadHandlerInterface $fileUploadHandler,
protected LockBackendInterface $lock,
protected CKEditor5PluginManagerInterface $pluginManager,
) {
......
......@@ -23,6 +23,7 @@
use Drupal\Core\Template\Attribute;
use Drupal\file\FileInterface;
use Drupal\file\IconMimeTypes;
use Drupal\file\Upload\FileUploadHandlerInterface;
use Drupal\file\Upload\FormUploadedFile;
/**
......@@ -205,8 +206,8 @@ function file_save_upload($form_field_name, $validators = [], $destination = FAL
$destination = 'temporary://';
}
/** @var \Drupal\file\Upload\FileUploadHandler $file_upload_handler */
$file_upload_handler = \Drupal::service('file.upload_handler');
/** @var \Drupal\file\Upload\FileUploadHandlerInterface $file_upload_handler */
$file_upload_handler = \Drupal::service(FileUploadHandlerInterface::class);
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$files = [];
......
......@@ -13,7 +13,10 @@ services:
file.upload_handler:
class: Drupal\file\Upload\FileUploadHandler
arguments: ['@file_system', '@entity_type.manager', '@stream_wrapper_manager', '@event_dispatcher', '@file.mime_type.guesser', '@current_user', '@request_stack', '@file.repository', '@file.validator', '@lock', '@validation.basic_recursive_validator_factory']
Drupal\file\Upload\FileUploadHandler: '@file.upload_handler'
Drupal\file\Upload\FileUploadHandler:
alias: 'file.upload_handler'
deprecated: 'The "%alias_id%" service alias is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use Drupal\file\Upload\FileUploadHandlerInterface instead.'
Drupal\file\Upload\FileUploadHandlerInterface: '@file.upload_handler'
file.repository:
class: Drupal\file\FileRepository
arguments: [ '@file_system', '@stream_wrapper_manager', '@entity_type.manager', '@module_handler', '@file.usage', '@current_user' ]
......
......@@ -25,7 +25,7 @@
/**
* Handles validating and creating file entities from file uploads.
*/
class FileUploadHandler {
class FileUploadHandler implements FileUploadHandlerInterface {
/**
* The default extensions if none are provided.
......@@ -120,32 +120,7 @@ public function __construct(
}
/**
* Creates a file from an upload.
*
* @param \Drupal\file\Upload\UploadedFileInterface $uploadedFile
* The uploaded file object.
* @param array $validators
* The validators to run against the uploaded file.
* @param string $destination
* The destination directory.
* @param \Drupal\Core\File\FileExists|int $fileExists
* The behavior when the destination file already exists.
*
* @return \Drupal\file\Upload\FileUploadResult
* The created file entity.
*
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
* Thrown when a file upload error occurred and $throws is TRUE.
* @throws \Drupal\Core\File\Exception\FileWriteException
* Thrown when there is an error moving the file and $throws is TRUE.
* @throws \Drupal\Core\File\Exception\FileException
* Thrown when a file system error occurs and $throws is TRUE.
* @throws \Drupal\file\Upload\FileValidationException
* Thrown when file validation fails and $throws is TRUE.
* @throws \Drupal\Core\Lock\LockAcquiringException
* Thrown when a lock cannot be acquired.
* @throws \ValueError
* Thrown if $fileExists is a legacy int and not a valid value.
* {@inheritdoc}
*/
public function handleFileUpload(UploadedFileInterface $uploadedFile, array $validators = [], string $destination = 'temporary://', /*FileExists*/$fileExists = FileExists::Replace): FileUploadResult {
if (!$fileExists instanceof FileExists) {
......
<?php
declare(strict_types=1);
namespace Drupal\file\Upload;
use Drupal\Core\File\FileExists;
/**
* Handles validating and creating file entities from file uploads.
*/
interface FileUploadHandlerInterface {
/**
* Creates a file from an upload.
*
* @param \Drupal\file\Upload\UploadedFileInterface $uploadedFile
* The uploaded file object.
* @param array $validators
* The validators to run against the uploaded file.
* @param string $destination
* The destination directory.
* @param \Drupal\Core\File\FileExists|int $fileExists
* The behavior when the destination file already exists.
*
* @return \Drupal\file\Upload\FileUploadResult
* The created file entity.
*
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
* Thrown when a file upload error occurred and $throws is TRUE.
* @throws \Drupal\Core\File\Exception\FileWriteException
* Thrown when there is an error moving the file and $throws is TRUE.
* @throws \Drupal\Core\File\Exception\FileException
* Thrown when a file system error occurs and $throws is TRUE.
* @throws \Drupal\file\Upload\FileValidationException
* Thrown when file validation fails and $throws is TRUE.
* @throws \Drupal\Core\Lock\LockAcquiringException
* Thrown when a lock cannot be acquired.
* @throws \ValueError
* Thrown if $fileExists is a legacy int and not a valid value.
*/
public function handleFileUpload(UploadedFileInterface $uploadedFile, array $validators = [], string $destination = 'temporary://', $fileExists = FileExists::Replace): FileUploadResult;
}
......@@ -17,7 +17,7 @@
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\file\Upload\ContentDispositionFilenameParser;
use Drupal\file\Upload\FileUploadHandler;
use Drupal\file\Upload\FileUploadHandlerInterface;
use Drupal\file\Upload\FileUploadLocationTrait;
use Drupal\file\Upload\FileUploadResult;
use Drupal\file\Upload\InputStreamFileWriterInterface;
......@@ -63,7 +63,7 @@ class FileUpload {
public function __construct(
protected AccountInterface $currentUser,
protected EntityFieldManagerInterface $fieldManager,
protected FileUploadHandler $fileUploadHandler,
protected FileUploadHandlerInterface $fileUploadHandler,
protected HttpKernelInterface $httpKernel,
protected InputStreamFileWriterInterface $inputStreamFileWriter,
protected FileSystemInterface $fileSystem,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment