fix file name unicode issue (#28)

This commit is contained in:
Chirag 2022-11-15 15:37:07 +05:30 committed by GitHub
parent 0ef6e0cbdd
commit 0c0a9f1349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -29,7 +29,8 @@ class StorageFileNameParser
public function getMovedFileName(): ?string public function getMovedFileName(): ?string
{ {
if ($this->fileName && $this->extension) { if ($this->fileName && $this->extension) {
return substr($this->fileName,0,50).'_'.$this->uuid.'.'.$this->extension; $fileName = substr($this->fileName, 0, 50).'_'.$this->uuid.'.'.$this->extension;
return mb_convert_encoding($fileName, 'UTF-8', 'UTF-8');
} }
return $this->uuid; return $this->uuid;
} }

View File

@ -25,3 +25,12 @@ it('can parse filenames', function () {
expect($parsedFilename->getMovedFileName())->toBeNull(); expect($parsedFilename->getMovedFileName())->toBeNull();
}); });
it('can handles non-utf characters', function () {
$fileName = 'Образец_для_заполнения_85e16d7b-58ed-43bc-8dce-7d3ff7d69f41.png';
$parsedFilename = \App\Service\Storage\StorageFileNameParser::parse($fileName);
expect($parsedFilename->fileName)->toBe('Образец_для_заполнения');
expect($parsedFilename->uuid)->toBe('85e16d7b-58ed-43bc-8dce-7d3ff7d69f41');
expect($parsedFilename->extension)->toBe('png');
expect($parsedFilename->getMovedFileName())->toBe($fileName);
});