Skip to content
Snippets Groups Projects

Issue #3203511: Improve filesize validation

Files
2
@@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace Drupal\file_extractor;
use Drupal\Component\Utility\Bytes;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
@@ -61,7 +62,7 @@ class ExtractionSettingsFormHelper {
$form['extractable']['max_filesize'] = [
'#type' => 'textfield',
'#title' => $this->t('Maximum file size'),
'#description' => $this->t('Enter a value like "10 KB", "10 MB" or "10 GB" in order to restrict the max file size of files that should be extracted.<br /> Enter "0" for no limit restriction.'),
'#description' => $this->t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the max file size of files that should be extracted.<br /> Enter "0" for no limit restriction.'),
'#default_value' => $temporary_values['extractable']['max_filesize'],
'#size' => 10,
];
@@ -80,9 +81,9 @@ class ExtractionSettingsFormHelper {
$form['extraction_result']['number_first_bytes'] = [
'#type' => 'textfield',
'#title' => $this->t('Maximum size of the extracted string'),
'#description' => $this->t('Enter a value like "1000", "10 KB", "10 MB" or "10 GB" in order to restrict the max size of the content after extraction.<br /> Enter "0" for no limit restriction.'),
'#description' => $this->t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the max size of the content after extraction.<br /> Enter "0" for no limit restriction.'),
'#default_value' => $temporary_values['extraction_result']['number_first_bytes'],
'#size' => 5,
'#size' => 10,
];
return $form;
@@ -99,52 +100,19 @@ class ExtractionSettingsFormHelper {
'extraction_result',
'number_first_bytes',
]));
$error = $this->validateSize($number_first_bytes);
if ($error) {
$form_state->setError($form['extraction_result']['number_first_bytes'], $this->t('The size limit option must contain a valid value. You may either enter "0" (for no restriction) or a string like "10 KB", "10 MB" or "10 GB".'));
if (!Bytes::validate($number_first_bytes)) {
$form_state->setError($form['extraction_result']['number_first_bytes'], $this->t('The "@name" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', [
'@name' => $this->t('Maximum size of the extracted string'),
]));
}
// Validate 'max_filesize'.
$max_filesize = trim($form_state->getValue(['extractable', 'max_filesize']));
$error = $this->validateSize($max_filesize);
if ($error) {
$form_state->setError($form['extractable']['max_filesize'], $this->t('The maximum file size option must contain a valid value. You may either enter "0" (for no restriction) or a string like "10 KB", "10 MB" or "10 GB".'));
}
}
/**
* Helper method to validate the size of files' format.
*
* @param string $bytes
* Number of bytes.
*
* @return bool
* TRUE if $bites is of form "N KB", "N MB" or "N GB" where N is integer.
*/
protected function validateSize(string $bytes) : bool {
$error = FALSE;
if ($bytes != '0') {
$size_info = explode(' ', $bytes);
// The only case we can have count($size_info) == 1 is for '0' value.
if (count($size_info) == 1) {
$error = $size_info[0] != '0';
}
elseif (count($size_info) != 2) {
$error = TRUE;
}
else {
// Ensure that the string starts with a numeric character.
if (!preg_match('/^[0-9]/', $bytes)) {
$error = TRUE;
}
if (!in_array($size_info[1], ['KB', 'MB', 'GB'])) {
$error = TRUE;
}
}
if (!Bytes::validate($max_filesize)) {
$form_state->setError($form['extractable']['max_filesize'], $this->t('The "@name" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', [
'@name' => $this->t('Maximum file size'),
]));
}
return $error;
}
}
Loading