Fix file type case sensitive validation

This commit is contained in:
Julien Nahum
2024-02-29 10:48:19 +01:00
parent 8a067012e5
commit 2ac9b96284
2 changed files with 47 additions and 15 deletions

View File

@@ -11,26 +11,15 @@ use Illuminate\Support\Str;
class StorageFile implements Rule
{
public int $maxSize;
public string $error = 'Invalid file.';
/** @var string[] */
public array $fileTypes;
/**
* @param string[] $fileTypes
*/
public function __construct(int $maxSize, array $fileTypes = [], public ?Form $form = null)
public function __construct(public int $maxSize, public array $fileTypes = [], public ?Form $form = null)
{
$this->maxSize = $maxSize;
$this->fileTypes = $fileTypes;
}
/**
* File can have 2 formats:
* - file_name-{uuid}.{ext}
* - file-name_{uuid}.{ext}
* - {uuid}
*
* @param string $attribute
@@ -69,8 +58,9 @@ class StorageFile implements Rule
if (count($this->fileTypes) > 0) {
$this->error = 'Incorrect file type. Allowed only: '.implode(',', $this->fileTypes);
return in_array($fileNameParser->extension, $this->fileTypes);
return collect($this->fileTypes)->map(function ($type) {
return strtolower($type);
})->contains(strtolower($fileNameParser->extension));
}
return true;