‪TYPO3CMS  9.5
FileSizeValidator.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
24 
32 {
36  protected ‪$supportedOptions = [
37  'minimum' => ['0B', 'The minimum file size to accept', 'string'],
38  'maximum' => [PHP_INT_MAX . 'B', 'The maximum file size to accept', 'string']
39  ];
40 
46  public function ‪isValid($resource)
47  {
48  $this->‪validateOptions();
49  if ($resource instanceof ‪FileReference) {
50  $fileSize = $resource->getOriginalResource()->getSize();
51  } elseif ($resource instanceof ‪File) {
52  $fileSize = $resource->getSize();
53  } elseif ($resource instanceof ‪PseudoFile) {
54  $fileSize = $resource->getSize();
55  } else {
56  $this->‪addError(
58  'validation.error.1505303626',
59  'form'
60  ),
61  1505303626
62  );
63  return;
64  }
65 
66  $minFileSize = GeneralUtility::getBytesFromSizeMeasurement($this->options['minimum']);
67  $maxFileSize = GeneralUtility::getBytesFromSizeMeasurement($this->options['maximum']);
68 
69  $labels = ' Bytes| Kilobyte| Megabyte| Gigabyte';
70  if ($fileSize < $minFileSize) {
71  $this->‪addError(
73  'validation.error.1505305752',
74  'form',
75  [GeneralUtility::formatSize($minFileSize, $labels)]
76  ),
77  1505305752,
78  [GeneralUtility::formatSize($minFileSize, $labels)]
79  );
80  }
81  if ($fileSize > $maxFileSize) {
82  $this->‪addError(
84  'validation.error.1505305753',
85  'form',
86  [GeneralUtility::formatSize($maxFileSize, $labels)]
87  ),
88  1505305753,
89  [GeneralUtility::formatSize($maxFileSize, $labels)]
90  );
91  }
92  }
93 
99  protected function ‪validateOptions()
100  {
101  if (!preg_match('/^(\d*\.?\d+)(B|K|M|G)$/i', $this->options['minimum'])) {
102  throw new ‪InvalidValidationOptionsException('The option "minimum" has an invalid format. Valid formats are something like this: "10B|K|M|G".', 1505304205);
103  }
104  if (!preg_match('/^(\d*\.?\d+)(B|K|M|G)$/i', $this->options['maximum'])) {
105  throw new ‪InvalidValidationOptionsException('The option "maximum" has an invalid format. Valid formats are something like this: "10B|K|M|G".', 1505304206);
106  }
107  }
108 }
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:23
‪TYPO3\CMS\Form\Mvc\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:23
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\translateErrorMessage
‪string null translateErrorMessage($translateKey, $extensionName, $arguments=[])
Definition: AbstractValidator.php:149
‪TYPO3\CMS\Form\Mvc\Validation\FileSizeValidator\isValid
‪isValid($resource)
Definition: FileSizeValidator.php:45
‪TYPO3\CMS\Form\Mvc\Validation\FileSizeValidator
Definition: FileSizeValidator.php:32
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Form\Mvc\Validation\FileSizeValidator\$supportedOptions
‪array $supportedOptions
Definition: FileSizeValidator.php:35
‪TYPO3\CMS\Form\Mvc\Validation
Definition: CountValidator.php:3
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\addError
‪addError($message, $code, array $arguments=[], $title='')
Definition: AbstractValidator.php:116
‪TYPO3\CMS\Form\Mvc\Validation\FileSizeValidator\validateOptions
‪validateOptions()
Definition: FileSizeValidator.php:98
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\PseudoFile
Definition: PseudoFile.php:28