diff --git a/core/modules/editor/src/Form/EditorImageDialog.php b/core/modules/editor/src/Form/EditorImageDialog.php index 0a6291dc42..1b4d2c6f03 100644 --- a/core/modules/editor/src/Form/EditorImageDialog.php +++ b/core/modules/editor/src/Form/EditorImageDialog.php @@ -3,6 +3,7 @@ namespace Drupal\editor\Form; use Drupal\Component\Utility\Bytes; +use Drupal\Core\File\FileUploadInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\editor\Entity\Editor; @@ -27,14 +28,24 @@ class EditorImageDialog extends FormBase { */ protected $fileStorage; + /** + * The file upload service. + * + * @var \Drupal\Core\File\FileUploadInterface + */ + protected $fileUpload; + /** * Constructs a form object for image dialog. * * @param \Drupal\Core\Entity\EntityStorageInterface $file_storage * The file storage service. + * @param \Drupal\Core\File\FileUploadInterface $file_upload + * The file upload service. */ - public function __construct(EntityStorageInterface $file_storage) { + public function __construct(EntityStorageInterface $file_storage, FileUploadInterface $file_upload) { $this->fileStorage = $file_storage; + $this->fileUpload = $file_upload; } /** @@ -42,7 +53,8 @@ public function __construct(EntityStorageInterface $file_storage) { */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager')->getStorage('file') + $container->get('entity.manager')->getStorage('file'), + $container->get('file_upload') ); } @@ -91,7 +103,7 @@ public function buildForm(array $form, FormStateInterface $form_state, Editor $e else { $max_dimensions = 0; } - $max_filesize = min(Bytes::toInt($image_upload['max_size']), \Drupal::service('file_upload')->getUploadMaxSize()); + $max_filesize = min(Bytes::toInt($image_upload['max_size']), $this->fileUpload->getUploadMaxSize()); $existing_file = isset($image_element['data-entity-uuid']) ? \Drupal::entityManager()->loadEntityByUuid('file', $image_element['data-entity-uuid']) : NULL; $fid = $existing_file ? $existing_file->id() : NULL; diff --git a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php index e9e3856f82..1031bad5dc 100644 --- a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php +++ b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php @@ -2,33 +2,34 @@ namespace Drupal\file\Plugin\rest\resource; +use Drupal\Component\Render\PlainTextOutput; use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Crypt; use Drupal\Core\Config\Config; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\File\FileSystemInterface; +use Drupal\Core\File\FileUploadInterface; use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Utility\Token; +use Drupal\file\Entity\File; use Drupal\file\FileInterface; use Drupal\rest\ModifiedResourceResponse; use Drupal\rest\Plugin\ResourceBase; -use Drupal\Component\Render\PlainTextOutput; -use Drupal\Core\Entity\EntityFieldManagerInterface; -use Drupal\file\Entity\File; use Drupal\rest\Plugin\rest\resource\EntityResourceValidationTrait; use Drupal\rest\RequestHandler; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; use Symfony\Component\Routing\Route; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Exception\HttpException; /** * File upload resource. @@ -124,6 +125,13 @@ class FileUploadResource extends ResourceBase { */ protected $systemFileConfig; + /** + * The file upload service. + * + * @var \Drupal\Core\File\FileUploadInterface + */ + protected $fileUpload; + /** * Constructs a FileUploadResource instance. * @@ -153,8 +161,10 @@ class FileUploadResource extends ResourceBase { * The lock service. * @param \Drupal\Core\Config\Config $system_file_config * The system file configuration. + * @param \Drupal\Core\File\FileUploadInterface $file_upload + * The file upload service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, $serializer_formats, LoggerInterface $logger, FileSystemInterface $file_system, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, AccountInterface $current_user, MimeTypeGuesserInterface $mime_type_guesser, Token $token, LockBackendInterface $lock, Config $system_file_config) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, $serializer_formats, LoggerInterface $logger, FileSystemInterface $file_system, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, AccountInterface $current_user, MimeTypeGuesserInterface $mime_type_guesser, Token $token, LockBackendInterface $lock, Config $system_file_config, FileUploadInterface $file_upload) { parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger); $this->fileSystem = $file_system; $this->entityTypeManager = $entity_type_manager; @@ -164,6 +174,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition $this->token = $token; $this->lock = $lock; $this->systemFileConfig = $system_file_config; + $this->fileUpload = $file_upload; } /** @@ -183,7 +194,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('file.mime_type.guesser'), $container->get('token'), $container->get('lock'), - $container->get('config.factory')->get('system.file') + $container->get('config.factory')->get('system.file'), + $container->get('file_upload') ); } @@ -463,7 +475,7 @@ protected function prepareFilename($filename, array &$validators) { // valid extensions, munge the filename to protect against possible // malicious extension hiding within an unknown file type. For example, // "filename.html.foo". - $filename = \Drupal::service('file_upload')->mungeFilename($filename, $validators['file_validate_extensions'][0]); + $filename = $this->fileUpload->mungeFilename($filename, $validators['file_validate_extensions'][0]); } // Rename potentially executable files, to help prevent exploits (i.e. will @@ -524,7 +536,7 @@ protected function getUploadValidators(FieldDefinitionInterface $field_definitio $settings = $field_definition->getSettings(); // Cap the upload size according to the PHP limit. - $max_filesize = Bytes::toInt(\Drupal::service('file_upload')->getUploadMaxSize()); + $max_filesize = Bytes::toInt($this->fileUpload->getUploadMaxSize()); if (!empty($settings['max_filesize'])) { $max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize'])); } diff --git a/core/modules/locale/src/Form/ImportForm.php b/core/modules/locale/src/Form/ImportForm.php index fe7ad4b769..1c324dc502 100644 --- a/core/modules/locale/src/Form/ImportForm.php +++ b/core/modules/locale/src/Form/ImportForm.php @@ -2,6 +2,7 @@ namespace Drupal\locale\Form; +use Drupal\Core\File\FileUploadInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; @@ -38,14 +39,11 @@ class ImportForm extends FormBase { protected $languageManager; /** - * {@inheritdoc} + * The file upload service. + * + * @var \Drupal\Core\File\FileUploadInterface */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('module_handler'), - $container->get('language_manager') - ); - } + protected $fileUpload; /** * Constructs a form for language import. @@ -54,10 +52,24 @@ public static function create(ContainerInterface $container) { * The module handler service. * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager * The configurable language manager. + * @param \Drupal\Core\File\FileUploadInterface $file_upload + * The file upload service. */ - public function __construct(ModuleHandlerInterface $module_handler, ConfigurableLanguageManagerInterface $language_manager) { + public function __construct(ModuleHandlerInterface $module_handler, ConfigurableLanguageManagerInterface $language_manager, FileUploadInterface $file_upload) { $this->moduleHandler = $module_handler; $this->languageManager = $language_manager; + $this->fileUpload = $file_upload; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('module_handler'), + $container->get('language_manager'), + $container->get('file_upload') + ); } /** @@ -99,7 +111,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $validators = [ 'file_validate_extensions' => ['po'], - 'file_validate_size' => [\Drupal::service('file_upload')->getUploadMaxSize()], + 'file_validate_size' => [$this->fileUpload->getUploadMaxSize()], ]; $form['file'] = [ '#type' => 'file', diff --git a/core/modules/media_library/src/Form/MediaLibraryUploadForm.php b/core/modules/media_library/src/Form/MediaLibraryUploadForm.php index 41204f557c..e844da9f87 100644 --- a/core/modules/media_library/src/Form/MediaLibraryUploadForm.php +++ b/core/modules/media_library/src/Form/MediaLibraryUploadForm.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\TypedData\FieldItemDataDefinition; +use Drupal\Core\File\FileUploadInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\ElementInfoManagerInterface; @@ -70,6 +71,13 @@ class MediaLibraryUploadForm extends FormBase { */ protected $mediumStyleExists = FALSE; + /** + * The file upload service. + * + * @var \Drupal\Core\File\FileUploadInterface + */ + protected $fileUpload; + /** * Constructs a new MediaLibraryUploadForm. * @@ -77,11 +85,14 @@ class MediaLibraryUploadForm extends FormBase { * The entity type manager. * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info * The element info manager. + * @param \Drupal\Core\File\FileUploadInterface $file_upload + * The file upload service. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager, ElementInfoManagerInterface $element_info) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, ElementInfoManagerInterface $element_info, FileUploadInterface $file_upload) { $this->entityTypeManager = $entity_type_manager; $this->elementInfo = $element_info; $this->mediumStyleExists = !empty($entity_type_manager->getStorage('image_style')->load('medium')); + $this->fileUpload = $file_upload; } /** @@ -90,7 +101,8 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Ele public static function create(ContainerInterface $container) { return new static( $container->get('entity_type.manager'), - $container->get('element_info') + $container->get('element_info'), + $container->get('file_upload') ); } @@ -560,7 +572,7 @@ protected function mergeUploadValidators(array $types) { } // If no field defines a max size, default to the system wide setting. if ($max_size === 0) { - $max_size = \Drupal::service('file_upload')->getUploadMaxSize(); + $max_size = $this->fileUpload->getUploadMaxSize(); } return [ 'file_validate_extensions' => [implode(' ', $extensions)],