8a5cee08e57736da328232db57aac27cfdee4581
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / ViewHelpers / File / SizeViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Install\ViewHelpers\File;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20
21 /**
22 * Get file size from file
23 *
24 * = Examples =
25 *
26 * <code title="Defaults">
27 * <f:file.size>/var/www/typo3/instance/typo3temp/foo.jpg</f:file.size>
28 * </code>
29 * <output>
30 * 1,2k
31 * </output>
32 *
33 * @internal
34 */
35 class SizeViewHelper extends AbstractViewHelper
36 {
37 /**
38 * Output is escaped already. We must not escape children, to avoid double encoding.
39 *
40 * @var bool
41 */
42 protected $escapeChildren = false;
43
44 /**
45 * Get size from file
46 *
47 * @param bool $format If true, file size will be formatted
48 * @throws \TYPO3\CMS\Install\ViewHelpers\Exception
49 * @return int File size
50 */
51 public function render($format = true)
52 {
53 return static::renderStatic(
54 array(
55 'format' => $format,
56 ),
57 $this->buildRenderChildrenClosure(),
58 $this->renderingContext
59 );
60 }
61
62 /**
63 * @param array $arguments
64 * @param \Closure $renderChildrenClosure
65 * @param RenderingContextInterface $renderingContext
66 * @throws \TYPO3\CMS\Install\ViewHelpers\Exception
67 *
68 * @return string
69 */
70 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
71 {
72 $format = $arguments['format'];
73
74 $absolutePathToFile = $renderChildrenClosure();
75 if (!is_file($absolutePathToFile)) {
76 throw new \TYPO3\CMS\Install\ViewHelpers\Exception(
77 'File not found',
78 1369563246
79 );
80 }
81 $size = filesize($absolutePathToFile);
82 if ($format) {
83 $size = GeneralUtility::formatSize($size);
84 }
85 return $size;
86 }
87 }