Commit 6dba2586 authored by Primsi's avatar Primsi
Browse files

Issue #2661402 by tduong, Berdir, Primsi: Support tokens in upload_location

parent 62f7b78d
services:
dropzonejs.upload_save:
class: Drupal\dropzonejs\DropzoneJsUploadSave
arguments: ['@entity.manager', '@file.mime_type.guesser', '@file_system', '@logger.factory', '@renderer', '@config.factory']
arguments: ['@entity.manager', '@file.mime_type.guesser', '@file_system', '@logger.factory', '@renderer', '@config.factory', '@token']
dropzonejs.upload_handler:
class: Drupal\dropzonejs\UploadHandler
arguments: ['@request_stack', '@config.factory', '@transliteration']
......@@ -83,7 +83,7 @@ class DropzoneJsEbWidget extends WidgetBase {
*/
public function defaultConfiguration() {
return [
'upload_location' => 'public://',
'upload_location' => 'public://[date:custom:Y]-[date:custom:m]',
'dropzone_description' => t('Drop files here to upload them'),
'max_filesize' => file_upload_max_size() / pow(Bytes::KILOBYTE, 2) . 'M',
'extensions' => 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp',
......
......@@ -7,11 +7,13 @@
namespace Drupal\dropzonejs;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Utility\Token;
use Drupal\file\FileInterface;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
use Symfony\Component\Validator\Constraints\File;
......@@ -68,6 +70,13 @@ class DropzoneJsUploadSave implements DropzoneJsUploadSaveInterface {
*/
protected $configFactory;
/**
* The token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* Construct the DropzoneUploadSave object.
*
......@@ -83,14 +92,17 @@ class DropzoneJsUploadSave implements DropzoneJsUploadSaveInterface {
* The renderer service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Config factory service.
* @param \Drupal\Core\Utility\Token $token
* The token service.
*/
public function __construct(EntityManagerInterface $entity_manager, MimeTypeGuesserInterface $mimetype_guesser, FileSystemInterface $file_system, LoggerChannelFactoryInterface $logger_factory, RendererInterface $renderer, ConfigFactoryInterface $config_factory) {
public function __construct(EntityManagerInterface $entity_manager, MimeTypeGuesserInterface $mimetype_guesser, FileSystemInterface $file_system, LoggerChannelFactoryInterface $logger_factory, RendererInterface $renderer, ConfigFactoryInterface $config_factory, Token $token) {
$this->entityManager = $entity_manager;
$this->mimeTypeGuesser = $mimetype_guesser;
$this->fileSystem = $file_system;
$this->logger = $logger_factory->get('dropzonejs');
$this->renderer = $renderer;
$this->configFactory = $config_factory;
$this->token = $token;
}
/**
......@@ -100,6 +112,10 @@ class DropzoneJsUploadSave implements DropzoneJsUploadSaveInterface {
// Create the file entity.
$file = $this->fileEntityFromUri($uri, $user);
// Replace tokens. As the tokens might contain HTML we convert it to plain
// text.
$destination = PlainTextOutput::renderFromHtml($this->token->replace($destination));
// Handle potentialy dangerous extensions.
$renamed = $this->renameExecutableExtensions($file);
// The .txt extension may not be in the allowed list of extensions. We have
......
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