Skip to content

Commit

Permalink
FileUpload: added getSuggestedExtension()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 30, 2024
1 parent 770fb9a commit 05e2bc1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
33 changes: 28 additions & 5 deletions src/Http/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ final class FileUpload
/** @var string|false|null */
private $type;

/** @var string|false|null */
private $extension;

/** @var int */
private $size;

Expand Down Expand Up @@ -98,9 +101,9 @@ public function getSanitizedName(): string
$name = str_replace(['-.', '.-'], '.', $name);
$name = trim($name, '.-');
$name = $name === '' ? 'unknown' : $name;
if ($this->isImage()) {
if ($ext = $this->getSuggestedExtension()) {
$name = preg_replace('#\.[^.]+$#D', '', $name);
$name .= '.' . ($this->getImageFileExtension() ?? 'unknown');
$name .= '.' . $ext;
}

return $name;
Expand Down Expand Up @@ -134,6 +137,27 @@ public function getContentType(): ?string
}


/**
* Returns the file extension (without dot) appropriate to the detected MIME type of uploaded file. Requires PHP extension fileinfo.
*/
public function getSuggestedExtension(): ?string
{
if ($this->isOk() && $this->extension === null) {
$exts = finfo_file(finfo_open(FILEINFO_EXTENSION), $this->tmpName);
if ($exts && $exts !== '???') {
return $this->extension = preg_replace('~[/,].*~', '', $exts);
}
[, , $type] = @getimagesize($this->tmpName); // @ - files smaller than 12 bytes causes read error
if ($type) {
return $this->extension = image_type_to_extension($type, false);
}
$this->extension = false;
}

return $this->extension ?: null;
}


/**
* Returns the size of the uploaded file in bytes.
*/
Expand Down Expand Up @@ -252,12 +276,11 @@ public function getImageSize(): ?array

/**
* Returns image file extension based on detected content type (without dot).
* @deprecated use getSuggestedExtension()
*/
public function getImageFileExtension(): ?string
{
return $this->isImage()
? explode('/', $this->getContentType())[1]
: null;
return $this->getSuggestedExtension();
}


Expand Down
7 changes: 4 additions & 3 deletions tests/Http/FileUpload.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('', function () {
Assert::true($upload->isOk());
Assert::true($upload->hasFile());
Assert::false($upload->isImage());
Assert::null($upload->getImageFileExtension());
Assert::null($upload->getSuggestedExtension());
Assert::same(file_get_contents(__DIR__ . '/files/file.txt'), $upload->getContents());
});

Expand All @@ -51,7 +51,7 @@ test('', function () {
Assert::same('image.png', $upload->getSanitizedName());
Assert::same('../.image.png', $upload->getUntrustedFullPath());
Assert::same('image/png', $upload->getContentType());
Assert::same('png', $upload->getImageFileExtension());
Assert::same('png', $upload->getSuggestedExtension());
Assert::same([108, 46], $upload->getImageSize());
Assert::true($upload->isImage());
});
Expand All @@ -68,6 +68,7 @@ test('', function () {

Assert::false($upload->isOk());
Assert::false($upload->hasFile());
Assert::null($upload->getContentType());
Assert::false($upload->isImage());
Assert::null($upload->getImageFileExtension());
Assert::null($upload->getSuggestedExtension());
});
2 changes: 1 addition & 1 deletion tests/Http/FileUpload.getSanitizedName.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Assert::with(new FileUpload([]), function () {


Assert::with(new FileUpload([]), function () {
$this->type = 'image/jpeg';
$this->extension = 'jpeg';

$this->name = '';
Assert::same('unknown.jpeg', $this->getSanitizedName());
Expand Down

0 comments on commit 05e2bc1

Please sign in to comment.