Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Cleanup FileUtility #555

Merged
merged 3 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 16 additions & 74 deletions Classes/Utility/FileUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,22 @@
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Typolink\LinkResultInterface;

/**
* Class FileUtility
*/
class FileUtility
{
public const RETINA_RATIO = 2;
public const LQIP_RATIO = 0.1;

/**
* @var ContentObjectRenderer
*/
protected $contentObjectRenderer;

/**
* @param RendererRegistry
*/
protected $rendererRegistry;

/**
* @var ImageService
*/
protected $imageService;

/**
* @var ServerRequestInterface
*/
protected $serverRequest;

/**
* @var array
*/
protected $errors = [];
protected ContentObjectRenderer $contentObjectRenderer;
protected RendererRegistry $rendererRegistry;
protected ImageService $imageService;
protected ?ServerRequestInterface $serverRequest;
protected EventDispatcherInterface $eventDispatcher;

/**
* @var EventDispatcherInterface
* @var array<string, array<string, string>>
*/
private EventDispatcherInterface $eventDispatcher;
protected array $errors = [];

/**
* @param ContentObjectRenderer|null $contentObjectRenderer
* @param RendererRegistry|null $rendererRegistry
* @param ImageService|null $imageService
* @param ServerRequestInterface|null $serverRequest
* @param EventDispatcherInterface|null $eventDispatcher
*/
public function __construct(
?ContentObjectRenderer $contentObjectRenderer = null,
?RendererRegistry $rendererRegistry = null,
Expand All @@ -98,22 +69,19 @@ public function processFile(FileInterface $fileReference, array $dimensions = []
$fileRenderer = $this->rendererRegistry->getRenderer($fileReference);
$crop = $fileReference->getProperty('crop');
$originalFileUrl = $fileReference->getPublicUrl();

$metaData = $fileReference->toArray();

$link = null;
$link = $fileReference->getProperty('link');
$linkData = null;

if (!empty($metaData['link'])) {
$linkData = $this->contentObjectRenderer->typoLink('', ['parameter' => $metaData['link'], 'returnLast' => 'result']);
if (!empty($link)) {
$linkData = $this->contentObjectRenderer->typoLink('', ['parameter' => $link, 'returnLast' => 'result']);
$link = $linkData instanceof LinkResultInterface ? $linkData->getUrl() : null;
}

$originalProperties = [
'title' => $fileReference->getProperty('title'),
'alternative' => $fileReference->getProperty('alternative'),
'description' => $fileReference->getProperty('description'),
'link' => $link ?? null,
'link' => $link === '' ? null : $link,
'linkData' => $linkData ?? null,
];

Expand Down Expand Up @@ -145,10 +113,8 @@ public function processFile(FileInterface $fileReference, array $dimensions = []
'height' => $this->getCroppedDimensionalProperty($fileReference, 'height', $cropVariant)
],
'crop' => $crop,
'autoplay' => $fileReference->hasProperty('autoplay')
? $fileReference->getProperty('autoplay') : null,
'extension' => $fileReference->hasProperty('extension')
? $fileReference->getProperty('extension') : null,
'autoplay' => $fileReference->getProperty('autoplay'),
'extension' => $fileReference->getProperty('extension'),
];

$properties = $this->eventDispatcher->dispatch(
Expand All @@ -168,20 +134,13 @@ public function processFile(FileInterface $fileReference, array $dimensions = []
}

/**
* @param FileInterface $fileReference
* @param array $arguments
* @param string $cropVariant
* @return ProcessedFile
* @param array<string, mixed> $arguments
*/
public function processImageFile(FileInterface $image, array $arguments = [], string $cropVariant = 'default'): ProcessedFile
{
try {
$properties = $image->getProperties();
$cropString = $properties['crop'];
if ($image->hasProperty('crop') && $image->getProperty('crop')) {
$cropString = $image->getProperty('crop');
}
$cropVariantCollection = $this->createCropVariant((string)$cropString);
$cropVariantCollection = $this->createCropVariant((string)$image->getProperty('crop'));
$cropVariant = $cropVariant ?: 'default';
$cropArea = $cropVariantCollection->getCropArea($cropVariant);
$processingInstructions = [
Expand All @@ -204,10 +163,6 @@ public function processImageFile(FileInterface $image, array $arguments = [], st
}
}

/**
* @param string $fileUrl
* @return string
*/
public function getAbsoluteUrl(string $fileUrl): string
{
$siteUrl = $this->getNormalizedParams()->getSiteUrl();
Expand All @@ -221,20 +176,14 @@ public function getAbsoluteUrl(string $fileUrl): string
return $fileUrl;
}

public function getErrors()
public function getErrors(): array
{
return $this->errors;
}

/**
* When retrieving the height or width for a media file
* a possible cropping needs to be taken into account.
*
* @param FileInterface $fileObject
* @param string $dimensionalProperty 'width' or 'height'
*
* @param string $cropVariant defaults to 'default' variant
* @return int
*/
protected function getCroppedDimensionalProperty(
FileInterface $fileObject,
Expand All @@ -251,10 +200,6 @@ protected function getCroppedDimensionalProperty(
)[$dimensionalProperty];
}

/**
* @param int $value
* @return string
*/
protected function calculateKilobytesToFileSize(int $value): string
{
$units = $this->translate('viewhelper.format.bytes.units', 'fluid');
Expand All @@ -267,9 +212,6 @@ protected function calculateKilobytesToFileSize(int $value): string
return number_format(round($bytes, 4 * 2)) . ' ' . $units[$pow];
}

/**
* @return NormalizedParams
*/
protected function getNormalizedParams(): NormalizedParams
{
return $this->serverRequest->getAttribute('normalizedParams');
Expand Down
6 changes: 6 additions & 0 deletions Tests/Unit/Utility/FileUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use TYPO3\CMS\Frontend\Typolink\LinkResult;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

use function array_merge;

class FileUtilityTest extends UnitTestCase
{
use ProphecyTrait;
Expand Down Expand Up @@ -264,6 +266,10 @@ protected function getMockFileForData($data, array $overrideToArray = [])
$file->method('getUid')->willReturn($data['uid']);
$file->method('getPublicUrl')->willReturn('/fileadmin/test-file.jpg');
if ($overrideToArray !== []) {
$_data = array_merge($data, $overrideToArray);
$file->method('getProperty')->willReturnCallback(static function ($key) use ($_data) {
return $_data[$key] ?? null;
});
$file->method('toArray')->willReturn($overrideToArray);
} else {
$file->method('toArray')->willReturn(
Expand Down