[TASK] Rename ResourceStorage::processUploadedFilename to sanitizeFileName 16/42116/9
authorFrans Saris <franssaris@gmail.com>
Wed, 29 Jul 2015 18:49:24 +0000 (20:49 +0200)
committerFrans Saris <franssaris@gmail.com>
Fri, 31 Jul 2015 11:38:29 +0000 (13:38 +0200)
Feature #67545 introduced ResourceStorage::processUploadedFilename and
changed the existing PreFileAddSignal. The name of the newly introduced
method doesn't really fit its functionality and using the same signal
for
two different purposes is also not correct.

This change renames the method
processUploadedFilename() to sanitizeFileName()
and introduces the SanitizeFileNameSignal.
The changes made to PreFileAddSignal are reverted.

Resolves: #68600
Related: #67545
Releases: master
Change-Id: Iabeffe5b8b3fe85a4f49b9341a45b2a026dd241c
Reviewed-on: http://review.typo3.org/42116
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Tested-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Tested-by: Frans Saris <franssaris@gmail.com>
typo3/sysext/backend/Classes/Controller/File/FileController.php
typo3/sysext/core/Classes/Resource/ResourceStorage.php
typo3/sysext/core/Classes/Resource/ResourceStorageInterface.php
typo3/sysext/core/Documentation/Changelog/master/Feature-68600-IntroducedResourceStorageSanitizeFileNameSignal.rst [new file with mode: 0644]
typo3/sysext/core/Documentation/Changelog/master/Important-68600-IntroducedResourceStorageSanitizeFileNameSignal.rst [new file with mode: 0644]

index ae9c050..756ccc8 100644 (file)
@@ -228,7 +228,7 @@ class FileController {
                $fileFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
                /** @var Folder $fileTargetObject */
                $fileTargetObject = $fileFactory->retrieveFileOrFolderObject($fileTarget);
-               $processedFileName = $fileTargetObject->getStorage()->processUploadedFileName($fileTargetObject, $fileName);
+               $processedFileName = $fileTargetObject->getStorage()->sanitizeFileName($fileName, $fileTargetObject);
 
                $result = FALSE;
                if ($fileTargetObject->hasFile($processedFileName)) {
index 8a156d1..846edb3 100644 (file)
@@ -1054,26 +1054,23 @@ class ResourceStorage implements ResourceStorageInterface {
        }
 
        /**
-        * Process uploaded file name
+        * Clean a fileName from not allowed characters
         *
+        * @param string $fileName The name of the file to be add, If not set, the local file name is used
         * @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) {
-
+       public function sanitizeFileName($fileName, Folder $targetFolder = NULL) {
                $targetFolder = $targetFolder ?: $this->getDefaultFolder();
-               $targetFileName = $this->driver->sanitizeFileName($targetFileName);
-
-               $this->assureFileAddPermissions($targetFolder, $targetFileName);
+               $fileName = $this->driver->sanitizeFileName($fileName);
 
                // The file name could be changed by an external slot
-               $targetFileName = $this->emitPreFileAddSignal($targetFileName, $targetFolder);
+               $fileName = $this->emitSanitizeFileNameSignal($fileName, $targetFolder);
 
-               return $targetFileName;
+               return $fileName;
        }
 
 
@@ -1098,14 +1095,12 @@ class ResourceStorage implements ResourceStorageInterface {
                        throw new \InvalidArgumentException('File "' . $localFilePath . '" does not exist.', 1319552745);
                }
                $targetFolder = $targetFolder ?: $this->getDefaultFolder();
-               $targetFileName = $this->driver->sanitizeFileName($targetFileName ?: PathUtility::basename($localFilePath));
+               $targetFileName = $this->sanitizeFileName($targetFileName ?: PathUtility::basename($localFilePath), $targetFolder);
 
-               $this->assureFileAddPermissions($targetFolder, $targetFileName);
-
-               // 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
                $targetFileName = $this->emitPreFileAddSignal($targetFileName, $targetFolder, $localFilePath);
 
+               $this->assureFileAddPermissions($targetFolder, $targetFileName);
+
                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') {
@@ -2255,6 +2250,18 @@ class ResourceStorage implements ResourceStorageInterface {
        }
 
        /**
+        * Emits sanitize fileName signal.
+        *
+        * @param string $fileName
+        * @param Folder $targetFolder
+        * @return string Modified target file name
+        */
+       protected function emitSanitizeFileNameSignal($fileName, Folder $targetFolder) {
+               list($fileName) = $this->getSignalSlotDispatcher()->dispatch(ResourceStorage::class, self::SIGNAL_SanitizeFileName, array($fileName, $targetFolder, $this, $this->driver));
+               return $fileName;
+       }
+
+       /**
         * Emits file pre-add signal.
         *
         * @param string $targetFileName
@@ -2262,7 +2269,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 8175c7d..17a4a5e 100644 (file)
@@ -20,6 +20,7 @@ namespace TYPO3\CMS\Core\Resource;
  */
 interface ResourceStorageInterface {
 
+       const SIGNAL_SanitizeFileName = 'sanitizeFileName';
        const SIGNAL_PreFileAdd = 'preFileAdd';
        const SIGNAL_PostFileAdd = 'postFileAdd';
        const SIGNAL_PostFileCreate = 'postFileCreate';
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-68600-IntroducedResourceStorageSanitizeFileNameSignal.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-68600-IntroducedResourceStorageSanitizeFileNameSignal.rst
new file mode 100644 (file)
index 0000000..6ac1f65
--- /dev/null
@@ -0,0 +1,20 @@
+====================================================================
+Feature: #68600 - Introduced ResourceStorage SanitizeFileName signal
+====================================================================
+
+Description
+===========
+
+In order to check whether an uploaded/newly added file already exists before uploading it or to ask for
+user preferences about already existing files only when needed, the final name for the uploaded file is needed.
+
+In order to let extensions do custom sanitizing of a file name the signal ``sanitizeFileName`` is introduced in
+``TYPO3\CMS\Core\Resource\ResourceStorage``.
+The signal is emitted when ``ResourceStorage::sanitizeFileName`` or ``ResourceStorage::addFile`` are called.
+
+
+Impact
+======
+
+All installations with extensions that use the PreFileAdd signal to change/sanitize a file name.
+This logic should be moved to the new sanitizeFileName signal.
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Important-68600-IntroducedResourceStorageSanitizeFileNameSignal.rst b/typo3/sysext/core/Documentation/Changelog/master/Important-68600-IntroducedResourceStorageSanitizeFileNameSignal.rst
new file mode 100644 (file)
index 0000000..e35637b
--- /dev/null
@@ -0,0 +1,21 @@
+======================================================================
+Important: #68600 - Introduced ResourceStorage SanitizeFileName signal
+======================================================================
+
+Description
+===========
+
+In order to check whether an uploaded/newly added file already exists before uploading it or to ask for
+user preferences about already existing files only when needed, the final name for the uploaded file is needed.
+
+Before #68600 the PreFileAdd signal was documented to have the ability to change the ``$targetFileName``,
+but the signal expects the local file path of the file. Since this information isn't available when checking
+only by a file name if a file already exists, a new signal has been added to ``TYPO3\CMS\Core\Resource\ResourceStorage``,
+which is emitted when the ResourceStorage is asked to sanitize a file name.
+
+
+Affected Installations
+======================
+
+All installations with extensions that use the PreFileAdd signal to change/sanitize a file name.
+This logic should be moved to the new sanitizeFileName signal.