Commit 9ab04864 authored by Alina Fleser's avatar Alina Fleser Committed by Andreas Fernandez
Browse files

[!!!][FEATURE] Ajax call to check if file already exists

To check if a file already exist we want prior to realuploding uploading
be able to verify the posible file existince using a ajax request.

The ajax request checks the final file name, considering possible
alterations of the name, like sanitization and PreFileAdd signal.

Resolves: #67545
Releases: master
Change-Id: I850694e0d065b674634437bbe51fbeee840c6e39
Reviewed-on: http://review.typo3.org/40310


Reviewed-by: Frans Saris's avatarFrans Saris <franssaris@gmail.com>
Tested-by: Frans Saris's avatarFrans Saris <franssaris@gmail.com>
Reviewed-by: Xavier Perseguers's avatarXavier Perseguers <xavier@typo3.org>
Reviewed-by: Andreas Fernandez's avatarAndreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez's avatarAndreas Fernandez <typo3@scripting-base.de>
parent d28bb427
......@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Backend\Controller\File;
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Http\AjaxRequestHandler;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Backend\Utility\BackendUtility;
......@@ -165,10 +166,10 @@ class FileController {
* actual return value
*
* @param array $params Always empty.
* @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj The AjaxRequestHandler object used to return content and set content types
* @param AjaxRequestHandler $ajaxObj The AjaxRequestHandler object used to return content and set content types
* @return void
*/
public function processAjaxRequest(array $params, \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj) {
public function processAjaxRequest(array $params, AjaxRequestHandler $ajaxObj) {
$this->init();
$this->main();
$errors = $this->fileProcessor->getErrorMessages();
......@@ -195,6 +196,26 @@ class FileController {
}
}
/**
* Ajax entry point to check if a file exists in a folder
*
* @param array $params Always empty.
* @param AjaxRequestHandler $ajaxObj The AjaxRequestHandler object used to return content and set content types
* @return void
*/
public function fileExistsAjaxRequest(array $params, AjaxRequestHandler $ajaxObj) {
$fileName = GeneralUtility::_GP('fileName');
$fileTarget = GeneralUtility::_GP('fileTarget');
/** @var \TYPO3\CMS\Core\Resource\ResourceFactory $fileFactory */
$fileFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
$fileTargetObject = $fileFactory->retrieveFileOrFolderObject($fileTarget);
$processedFileName = $fileTargetObject->getStorage()->processUploadedFileName($fileTargetObject, $fileName);
$ajaxObj->addContent('result', $fileTargetObject->hasFile($processedFileName));
$ajaxObj->setContentFormat('json');
}
/**
* Flatten result value from FileProcessor
*
......
......@@ -1053,6 +1053,30 @@ class ResourceStorage implements ResourceStorageInterface {
}
}
/**
* Process uploaded file name
*
* @param Folder $targetFolder The target folder where the file should be added
* @param string $targetFileName The name of the file to be add, If not set, the local file name is used
*
* @throws \InvalidArgumentException
* @throws Exception\ExistingTargetFileNameException
* @return FileInterface
*/
public function processUploadedFileName(Folder $targetFolder, $targetFileName) {
$targetFolder = $targetFolder ?: $this->getDefaultFolder();
$targetFileName = $this->driver->sanitizeFileName($targetFileName);
$this->assureFileAddPermissions($targetFolder, $targetFileName);
// The file name could be changed by an external slot
$targetFileName = $this->emitPreFileAddSignal($targetFileName, $targetFolder);
return $targetFileName;
}
/********************
* FILE ACTIONS
********************/
......@@ -2238,7 +2262,7 @@ class ResourceStorage implements ResourceStorageInterface {
* @param string $sourceFilePath
* @return string Modified target file name
*/
protected function emitPreFileAddSignal($targetFileName, Folder $targetFolder, $sourceFilePath) {
protected function emitPreFileAddSignal($targetFileName, Folder $targetFolder, $sourceFilePath = '') {
$this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Core\Resource\ResourceStorage::class, self::SIGNAL_PreFileAdd, array(&$targetFileName, $targetFolder, $sourceFilePath, $this, $this->driver));
return $targetFileName;
}
......
......@@ -650,6 +650,10 @@ return array(
'callbackMethod' => \TYPO3\CMS\Backend\Controller\File\FileController::class . '->processAjaxRequest',
'csrfTokenCheck' => TRUE
),
'TYPO3_tcefile::fileExists' => array(
'callbackMethod' => \TYPO3\CMS\Backend\Controller\File\FileController::class . '->fileExistsAjaxRequest',
'csrfTokenCheck' => TRUE
),
't3lib_TCEforms_inline::createNewRecord' => array(
'callbackMethod' => \TYPO3\CMS\Backend\Form\FormEngine::class . '->processInlineAjaxRequest',
'csrfTokenCheck' => TRUE
......
======================================================
Breaking: #67545 - PreFileAdd signal behaviour changed
======================================================
Description
===========
In order to check whether an uploaded file exists already before uploading it or
to ask about user preferences about already existent files only when needed,
the final name of the file was need. As the final name can be altered by PreFileAdd signal,
which originally always received the temporary uploaded file path as parameter. But as this isn't
available in the file name check $sourceFilePath will be an empty string then.
Impact
======
All PreFileAdd slot methods that depend on the $sourceFilePath param must be adapted to handle the new empty string value.
Affected Installations
======================
All extensions that use the PreFileAdd signal and depend on the $sourceFilePath param.
\ No newline at end of file
========================================================
Feature: #67545 - AJAX call to check whether file exists
========================================================
Description
===========
An BE AJAX call to check whether a file exists has been added. The call needs two parameters to work properly.
Impact
======
The method can be called with ``TYPO3.settings.ajaxUrls['TYPO3_tcefile::fileExists']``.
The parameters ``fileName`` and ``fileTarget`` are required:
- fileName: Name of the file
- fileTarget: Combined identifier of target directory for the file
\ No newline at end of file
Supports Markdown
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