[!!!][FEATURE] Ajax call to check if file already exists 10/40310/12
authorAlina Fleser <afleser@arxia.com>
Tue, 16 Jun 2015 14:33:18 +0000 (17:33 +0300)
committerAndreas Fernandez <typo3@scripting-base.de>
Thu, 16 Jul 2015 07:55:41 +0000 (09:55 +0200)
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 <franssaris@gmail.com>
Tested-by: Frans Saris <franssaris@gmail.com>
Reviewed-by: Xavier Perseguers <xavier@typo3.org>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
typo3/sysext/backend/Classes/Controller/File/FileController.php
typo3/sysext/core/Classes/Resource/ResourceStorage.php
typo3/sysext/core/Configuration/DefaultConfiguration.php
typo3/sysext/core/Documentation/Changelog/master/Breaking-67545-PreFileAddSignalBehaviourChanged.rst [new file with mode: 0644]
typo3/sysext/core/Documentation/Changelog/master/Feature-67545-AJAXMethodToCheckIfFileExists.rst [new file with mode: 0644]

index b869eb1..dea3e41 100644 (file)
@@ -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();
@@ -196,6 +197,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
         *
         * The value can be a File, Folder or boolean
index 193af25..b2935e5 100644 (file)
@@ -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;
        }
index a6e84b8..999d629 100644 (file)
@@ -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
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Breaking-67545-PreFileAddSignalBehaviourChanged.rst b/typo3/sysext/core/Documentation/Changelog/master/Breaking-67545-PreFileAddSignalBehaviourChanged.rst
new file mode 100644 (file)
index 0000000..e722ef1
--- /dev/null
@@ -0,0 +1,24 @@
+======================================================
+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
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-67545-AJAXMethodToCheckIfFileExists.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-67545-AJAXMethodToCheckIfFileExists.rst
new file mode 100644 (file)
index 0000000..784af44
--- /dev/null
@@ -0,0 +1,18 @@
+========================================================
+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