Skip to content

Commit 84f31ad

Browse files
committed
chore:
- constructor cleanup - adds the file/type in exception message
1 parent 73ebf08 commit 84f31ad

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

UploadedFile.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,23 @@
1616
use Psr\Http\Message\{StreamInterface, UploadedFileInterface};
1717
use function Koded\Stdlib\randomstring;
1818

19-
2019
class UploadedFile implements UploadedFileInterface
2120
{
22-
private ?string $file;
23-
private ?string $name;
21+
private mixed $file;
22+
private mixed $name;
2423
private ?string $type;
2524
private ?int $size;
2625
private int $error;
2726
private bool $moved = false;
2827

2928
public function __construct(array $uploadedFile)
3029
{
31-
$this->size = $uploadedFile['size'] ?? null;
3230
$this->file = $uploadedFile['tmp_name'] ?? null;
3331
$this->name = $uploadedFile['name'] ?? randomstring(9);
34-
$this->error = (int)($uploadedFile['error'] ?? \UPLOAD_ERR_OK);
35-
36-
// Create a file out of the stream
37-
if ($this->file instanceof StreamInterface) {
38-
$file = \sys_get_temp_dir() . '/' . $this->name;
39-
\file_put_contents($file, $this->file->getContents());
40-
$this->file = $file;
41-
} elseif (false === \is_string($this->file)) {
42-
throw UploadedFileException::fileNotSupported();
43-
} elseif (0 === \strlen($this->file)) {
44-
throw UploadedFileException::filenameCannotBeEmpty();
45-
}
46-
// Never trust the provided mime type
32+
$this->size = $uploadedFile['size'] ?? null;
33+
$this->prepareFile();
4734
$this->type = $this->getClientMediaType();
35+
$this->error = (int)($uploadedFile['error'] ?? \UPLOAD_ERR_OK);
4836
}
4937

5038
public function getStream(): StreamInterface
@@ -115,6 +103,24 @@ private function assertTargetPath($targetPath): void
115103
@\mkdir($dirname, 0777, true);
116104
}
117105
}
106+
107+
private function prepareFile(): void
108+
{
109+
if ($this->file instanceof StreamInterface) {
110+
// Create a temporary file out of the stream object
111+
$this->size = $this->file->getSize();
112+
$file = \sys_get_temp_dir() . '/' . $this->name;
113+
\file_put_contents($file, $this->file->getContents());
114+
$this->file = $file;
115+
return;
116+
}
117+
if (false === \is_string($this->file)) {
118+
throw UploadedFileException::fileNotSupported($this->file);
119+
}
120+
if (0 === \mb_strlen($this->file)) {
121+
throw UploadedFileException::filenameCannotBeEmpty();
122+
}
123+
}
118124
}
119125

120126

@@ -145,9 +151,11 @@ public static function fileAlreadyMoved(): \RuntimeException
145151
return new \RuntimeException('File is not available, because it was previously moved');
146152
}
147153

148-
public static function fileNotSupported(): \InvalidArgumentException
154+
public static function fileNotSupported(mixed $file): \InvalidArgumentException
149155
{
150-
return new \InvalidArgumentException('The uploaded file is not supported');
156+
return new \InvalidArgumentException(sprintf(
157+
'The uploaded file is not supported, expected string, %s given', \get_debug_type($file)
158+
));
151159
}
152160

153161
public static function filenameCannotBeEmpty(): \InvalidArgumentException

0 commit comments

Comments
 (0)