Commit c55f0520 authored by Frans Saris's avatar Frans Saris Committed by Benni Mack
Browse files

[FEATURE] Make file conflict mode available for BE file upload

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's avatarAndreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez's avatarAndreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Benni Mack's avatarBenjamin Mack <benni@typo3.org>
Tested-by: Benni Mack's avatarBenjamin Mack <benni@typo3.org>
parent 35f8da6e
......@@ -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');
......
......@@ -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>
......
......@@ -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 . '" />
';
......
......@@ -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);
......
......@@ -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,9 +68,17 @@ 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.
......@@ -136,6 +145,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
*
......@@ -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;
......
============================================================================
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
==========================================================================================================
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
......@@ -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();
......
......@@ -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');
......
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