[FEATURE] Make file conflict mode available for BE file upload 38/41338/6
authorFrans Saris <franssaris@gmail.com>
Wed, 15 Jul 2015 19:38:43 +0000 (21:38 +0200)
committerBenjamin Mack <benni@typo3.org>
Thu, 16 Jul 2015 08:43:21 +0000 (10:43 +0200)
Make is possible to set conflict mode to changeName instead of only
being able to override of chancel when uploading files from backend.

Release: master
Resolves: #63603
Change-Id: I5019337b547d70c99ff4f7a8e8e8fbae2dc1ed8b
Reviewed-on: http://review.typo3.org/41338
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Benjamin Mack <benni@typo3.org>
Tested-by: Benjamin Mack <benni@typo3.org>
typo3/sysext/backend/Classes/Controller/File/FileController.php
typo3/sysext/backend/Classes/Controller/File/FileUploadController.php
typo3/sysext/backend/Classes/Controller/File/ReplaceFileController.php
typo3/sysext/backend/Resources/Public/JavaScript/DragUploader.js
typo3/sysext/core/Classes/Utility/File/ExtendedFileUtility.php
typo3/sysext/core/Documentation/Changelog/master/Deprecation-63603-ExtendedFileUtilitydontCheckForUniqueIsDeprecated.rst [new file with mode: 0644]
typo3/sysext/core/Documentation/Changelog/master/Deprecation-63603-FileControllerAndFileListControllerOverwriteExistingFilesChangedToStringValue.rst [new file with mode: 0644]
typo3/sysext/filelist/Classes/Controller/FileListController.php
typo3/sysext/impexp/Classes/Controller/ImportExportController.php

index 3157bed..ae9c050 100644 (file)
@@ -18,6 +18,7 @@ use TYPO3\CMS\Core\Http\AjaxRequestHandler;
 use TYPO3\CMS\Core\Resource\Folder;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Utility\File\ExtendedFileUtility;
 
 /**
  * Gateway for TCE (TYPO3 Core Engine) file-handling through POST forms.
@@ -43,9 +44,9 @@ class FileController {
        protected $CB;
 
        /**
-        * If existing files should be overridden.
+        * Defines behaviour when uploading files with names that already exist; possible value are 'cancel', 'replace', 'changeName'
         *
-        * @var bool
+        * @var string
         */
        protected $overwriteExistingFiles;
 
@@ -68,7 +69,7 @@ class FileController {
         * Internal, dynamic:
         * File processor object
         *
-        * @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
+        * @var ExtendedFileUtility
         */
        protected $fileProcessor;
 
@@ -97,10 +98,16 @@ class FileController {
                $this->file = GeneralUtility::_GP('file');
                $this->CB = GeneralUtility::_GP('CB');
                $this->overwriteExistingFiles = GeneralUtility::_GP('overwriteExistingFiles');
+
+               if ((string)$this->overwriteExistingFiles === '1') {
+                       GeneralUtility::deprecationLog('overwriteExitingFiles = 1 is deprecated. Use overwriteExitingFiles = "replace". Support for old behavior will be removed in TYPO3 CMS 8.');
+                       $this->overwriteExistingFiles = 'replace';
+               }
+
                $this->vC = GeneralUtility::_GP('vC');
                $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
                $this->initClipboard();
-               $this->fileProcessor = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class);
+               $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
        }
 
        /**
@@ -133,7 +140,16 @@ class FileController {
                // Initializing:
                $this->fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
                $this->fileProcessor->setActionPermissions();
-               $this->fileProcessor->dontCheckForUnique = $this->overwriteExistingFiles ? 1 : 0;
+               switch ($this->overwriteExistingFiles) {
+                       case 'replace':
+                       case 'changeName':
+                               $conflictMode = $this->overwriteExistingFiles;
+                               break;
+                       default:
+                               $conflictMode = 'cancel';
+                               break;
+               }
+               $this->fileProcessor->setExistingFilesConflictMode($conflictMode);
                // Checking referrer / executing:
                $refInfo = parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
                $httpHost = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
index e25604e..f509c49 100644 (file)
@@ -150,7 +150,7 @@ class FileUploadController {
                // Make checkbox for "overwrite"
                $content = '
                        <div id="c-override">
-                               <p><label for="overwriteExistingFiles"><input type="checkbox" class="checkbox" name="overwriteExistingFiles" id="overwriteExistingFiles" value="1" /> ' . $this->getLanguageService()->getLL('overwriteExistingFiles', 1) . '</label></p>
+                               <p><label for="overwriteExistingFiles"><input type="checkbox" class="checkbox" name="overwriteExistingFiles" id="overwriteExistingFiles" value="replace" /> ' . $this->getLanguageService()->getLL('overwriteExistingFiles', 1) . '</label></p>
                                <p>&nbsp;</p>
                                <p>' . $this->getLanguageService()->getLL('uploadMultipleFilesInfo', TRUE) . '</p>
                        </div>
index b50d22a..a19a7a6 100644 (file)
@@ -165,7 +165,7 @@ class ReplaceFileController {
                        });
                        </script>
 
-                       <input type="hidden" name="overwriteExistingFiles" value="1" />
+                       <input type="hidden" name="overwriteExistingFiles" value="replace" />
                        <input type="hidden" name="file[replace][1][data]" value="1" />
                        <input type="hidden" name="file[replace][1][uid]" value="' . $this->uid . '" />
                ';
index 9800e84..8da088f 100644 (file)
@@ -358,7 +358,7 @@ define('TYPO3/CMS/Backend/DragUploader', ['jquery', 'TYPO3/CMS/Lang/Lang'], func
                        formData.append('file[upload][1][target]', me.dragUploader.target);
                        formData.append('file[upload][1][data]', '1');
                        if(me.override) {
-                               formData.append('overwriteExistingFiles', '1');
+                               formData.append('overwriteExistingFiles', 'replace');
                        }
                        formData.append('redirect', '');
                        formData.append('upload_1', me.file);
index d7f90e5..07ecc6a 100644 (file)
@@ -18,6 +18,7 @@ use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Backend\Utility\IconUtility;
 use TYPO3\CMS\Core\Messaging\FlashMessage;
 use TYPO3\CMS\Core\Messaging\FlashMessageService;
+use TYPO3\CMS\Core\Resource\Exception;
 use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
 use TYPO3\CMS\Core\Resource\File;
 use TYPO3\CMS\Core\Resource\Folder;
@@ -67,10 +68,18 @@ class ExtendedFileUtility extends BasicFileUtility {
         * If set, the uploaded files will overwrite existing files.
         *
         * @var bool
+        * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8
         */
        public $dontCheckForUnique = 0;
 
        /**
+        * Defines behaviour when uploading files with names that already exist; possible value are 'cancel', 'replace', 'changeName'
+        *
+        * @var string
+        */
+       protected $existingFilesConflictMode;
+
+       /**
         * This array is self-explaining (look in the class below).
         * It grants access to the functions. This could be set from outside in order to enabled functions to users.
         * See also the function setActionPermissions() which takes input directly from the user-record
@@ -137,6 +146,28 @@ class ExtendedFileUtility extends BasicFileUtility {
        protected $fileFactory;
 
        /**
+        * Get existingFilesConflictMode
+        *
+        * @return string
+        */
+       public function getExistingFilesConflictMode() {
+               return $this->existingFilesConflictMode;
+       }
+
+       /**
+        * Set existingFilesConflictMode
+        *
+        * @param string $existingFilesConflictMode
+        * @throws Exception
+        */
+       public function setExistingFilesConflictMode($existingFilesConflictMode) {
+               if (!in_array($existingFilesConflictMode, array('cancel', 'replace', 'changeName'))) {
+                       throw new Exception(sprintf('Invalid argument, received: "%s", expected: "cancel", "replace" or "changeName"', $existingFilesConflictMode));
+               }
+               $this->existingFilesConflictMode = $existingFilesConflictMode;
+       }
+
+       /**
         * Initialization of the class
         *
         * @param array $fileCmds Array with the commands to execute. See "TYPO3 Core API" document
@@ -938,16 +969,16 @@ class ExtendedFileUtility extends BasicFileUtility {
                                'size' => $uploadedFileData['size'][$i]
                        );
                        try {
-                               // @todo can be improved towards conflict mode naming
-                               if ($this->dontCheckForUnique) {
-                                       $conflictMode = 'replace';
-                               } else {
-                                       $conflictMode = 'cancel';
+
+                               if ((int)$this->dontCheckForUnique === 1) {
+                                       GeneralUtility::deprecationLog('dontCheckForUnique = 1 is deprecated. Use setExistingFilesConflictMode(\'replace\');. Support for dontCheckForUnique will be removed in TYPO3 CMS 8.');
+                                       $this->existingFilesConflictMode = 'replace';
                                }
+
                                /** @var $fileObject File */
-                               $fileObject = $targetFolderObject->addUploadedFile($fileInfo, $conflictMode);
+                               $fileObject = $targetFolderObject->addUploadedFile($fileInfo, $this->existingFilesConflictMode);
                                $fileObject = ResourceFactory::getInstance()->getFileObjectByStorageAndIdentifier($targetFolderObject->getStorage()->getUid(), $fileObject->getIdentifier());
-                               if ($conflictMode === 'replace') {
+                               if ($this->existingFilesConflictMode === 'replace') {
                                        $this->getIndexer($fileObject->getStorage())->updateIndexEntry($fileObject);
                                }
                                $resultObjects[] = $fileObject;
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-63603-ExtendedFileUtilitydontCheckForUniqueIsDeprecated.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-63603-ExtendedFileUtilitydontCheckForUniqueIsDeprecated.rst
new file mode 100644 (file)
index 0000000..971ebf7
--- /dev/null
@@ -0,0 +1,26 @@
+============================================================================
+Deprecation: #63603 - ExtendedFileUtility::$dontCheckForUnique is deprecated
+============================================================================
+
+Description
+===========
+
+The ExtendedFileUtility ``$dontCheckForUnique`` flag is deprecated and replaced by ``$fileUtility->setExistingFileConflictMode()`` with the possible options ``cancel``, ``replace`` and ``changeName``.
+
+
+Impact
+======
+
+Extensions still using ``ExtendedFileUtility::$dontCheckForUnique`` will throw a deprecation warning.
+
+
+Affected Installations
+======================
+
+All installations with extensions that use ``ExtendedFileUtility::$dontCheckForUnique``.
+
+
+Migration
+=========
+
+Change the ``$fileUtility->dontCheckForUnique = TRUE`` to ``$fileUtility->setExistingFileConflictMode('replace')``.
\ No newline at end of file
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-63603-FileControllerAndFileListControllerOverwriteExistingFilesChangedToStringValue.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-63603-FileControllerAndFileListControllerOverwriteExistingFilesChangedToStringValue.rst
new file mode 100644 (file)
index 0000000..a914666
--- /dev/null
@@ -0,0 +1,26 @@
+==========================================================================================================
+Deprecation: #63603 - FileController and FileListController overwriteExistingFiles changed to string value
+==========================================================================================================
+
+Description
+===========
+
+The GET/POST param to tell the FileController and FileListController to override a file or not switched from a bool value to a string with the possibilities ``cancel``, ``replace`` and ``changeName``.
+
+
+Impact
+======
+
+Extensions still using ``overwriteExistingFiles = 1`` will throw a deprecation warning.
+
+
+Affected Installations
+======================
+
+All installations with extensions that use the BE upload functionality and supply the file override option.
+
+
+Migration
+=========
+
+Change the ``<input name="overwriteExistingFiles" value="1">`` to ``<input name="overwriteExistingFiles" value="replace">``.
\ No newline at end of file
index 79db38e..ca19599 100644 (file)
@@ -109,9 +109,11 @@ class FileListController {
        public $cmd;
 
        /**
-        * @var bool
+        * Defines behaviour when uploading files with names that already exist; possible value are 'cancel', 'replace', 'changeName'
+        *
+        * @var string
         */
-       public $overwriteExistingFiles;
+       protected $overwriteExistingFiles;
 
        /**
         * The file list object
@@ -152,6 +154,11 @@ class FileListController {
                $this->cmd = GeneralUtility::_GP('cmd');
                $this->overwriteExistingFiles = GeneralUtility::_GP('overwriteExistingFiles');
 
+               if ($this->overwriteExistingFiles === '1') {
+                       GeneralUtility::deprecationLog('overwriteExitingFiles = 1 is deprecated. Use overwriteExitingFiles = "replace". Support for old behavior will be removed in TYPO3 CMS 8.');
+                       $this->overwriteExistingFiles = 'replace';
+               }
+
                try {
                        if ($combinedIdentifier) {
                                /** @var $fileFactory ResourceFactory */
@@ -327,11 +334,22 @@ class FileListController {
                                        foreach ($items as $v) {
                                                $FILE['delete'][] = array('data' => $v);
                                        }
+                                       switch ($this->overwriteExistingFiles) {
+                                               case 'replace':
+                                               case 'changeName':
+                                                       $conflictMode = $this->overwriteExistingFiles;
+                                                       break;
+                                               default:
+                                                       $conflictMode = 'cancel';
+                                                       break;
+                                       }
+
                                        // Init file processing object for deleting and pass the cmd array.
+                                       /** @var ExtendedFileUtility $fileProcessor */
                                        $fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
                                        $fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
                                        $fileProcessor->setActionPermissions();
-                                       $fileProcessor->dontCheckForUnique = $this->overwriteExistingFiles ? 1 : 0;
+                                       $fileProcessor->setExistingFilesConflictMode($conflictMode);
                                        $fileProcessor->start($FILE);
                                        $fileProcessor->processData();
                                        $fileProcessor->pushErrorMessagesToFlashMessageQueue();
index e88a8a3..f954b9d 100644 (file)
@@ -1318,7 +1318,7 @@ class ImportExportController extends \TYPO3\CMS\Backend\Module\BaseScriptClass {
                $this->fileProcessor = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class);
                $this->fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
                $this->fileProcessor->setActionPermissions();
-               $this->fileProcessor->dontCheckForUnique = GeneralUtility::_GP('overwriteExistingFiles') ? 1 : 0;
+               $this->fileProcessor->setExistingFilesConflictMode((int)GeneralUtility::_GP('overwriteExistingFiles') === 1 ? 'replace' : 'cancel');
                // Checking referer / executing:
                $refInfo = parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
                $httpHost = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');