Commit 449af843 authored by chr.fritsch's avatar chr.fritsch Committed by chr.fritsch

Issue #2724149 by chr.fritsch, joehoppe, phenaproxima: The upload handler...

Issue #2724149 by chr.fritsch, joehoppe, phenaproxima: The upload handler should use stream wrappers
parent 2f2f9f5c
tmp_dir: ''
tmp_upload_scheme: temporary
......@@ -2,6 +2,6 @@ dropzonejs.settings:
type: config_object
label: 'DropzoneJS settings'
mapping:
tmp_dir:
tmp_upload_scheme:
type: string
label: 'Temporary directory location'
label: 'Upload scheme'
......@@ -40,3 +40,14 @@ function dropzonejs_requirements($phase) {
return $requirements;
}
/**
* The upload handler should use stream wrappers.
*/
function dropzonejs_update_8001() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('dropzonejs.settings');
$config->clear('tmp_dir');
$config->set('tmp_upload_scheme', 'temporary');
$config->save(TRUE);
}
......@@ -134,13 +134,12 @@ class DropzoneJs extends FormElement {
if (!empty($user_input['uploaded_files'])) {
$file_names = array_filter(explode(';', $user_input['uploaded_files']));
$tmp_override = \Drupal::config('dropzonejs.settings')->get('tmp_dir');
$temp_path = ($tmp_override) ? $tmp_override : \Drupal::config('system.file')->get('path.temporary');
$tmp_upload_scheme = \Drupal::configFactory()->get('dropzonejs.settings')->get('tmp_upload_scheme');
foreach ($file_names as $name) {
// The upload handler appended the txt extension to the file for
// security reasons. We will remove it in this callback.
$old_filepath = "$temp_path/$name";
$old_filepath = $tmp_upload_scheme . '://' . $name;
// The upload handler appended the txt extension to the file for
// security reasons. Because here we know the acceptable extensions
......@@ -152,7 +151,7 @@ class DropzoneJs extends FormElement {
// we still have to move.
if (file_exists($old_filepath)) {
// Finaly rename the file and add it to results.
$new_filepath = "$temp_path/$name";
$new_filepath = $tmp_upload_scheme . '://' . $name;
$move_result = file_unmanaged_move($old_filepath, $new_filepath);
if ($move_result) {
......
......@@ -27,17 +27,6 @@ class UploadHandler implements UploadHandlerInterface {
*/
protected $request;
/**
* Stores temporary folder URI.
*
* This is configurable via the configuration variable. It was added for HA
* environments where temporary location may need to be a shared across all
* servers.
*
* @var string
*/
protected $temporaryUploadLocation;
/**
* Transliteration service.
*
......@@ -52,39 +41,30 @@ class UploadHandler implements UploadHandlerInterface {
*/
protected $languageManager;
/**
* The scheme (stream wrapper) used to store uploaded files.
*
* @var string
*/
protected $tmpUploadScheme;
/**
* Constructs dropzone upload controller route controller.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Config factory.
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
* Transliteration service.
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
* Transliteration service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* LanguageManager service.
*/
public function __construct(RequestStack $request_stack, ConfigFactoryInterface $config, TransliterationInterface $transliteration, LanguageManagerInterface $language_manager) {
public function __construct(RequestStack $request_stack, ConfigFactoryInterface $config_factory, 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;
}
/**
* Prepares temporary destination folder for uploaded files.
*
* @throws \Drupal\dropzonejs\UploadException
*/
protected function prepareTemporaryUploadDestination() {
$writable = file_prepare_directory($this->temporaryUploadLocation, FILE_CREATE_DIRECTORY);
if (!$writable) {
throw new UploadException(UploadException::DESTINATION_FOLDER_ERROR);
}
// Try to make sure this is private via htaccess.
file_save_htaccess($this->temporaryUploadLocation, TRUE);
$this->tmpUploadScheme = $config_factory->get('dropzonejs.settings')->get('tmp_upload_scheme');
}
/**
......@@ -127,7 +107,6 @@ class UploadHandler implements UploadHandlerInterface {
* {@inheritdoc}
*/
public function handleUpload(UploadedFile $file) {
$this->prepareTemporaryUploadDestination();
$error = $file->getError();
if ($error != UPLOAD_ERR_OK) {
......@@ -155,7 +134,7 @@ class UploadHandler implements UploadHandlerInterface {
}
// Open temp file.
$tmp = "{$this->temporaryUploadLocation}/{$this->getFilename($file)}";
$tmp = $this->tmpUploadScheme . '://' . $this->getFilename($file);
if (!($out = fopen($tmp, $this->request->request->get('chunk', 0) ? 'ab' : 'wb'))) {
throw new UploadException(UploadException::OUTPUT_ERROR);
}
......
......@@ -58,6 +58,7 @@ class DropzoneJsUploadControllerTest extends KernelTestBase {
protected function setUp() {
parent::setUp();
$this->installSchema('system', 'router');
$this->installConfig('dropzonejs');
$this->installEntitySchema('user');
$this->filesDir = $this->siteDirectory . '/files';
......
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