Commit 2bd32ecf authored by git's avatar git Committed by Primsi

Issue #2828845 by VladimirMarko, Berdir, Primsi: Make filename transliteration...

Issue #2828845 by VladimirMarko, Berdir, Primsi: Make filename transliteration consistent with upcoming core issue
parent 36ef0ce5
......@@ -4,4 +4,4 @@ services:
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']
arguments: ['@request_stack', '@config.factory', '@transliteration', '@language_manager']
......@@ -4,8 +4,10 @@ namespace Drupal\dropzonejs;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Component\Utility\Unicode;
/**
* Handles files uploaded by Dropzone.
......@@ -43,6 +45,13 @@ class UploadHandler implements UploadHandlerInterface {
*/
protected $transliteration;
/**
* Language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs dropzone upload controller route controller.
*
......@@ -52,12 +61,15 @@ class UploadHandler implements UploadHandlerInterface {
* Config factory.
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
* Transliteration service.
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
* Transliteration service.
*/
public function __construct(RequestStack $request_stack, ConfigFactoryInterface $config, TransliterationInterface $transliteration) {
public function __construct(RequestStack $request_stack, ConfigFactoryInterface $config, TransliterationInterface $transliteration, LanguageManagerInterface $language_manager) {
$this->request = $request_stack->getCurrentRequest();
$tmp_override = $config->get('dropzonejs.settings')->get('tmp_dir');
$this->temporaryUploadLocation = $tmp_override ?: $config->get('system.file')->get('path.temporary');
$this->transliteration = $transliteration;
$this->languageManager = $language_manager;
}
/**
......@@ -87,13 +99,26 @@ class UploadHandler implements UploadHandlerInterface {
throw new UploadException(UploadException::FILENAME_ERROR);
}
// @todo The following filename sanitization steps replicate the behaviour
// of the 2492171-28 patch for https://www.drupal.org/node/2492171.
// Try to reuse that code instead, once that issue is committed.
// Transliterate.
$processed_filename = $this->transliteration->transliterate($original_name);
$langcode = $this->languageManager->getCurrentLanguage()->getId();
$filename = $this->transliteration->transliterate($original_name, $langcode, '');
// Replace whitespace.
$filename = str_replace(' ', '_', $filename);
// Remove remaining unsafe characters.
$filename = preg_replace('![^0-9A-Za-z_.-]!', '', $filename);
// 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);
// For security reasons append the txt extension. It will be removed in
// Drupal\dropzonejs\Element::valueCallback when we will know the valid
// extension and we will be able to properly sanitize the filename.
$processed_filename = $processed_filename . '.txt';
$processed_filename = $filename . '.txt';
return $processed_filename;
}
......
......@@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\FileBag;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests dropzoneJs upload controller.
......@@ -49,7 +50,7 @@ class DropzoneJsUploadControllerTest extends KernelTestBase {
*
* @var array
*/
public static $modules = ['system', 'file', 'user', 'dropzonejs'];
public static $modules = ['system', 'file', 'user', 'dropzonejs', 'language'];
/**
* {@inheritdoc}
......@@ -76,7 +77,13 @@ class DropzoneJsUploadControllerTest extends KernelTestBase {
public function testDropzoneJsUploadController() {
$this->container->get('router.builder')->rebuild();
$uploaded_file = new UploadedFile($this->tmpFile, "{$this->testfilePrefix}controller");
$language = ConfigurableLanguage::createFromLangcode('ru');
$language->save();
$this->config('system.site')->set('default_langcode', $language->getId())->save();
$unicode_emoticon = json_decode('"\uD83D\uDE0E"');
$uploaded_file = new UploadedFile($this->tmpFile, "{$this->testfilePrefix}controller-Капля a,A;1{$unicode_emoticon}.jpg");
$file_bag = new FileBag();
$file_bag->set('file', $uploaded_file);
......@@ -90,6 +97,7 @@ class DropzoneJsUploadControllerTest extends KernelTestBase {
$result = json_decode($controller_result->getContent());
$result_file = $this->filesDir . '/' . $result->result;
$this->assertStringEndsWith('-kapla_aa1.jpg.txt', $result_file);
$this->assertTrue(file_exists($result_file));
$this->assertEquals(file_get_contents($result_file), $this->testfileData);
}
......
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