Commit b52701b0 authored by katherined's avatar katherined Committed by chr.fritsch

Issue #3042624 by katherined, RoshniPatel.addweb, volodymyr.bratushchak:...

Issue #3042624 by katherined, RoshniPatel.addweb, volodymyr.bratushchak: Drupal 9 Deprecated Code Report
parent 22afb9b4
......@@ -22,7 +22,7 @@ function dropzonejs_requirements($phase) {
// If library is not found, then look in the current profile libraries path.
if (!$library_found) {
$profile_path = drupal_get_path('profile', drupal_get_profile());
$profile_path = drupal_get_path('profile', \Drupal::installProfile());
$profile_path .= '/libraries/dropzone/dist/min/dropzone.min.js';
// Is the library found in the current profile libraries path.
$library_found = file_exists($profile_path);
......
services:
dropzonejs.upload_save:
class: Drupal\dropzonejs\DropzoneJsUploadSave
arguments: ['@entity.manager', '@file.mime_type.guesser', '@file_system', '@logger.factory', '@renderer', '@config.factory', '@token']
arguments: ['@entity.manager', '@file.mime_type.guesser', '@file_system', '@logger.factory', '@renderer', '@config.factory', '@token', '@messenger']
dropzonejs.upload_handler:
class: Drupal\dropzonejs\UploadHandler
arguments: ['@request_stack', '@config.factory', '@transliteration', '@language_manager']
......@@ -3,10 +3,12 @@
namespace Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget;
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Utility\Token;
......@@ -49,6 +51,13 @@ class DropzoneJsEbWidget extends WidgetBase {
*/
protected $token;
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Constructs widget plugin.
*
......@@ -71,11 +80,12 @@ class DropzoneJsEbWidget extends WidgetBase {
* @param \Drupal\Core\Utility\Token $token
* The token service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, DropzoneJsUploadSaveInterface $dropzonejs_upload_save, AccountProxyInterface $current_user, Token $token) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, DropzoneJsUploadSaveInterface $dropzonejs_upload_save, AccountProxyInterface $current_user, Token $token, FileSystemInterface $fileSystem) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager, $validation_manager);
$this->dropzoneJsUploadSave = $dropzonejs_upload_save;
$this->currentUser = $current_user;
$this->token = $token;
$this->fileSystem = $fileSystem;
}
/**
......@@ -91,7 +101,8 @@ class DropzoneJsEbWidget extends WidgetBase {
$container->get('plugin.manager.entity_browser.widget_validation'),
$container->get('dropzonejs.upload_save'),
$container->get('current_user'),
$container->get('token')
$container->get('token'),
$container->get('file_system')
);
}
......@@ -102,7 +113,7 @@ class DropzoneJsEbWidget extends WidgetBase {
return [
'upload_location' => 'public://[date:custom:Y]-[date:custom:m]',
'dropzone_description' => $this->t('Drop files here to upload them'),
'max_filesize' => file_upload_max_size() / pow(Bytes::KILOBYTE, 2) . 'M',
'max_filesize' => Environment::getUploadMaxSize() / pow(Bytes::KILOBYTE, 2) . 'M',
'extensions' => 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp',
'clientside_resize' => FALSE,
'resize_width' => NULL,
......@@ -247,7 +258,7 @@ class DropzoneJsEbWidget extends WidgetBase {
// it's still better not to rely only on client side validation.
if (($trigger['#type'] == 'submit' && $trigger['#name'] == 'op') || $trigger['#name'] === 'auto_select_handler') {
$upload_location = $this->getUploadLocation();
if (!file_prepare_directory($upload_location, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
if (!$this->fileSystem->prepareDirectory($upload_location, FileSystemInterface::MODIFY_PERMISSIONS)) {
$form_state->setError($form['widget']['upload'], $this->t('Files could not be uploaded because the destination directory %destination is not configured correctly.', ['%destination' => $this->getConfiguration()['settings']['upload_location']]));
}
......
......@@ -6,6 +6,7 @@ use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
......@@ -73,6 +74,13 @@ class DropzoneJsUploadSave implements DropzoneJsUploadSaveInterface {
*/
protected $token;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Construct the DropzoneUploadSave object.
*
......@@ -90,8 +98,10 @@ class DropzoneJsUploadSave implements DropzoneJsUploadSaveInterface {
* Config factory service.
* @param \Drupal\Core\Utility\Token $token
* The token service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, MimeTypeGuesserInterface $mimetype_guesser, FileSystemInterface $file_system, LoggerChannelFactoryInterface $logger_factory, RendererInterface $renderer, ConfigFactoryInterface $config_factory, Token $token) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, MimeTypeGuesserInterface $mimetype_guesser, FileSystemInterface $file_system, LoggerChannelFactoryInterface $logger_factory, RendererInterface $renderer, ConfigFactoryInterface $config_factory, Token $token, MessengerInterface $messenger) {
$this->entityTypeManager = $entity_type_manager;
$this->mimeTypeGuesser = $mimetype_guesser;
$this->fileSystem = $file_system;
......@@ -99,6 +109,7 @@ class DropzoneJsUploadSave implements DropzoneJsUploadSaveInterface {
$this->renderer = $renderer;
$this->configFactory = $config_factory;
$this->token = $token;
$this->messenger = $messenger;
}
/**
......@@ -129,7 +140,7 @@ class DropzoneJsUploadSave implements DropzoneJsUploadSaveInterface {
// to add it here or else the file upload will fail.
if ($renamed && !empty($extensions)) {
$extensions .= ' txt';
drupal_set_message($this->t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $file->getFilename()]));
$this->messenger->addMessage($this->t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $file->getFilename()]));
}
// Validate the file.
......@@ -144,20 +155,21 @@ class DropzoneJsUploadSave implements DropzoneJsUploadSaveInterface {
'#items' => $errors,
],
];
drupal_set_message($this->renderer->renderPlain($message), 'error');
$this->messenger->addError($this->renderer->renderPlain($message));
return FALSE;
}
// Prepare destination.
if (!$this->prepareDestination($file, $destination)) {
drupal_set_message($this->t('The file could not be uploaded because the destination %destination is invalid.', ['%destination' => $destination]), 'error');
$this->messenger->addError($this->t('The file could not be uploaded because the destination %destination is invalid.', ['%destination' => $destination]));
return FALSE;
}
// Move uploaded files from PHP's upload_tmp_dir to destination.
$move_result = file_unmanaged_move($uri, $file->getFileUri());
$move_result = $this->fileSystem->move($uri, $file->getFileUri());
if (!$move_result) {
drupal_set_message($this->t('File upload error. Could not move uploaded file.'), 'error');
$this->messenger->addError($this->t('File upload error. Could not move uploaded file.'));
$this->logger->notice('Upload error. Could not move uploaded file %file to destination %destination.', ['%file' => $file->getFilename(), '%destination' => $file->getFileUri()]);
return FALSE;
}
......@@ -231,7 +243,7 @@ class DropzoneJsUploadSave implements DropzoneJsUploadSaveInterface {
if (substr($destination, -1) != '/') {
$destination .= '/';
}
$destination = file_destination($destination . $file->getFilename(), FILE_EXISTS_RENAME);
$destination = $this->fileSystem->getDestinationFilename($destination . $file->getFilename(), FileSystemInterface::EXISTS_RENAME);
$file->setFileUri($destination);
return TRUE;
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\dropzonejs\Element;
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
......@@ -96,7 +97,7 @@ class DropzoneJs extends FormElement {
];
if (empty($element['#max_filesize'])) {
$element['#max_filesize'] = file_upload_max_size();
$element['#max_filesize'] = Environment::getUploadMaxSize();
}
// Set #max_files to NULL (explicitly unlimited) if #max_files is not
......@@ -107,7 +108,7 @@ class DropzoneJs extends FormElement {
if (!\Drupal::currentUser()->hasPermission('dropzone upload files')) {
$element['#access'] = FALSE;
drupal_set_message(new TranslatableMarkup("You don't have sufficent permissions to use the DropzoneJS uploader. Contact your system administrator"), 'warning');
\Drupal::messenger()->addWarning(new TranslatableMarkup("You don't have sufficent permissions to use the DropzoneJS uploader. Contact your system administrator"));
}
return $element;
......@@ -185,7 +186,9 @@ class DropzoneJs extends FormElement {
if (file_exists($old_filepath)) {
// Finaly rename the file and add it to results.
$new_filepath = $tmp_upload_scheme . '://' . $name;
$move_result = file_unmanaged_move($old_filepath, $new_filepath);
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$move_result = $file_system->move($old_filepath, $new_filepath);
if ($move_result) {
$return['uploaded_files'][] = [
......@@ -194,7 +197,7 @@ class DropzoneJs extends FormElement {
];
}
else {
drupal_set_message(self::t('There was a problem while processing the file named @name', ['@name' => $name]), 'error');
\Drupal::messenger()->addError(self::t('There was a problem while processing the file named @name', ['@name' => $name]));
}
}
}
......
......@@ -8,7 +8,6 @@ use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Component\Utility\Unicode;
/**
* Handles files uploaded by Dropzone.
......@@ -96,7 +95,7 @@ class UploadHandler implements UploadHandlerInterface {
// Remove multiple consecutive non-alphabetical characters.
$filename = preg_replace('/(_)_+|(\.)\.+|(-)-+/', '\\1\\2\\3', $filename);
// Force lowercase to prevent issues on case-insensitive file systems.
$filename = Unicode::strtolower($filename);
$filename = strtolower($filename);
// For security reasons append the txt extension. It will be removed in
// Drupal\dropzonejs\Element::valueCallback when we will know the valid
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment