[BUGFIX] ResourceStorage::SIGNAL_PreFileAdd should be triggered earlier 99/28099/4
authorXavier Perseguers <xavier@typo3.org>
Thu, 6 Mar 2014 10:43:16 +0000 (11:43 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 8 Mar 2014 16:32:46 +0000 (17:32 +0100)
The PreFileAdd signal should be triggered before the file exist check.
So the name and/or content can be manipulated before the target filename
is checked.

Change-Id: I0bbe32a4efa395a99278c480210310bd7466f259
Fixes: #56394
Releases: 6.2
Reviewed-on: https://review.typo3.org/28099
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/core/Classes/Resource/ResourceStorage.php

index 18d36fb..9284cf5 100644 (file)
@@ -1106,14 +1106,16 @@ class ResourceStorage {
                $this->assureFileAddPermissions($localFilePath, $targetFolder, $targetFileName);
                $targetFolder = $targetFolder ?: $this->getDefaultFolder();
                $targetFileName = $this->driver->sanitizeFileName($targetFileName ?: PathUtility::basename($localFilePath));
+
+               // We do not care whether the file exists yet because $targetFileName may be changed by an
+               // external slot and only then we should check how to proceed according to $conflictMode
+               $this->emitPreFileAddSignal($targetFileName, $targetFolder, $localFilePath);
+
                if ($conflictMode === 'cancel' && $this->driver->fileExistsInFolder($targetFileName, $targetFolder->getIdentifier())) {
                        throw new Exception\ExistingTargetFileNameException('File "' . $targetFileName . '" already exists in folder ' . $targetFolder->getIdentifier(), 1322121068);
                } elseif ($conflictMode === 'changeName') {
                        $targetFileName = $this->getUniqueName($targetFolder, $targetFileName);
                }
-               // We do not care whether the file exists if $conflictMode is "replace",
-               // so just use the name as is in that case
-               $this->emitPreFileAddSignal($targetFileName, $targetFolder);
 
                $fileIdentifier = $this->driver->addFile($localFilePath, $targetFolder->getIdentifier(), $targetFileName);
                $file = ResourceFactory::getInstance()->getFileObjectByStorageAndIdentifier($this->getUid(), $fileIdentifier);
@@ -2094,12 +2096,13 @@ class ResourceStorage {
        /**
         * Emits file pre-add signal
         *
-        * @param string $fileName
+        * @param string $targetFileName
         * @param Folder $targetFolder
+        * @param string $sourceFilePath
         * @return void
         */
-       protected function emitPreFileAddSignal($fileName, Folder $targetFolder) {
-               $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', self::SIGNAL_PreFileAdd, array($fileName, $targetFolder));
+       protected function emitPreFileAddSignal(&$targetFileName, Folder $targetFolder, $sourceFilePath) {
+               $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', self::SIGNAL_PreFileAdd, array(&$targetFileName, $targetFolder, $sourceFilePath, $this, $this->driver));
        }
 
        /**