{file.size} * * ``` * * @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-format-bytes */ final class BytesViewHelper extends AbstractViewHelper { /** * Output is escaped already. We must not escape children, to avoid double encoding. * * @var bool */ protected $escapeChildren = false; public function initializeArguments(): void { $this->registerArgument('value', 'int|float|string', 'The incoming data to convert, or NULL if VH children should be used'); $this->registerArgument('decimals', 'int', 'The number of digits after the decimal point', false, 0); $this->registerArgument('decimalSeparator', 'string', 'The decimal point character', false, '.'); $this->registerArgument('thousandsSeparator', 'string', 'The character for grouping the thousand digits', false, ','); $this->registerArgument('units', 'string', 'comma separated list of available units, default is LocalizationUtility::translate(\'viewhelper.format.bytes.units\', \'fluid\')'); } /** * Render the supplied byte count as a human-readable string. */ public function render(): string { if ($this->arguments['units'] !== null) { $units = $this->arguments['units']; } else { $units = LocalizationUtility::translate('viewhelper.format.bytes.units', 'fluid'); } $units = GeneralUtility::trimExplode(',', (string)$units, true); $value = $this->renderChildren(); if (is_numeric($value)) { $value = (float)$value; } else { $value = 0; } $bytes = max($value, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= 2 ** (10 * $pow); return sprintf( '%s %s', number_format( round($bytes, 4 * $this->arguments['decimals']), (int)$this->arguments['decimals'], $this->arguments['decimalSeparator'], $this->arguments['thousandsSeparator'] ), $units[$pow] ); } /** * Explicitly set argument name to be used as content. */ public function getContentArgumentName(): string { return 'value'; } }