Commit ab5d305b authored by Benni Mack's avatar Benni Mack
Browse files

[TASK] Centralize imagefile_ext, mediafile_ext and textfile_ext

Most access to the configuration options are made against AbstractFile
objects in FAL, which is now centralized in one place in AbstractFile.

This reduces errors when checking for file patterns.

Resolves: #90305
Releases: master
Change-Id: I7835d877bae1addf830ede3645ff8f1faa113052
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63155

Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: Andreas Fernandez's avatarAndreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Susanne Moog's avatarSusanne Moog <look@susi.dev>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Andreas Fernandez's avatarAndreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Daniel Goerz's avatarDaniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Susanne Moog's avatarSusanne Moog <look@susi.dev>
Reviewed-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 989ca301
......@@ -385,13 +385,7 @@ class Clipboard
$thumb = [];
$folder = $fileObject instanceof \TYPO3\CMS\Core\Resource\Folder;
$size = $folder ? '' : '(' . GeneralUtility::formatSize($fileObject->getSize()) . 'bytes)';
if (
!$folder
&& GeneralUtility::inList(
$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
$fileObject->getExtension()
)
) {
if (!$folder && $fileObject->isImage()) {
$thumb = [
'image' => $fileObject->process(\TYPO3\CMS\Core\Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW, [])->getPublicUrl(true),
'title' => htmlspecialchars($fileObject->getName())
......
......@@ -310,7 +310,6 @@ class ElementInformationController
} else {
$rendererRegistry = GeneralUtility::makeInstance(RendererRegistry::class);
$fileRenderer = $rendererRegistry->getRenderer($this->fileObject);
$fileExtension = $this->fileObject->getExtension();
$preview['url'] = $this->fileObject->getPublicUrl(true);
$width = '590m';
......@@ -327,7 +326,7 @@ class ElementInformationController
);
// else check if we can create an Image preview
} elseif (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileExtension)) {
} elseif ($this->fileObject->isImage()) {
$preview['fileObject'] = $this->fileObject;
$preview['width'] = $width;
$preview['height'] = $height;
......
......@@ -246,8 +246,7 @@ class FileController
*/
protected function getFileEditRedirect(File $file): ?string
{
$textFileExtensionList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'] ?? '';
if (!GeneralUtility::inList($textFileExtensionList, $file->getExtension())) {
if (!$file->isTextFile()) {
return null;
}
$properties = $file->getProperties();
......@@ -279,7 +278,7 @@ class FileController
{
if ($result instanceof File) {
$thumbUrl = '';
if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $result->getExtension())) {
if ($result->isImage()) {
$processedFile = $result->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, []);
if ($processedFile) {
$thumbUrl = $processedFile->getPublicUrl(true);
......
......@@ -367,8 +367,7 @@ class InlineRecordContainer extends AbstractContainer
}
$imageSetup = array_merge(['width' => '45', 'height' => '45c'], $imageSetup);
if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['thumbnails']
&& GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileObject->getExtension())) {
if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['thumbnails'] && $fileObject->isImage()) {
$processedImage = $fileObject->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, $imageSetup);
// Only use a thumbnail if the processing process was successful by checking if image width is set
if ($processedImage->getProperty('width')) {
......
......@@ -72,7 +72,7 @@ class FileInfoElement extends AbstractFormElement
. htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:warning.file_missing'))
. '</span>';
}
if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())) {
if ($file->isImage()) {
$processedFile = $file->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, ['width' => 150, 'height' => 150]);
$previewImage = $processedFile->getPublicUrl(true);
if ($previewImage) {
......
......@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Core\Resource;
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
......@@ -319,6 +320,33 @@ abstract class AbstractFile implements FileInterface
return (int)$this->properties['type'];
}
/**
* Useful to find out if this file can be previewed or resized as image.
* @return bool true if File has an image-extension according to $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
*/
public function isImage(): bool
{
return GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] ?? ''), $this->getExtension());
}
/**
* Useful to find out if this file has a file extension based on any of the registered media extensions
* @return bool true if File is a media-extension according to $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']
*/
public function isMediaFile(): bool
{
return GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'] ?? ''), $this->getExtension());
}
/**
* Useful to find out if this file can be edited.
*
* @return bool true if File is a text-based file extension according to $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext']
*/
public function isTextFile(): bool
{
return GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'] ?? ''), $this->getExtension());
}
/******************
* CONTENTS RELATED
******************/
......
......@@ -107,9 +107,7 @@ class LocalPreviewHelper
protected function generatePreviewFromFile(File $file, array $configuration, $targetFilePath)
{
// Check file extension
if ($file->getType() !== File::FILETYPE_IMAGE
&& !GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())
) {
if ($file->getType() !== File::FILETYPE_IMAGE && !$file->isImage()) {
// Create a default image
$graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
$graphicalFunctions->getTemporaryImageWithText(
......
......@@ -989,20 +989,20 @@ class ExtendedFileUtility extends BasicFileUtility
// for backwards compatibility: the combined file identifier was the path+filename
$fileIdentifier = $cmds['target'];
$fileObject = $this->getFileObject($fileIdentifier);
// Example identifier for $cmds['target'] => "2:targetpath/targetfolder/"
$content = $cmds['data'];
if (!$fileObject instanceof File) {
$this->writeLog(SystemLogFileAction::EDIT, SystemLogErrorClassification::SYSTEM_ERROR, 123, 'Target "%s" was not a file!', [$fileIdentifier]);
$this->addMessageToFlashMessageQueue('FileUtility.TargetWasNotAFile', [$fileIdentifier]);
return false;
}
$extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
if (!GeneralUtility::inList($extList, $fileObject->getExtension())) {
if (!$fileObject->isTextFile()) {
$extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
$this->writeLog(SystemLogFileAction::EDIT, SystemLogErrorClassification::USER_ERROR, 102, 'File extension "%s" is not a textfile format! (%s)', [$fileObject->getExtension(), $extList]);
$this->addMessageToFlashMessageQueue('FileUtility.FileExtensionIsNotATextfileFormat', [$fileObject->getExtension(), $extList]);
return false;
}
try {
// Example identifier for $cmds['target'] => "2:targetpath/targetfolder/"
$content = $cmds['data'];
$fileObject->setContents($content);
clearstatcache();
$this->writeLog(SystemLogFileAction::EDIT, SystemLogErrorClassification::MESSAGE, 1, 'File saved to "%s", bytes: %s, MD5: %s ', [$fileObject->getIdentifier(), $fileObject->getSize(), md5($content)]);
......
......@@ -182,7 +182,7 @@ class FileProvider extends \TYPO3\CMS\Backend\ContextMenu\ItemProviders\Abstract
{
return $this->isFile()
&& $this->record->checkActionPermission('write')
&& GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $this->record->getExtension());
&& $this->record->isTextFile();
}
/**
......
......@@ -182,9 +182,9 @@ class EditFileController
$assigns['moduleUrlTceFile'] = (string)$uriBuilder->buildUriFromRoute('tce_file');
$assigns['fileName'] = $this->fileObject->getName();
$extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
try {
if (!$extList || !GeneralUtility::inList($extList, $this->fileObject->getExtension())) {
$extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
if (!$this->fileObject->isTextFile()) {
// @todo throw a minor exception here, not the global one
throw new \Exception('Files with that extension are not editable. Allowed extensions are: ' . $extList, 1476050135);
}
......
......@@ -20,6 +20,7 @@ use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Resource\AbstractFile;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
......@@ -75,7 +76,8 @@ class FileFacade
public function getIsEditable(): bool
{
return $this->getIsWritable()
&& GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $this->resource->getExtension());
&& $this->resource instanceof AbstractFile
&& $this->resource->isTextFile();
}
/**
......@@ -282,7 +284,7 @@ class FileFacade
*/
public function getIsImage()
{
return GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], strtolower($this->getExtension()));
return $this->resource instanceof AbstractFile && $this->resource->isImage();
}
/**
......
......@@ -795,7 +795,7 @@ class FileList
. htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:warning.file_missing'))
. '</span>';
// Thumbnails?
} elseif ($this->thumbs && ($this->isImage($ext) || $this->isMediaFile($ext))) {
} elseif ($this->thumbs && ($fileObject->isImage() || $fileObject->isMediaFile())) {
$imageUri = BackendUtility::getThumbnailUrl($fileObject->getUid(), [
'width' => $this->thumbnailConfiguration->getWidth(),
'height' => $this->thumbnailConfiguration->getHeight()
......@@ -851,24 +851,26 @@ class FileList
/**
* Returns TRUE if $ext is an image-extension according to $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
* Use the AbstractFile->isImage() method if you're using File objects directly
*
* @param string $ext File extension
* @return bool
*/
public function isImage($ext)
{
return GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], strtolower($ext));
return GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']), strtolower($ext));
}
/**
* Returns TRUE if $ext is a media-extension according to $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']
* Use the AbstractFile->isMediaFile() method if you're using File objects directly
*
* @param string $ext File extension
* @return bool
*/
public function isMediaFile($ext)
{
return GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'], strtolower($ext));
return GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']), strtolower($ext));
}
/**
......@@ -994,7 +996,7 @@ class FileList
$fullIdentifier = $fileOrFolderObject->getCombinedIdentifier();
// Edit file content (if editable)
if ($fileOrFolderObject instanceof File && $fileOrFolderObject->checkActionPermission('write') && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $fileOrFolderObject->getExtension())) {
if ($fileOrFolderObject instanceof File && $fileOrFolderObject->checkActionPermission('write') && $fileOrFolderObject->isTextFile()) {
$url = (string)$this->uriBuilder->buildUriFromRoute('file_edit', ['target' => $fullIdentifier]);
$editOnClick = 'top.list_frame.location.href=' . GeneralUtility::quoteJSvalue($url) . '+\'&returnUrl=\'+encodeURIComponent(top.list_frame.document.location.pathname+top.list_frame.document.location.search);return false;';
$cells['edit'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($editOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.editcontent') . '">'
......
......@@ -4359,7 +4359,7 @@ class ContentObjectRenderer implements LoggerAwareInterface
$processingConfiguration['stripProfile'] = $fileArray['stripProfile'];
}
// Check if we can handle this type of file for editing
if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileObject->getExtension())) {
if ($fileObject->isImage()) {
$maskArray = $fileArray['m.'];
// Must render mask images and include in hash-calculating
// - otherwise we cannot be sure the filename is unique for the setup!
......
......@@ -262,10 +262,9 @@ class FileBrowser extends AbstractElementBrowser implements ElementBrowserInterf
}
foreach ($files as $fileObject) {
$fileExtension = $fileObject->getExtension();
// Thumbnail/size generation:
$imgInfo = [];
if (!$noThumbs && GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] . ',' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']), strtolower($fileExtension))) {
if (!$noThumbs && ($fileObject->isMediaFile() || $fileObject->isImage())) {
$processedFile = $fileObject->process(
ProcessedFile::CONTEXT_IMAGEPREVIEW,
$this->thumbnailConfiguration
......@@ -295,7 +294,7 @@ class FileBrowser extends AbstractElementBrowser implements ElementBrowserInterf
'uid' => $fileObject->getUid(),
'fileName' => $fileObject->getName(),
'filePath' => $fileObject->getUid(),
'fileExt' => $fileExtension,
'fileExt' => $fileObject->getExtension(),
'fileIcon' => $icon
];
if ($this->fileIsSelectableInFileList($fileObject, $imgInfo)) {
......
......@@ -900,7 +900,7 @@ class SetupModuleController
}
// Check if extension is allowed
if ($file && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())) {
if ($file && $file->isImage()) {
// Create new file reference
$storeRec['sys_file_reference']['NEW1234'] = [
......
Markdown is supported
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