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: ...@@ -2,6 +2,6 @@ dropzonejs.settings:
type: config_object type: config_object
label: 'DropzoneJS settings' label: 'DropzoneJS settings'
mapping: mapping:
tmp_dir: tmp_upload_scheme:
type: string type: string
label: 'Temporary directory location' label: 'Upload scheme'
...@@ -40,3 +40,14 @@ function dropzonejs_requirements($phase) { ...@@ -40,3 +40,14 @@ function dropzonejs_requirements($phase) {
return $requirements; 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 { ...@@ -134,13 +134,12 @@ class DropzoneJs extends FormElement {
if (!empty($user_input['uploaded_files'])) { if (!empty($user_input['uploaded_files'])) {
$file_names = array_filter(explode(';', $user_input['uploaded_files'])); $file_names = array_filter(explode(';', $user_input['uploaded_files']));
$tmp_override = \Drupal::config('dropzonejs.settings')->get('tmp_dir'); $tmp_upload_scheme = \Drupal::configFactory()->get('dropzonejs.settings')->get('tmp_upload_scheme');
$temp_path = ($tmp_override) ? $tmp_override : \Drupal::config('system.file')->get('path.temporary');
foreach ($file_names as $name) { foreach ($file_names as $name) {
// The upload handler appended the txt extension to the file for // The upload handler appended the txt extension to the file for
// security reasons. We will remove it in this callback. // 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 // The upload handler appended the txt extension to the file for
// security reasons. Because here we know the acceptable extensions // security reasons. Because here we know the acceptable extensions
...@@ -152,7 +151,7 @@ class DropzoneJs extends FormElement { ...@@ -152,7 +151,7 @@ class DropzoneJs extends FormElement {
// we still have to move. // we still have to move.
if (file_exists($old_filepath)) { if (file_exists($old_filepath)) {
// Finaly rename the file and add it to results. // 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); $move_result = file_unmanaged_move($old_filepath, $new_filepath);
if ($move_result) { if ($move_result) {
......
...@@ -27,17 +27,6 @@ class UploadHandler implements UploadHandlerInterface { ...@@ -27,17 +27,6 @@ class UploadHandler implements UploadHandlerInterface {
*/ */
protected $request; 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. * Transliteration service.
* *
...@@ -52,39 +41,30 @@ class UploadHandler implements UploadHandlerInterface { ...@@ -52,39 +41,30 @@ class UploadHandler implements UploadHandlerInterface {
*/ */
protected $languageManager; protected $languageManager;
/**
* The scheme (stream wrapper) used to store uploaded files.
*
* @var string
*/
protected $tmpUploadScheme;
/** /**
* Constructs dropzone upload controller route controller. * Constructs dropzone upload controller route controller.
* *
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack. * The request stack.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Config factory. * Config factory.
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration * @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
* Transliteration service. * Transliteration service.
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* Transliteration service. * 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(); $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->transliteration = $transliteration;
$this->languageManager = $language_manager; $this->languageManager = $language_manager;
} $this->tmpUploadScheme = $config_factory->get('dropzonejs.settings')->get('tmp_upload_scheme');
/**
* 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);
} }
/** /**
...@@ -127,7 +107,6 @@ class UploadHandler implements UploadHandlerInterface { ...@@ -127,7 +107,6 @@ class UploadHandler implements UploadHandlerInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handleUpload(UploadedFile $file) { public function handleUpload(UploadedFile $file) {
$this->prepareTemporaryUploadDestination();
$error = $file->getError(); $error = $file->getError();
if ($error != UPLOAD_ERR_OK) { if ($error != UPLOAD_ERR_OK) {
...@@ -155,7 +134,7 @@ class UploadHandler implements UploadHandlerInterface { ...@@ -155,7 +134,7 @@ class UploadHandler implements UploadHandlerInterface {
} }
// Open temp file. // 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'))) { if (!($out = fopen($tmp, $this->request->request->get('chunk', 0) ? 'ab' : 'wb'))) {
throw new UploadException(UploadException::OUTPUT_ERROR); throw new UploadException(UploadException::OUTPUT_ERROR);
} }
......
...@@ -58,6 +58,7 @@ class DropzoneJsUploadControllerTest extends KernelTestBase { ...@@ -58,6 +58,7 @@ class DropzoneJsUploadControllerTest extends KernelTestBase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->installSchema('system', 'router'); $this->installSchema('system', 'router');
$this->installConfig('dropzonejs');
$this->installEntitySchema('user'); $this->installEntitySchema('user');
$this->filesDir = $this->siteDirectory . '/files'; $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