Commit a7477cd2 authored by jungle's avatar jungle Committed by chr.fritsch

Issue #2846920 by jungle, azcomi, alaa abbad: Make filename transliteration optional

parent 365cc0e6
tmp_upload_scheme: temporary
filename_transliteration: true
......@@ -5,3 +5,6 @@ dropzonejs.settings:
tmp_upload_scheme:
type: string
label: 'Upload scheme'
filename_transliteration:
type: boolean
label: 'Transliterate names of uploaded files'
......@@ -51,3 +51,14 @@ function dropzonejs_update_8001() {
$config->set('tmp_upload_scheme', 'temporary');
$config->save(TRUE);
}
/**
* Set default value for transliterate file name.
*/
function dropzonejs_update_8002() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('dropzonejs.settings');
$config->clear('filename_transliteration');
$config->set('filename_transliteration', TRUE);
$config->save(TRUE);
}
......@@ -44,11 +44,11 @@ class UploadHandler implements UploadHandlerInterface {
protected $languageManager;
/**
* The scheme (stream wrapper) used to store uploaded files.
* The settings of dropzonejs.
*
* @var string
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $tmpUploadScheme;
protected $dropzoneSettings;
/**
* Constructs dropzone upload controller route controller.
......@@ -66,7 +66,7 @@ class UploadHandler implements UploadHandlerInterface {
$this->request = $request_stack->getCurrentRequest();
$this->transliteration = $transliteration;
$this->languageManager = $language_manager;
$this->tmpUploadScheme = $config_factory->get('dropzonejs.settings')->get('tmp_upload_scheme');
$this->dropzoneSettings = $config_factory->get('dropzonejs.settings');
}
/**
......@@ -81,6 +81,10 @@ class UploadHandler implements UploadHandlerInterface {
throw new UploadException(UploadException::FILENAME_ERROR);
}
if (!$this->dropzoneSettings->get('filename_transliteration')) {
return $original_name . '.txt';
}
// @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.
......@@ -100,9 +104,7 @@ class UploadHandler implements UploadHandlerInterface {
// 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 = $filename . '.txt';
return $processed_filename;
return $filename . '.txt';
}
/**
......@@ -136,7 +138,7 @@ class UploadHandler implements UploadHandlerInterface {
}
// Open temp file.
$tmp = $this->tmpUploadScheme . '://' . $this->getFilename($file);
$tmp = $this->dropzoneSettings->get('tmp_upload_scheme') . '://' . $this->getFilename($file);
if (!($out = fopen($tmp, $this->request->request->get('chunk', 0) ? 'ab' : 'wb'))) {
throw new UploadException(UploadException::OUTPUT_ERROR);
}
......
......@@ -98,4 +98,36 @@ class DropzoneJsUploadControllerTest extends KernelTestBase {
$this->assertEquals(file_get_contents($result_file), $this->testfileData);
}
/**
* Tests that dropzoneJs ignores filename transliteration.
*/
public function testIgnoreTransliteration() {
$this->container->get('router.builder')->rebuild();
$language = ConfigurableLanguage::createFromLangcode('zh-hans');
$language->save();
$this->config('system.site')->set('default_langcode', $language->getId())->save();
$this->config('dropzonejs.settings')->set('filename_transliteration', FALSE)->save();
// The filename should be expected as it is.
$chinese_with_emoji_fileanme_without_extension = '中文😁';
$uploaded_file = new UploadedFile($this->tmpFile, "{$this->testfilePrefix}${chinese_with_emoji_fileanme_without_extension}.jpg");
$file_bag = new FileBag();
$file_bag->set('file', $uploaded_file);
$request = new Request();
$request->files = $file_bag;
$upload_handler = $this->container->get('dropzonejs.upload_handler');
$controller = new UploadController($upload_handler, $request);
$controller_result = $controller->handleUploads();
$this->assertInstanceOf(JsonResponse::class, $controller_result);
$result = json_decode($controller_result->getContent());
$result_file = $this->filesDir . '/' . $result->result;
$this->assertStringEndsWith($chinese_with_emoji_fileanme_without_extension . '.jpg.txt', $result_file);
$this->assertFileExists($result_file);
$this->assertStringEqualsFile($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