|
16 | 16 | use Psr\Http\Message\{StreamInterface, UploadedFileInterface}; |
17 | 17 | use function Koded\Stdlib\randomstring; |
18 | 18 |
|
19 | | - |
20 | 19 | class UploadedFile implements UploadedFileInterface |
21 | 20 | { |
22 | | - private ?string $file; |
23 | | - private ?string $name; |
| 21 | + private mixed $file; |
| 22 | + private mixed $name; |
24 | 23 | private ?string $type; |
25 | 24 | private ?int $size; |
26 | 25 | private int $error; |
27 | 26 | private bool $moved = false; |
28 | 27 |
|
29 | 28 | public function __construct(array $uploadedFile) |
30 | 29 | { |
31 | | - $this->size = $uploadedFile['size'] ?? null; |
32 | 30 | $this->file = $uploadedFile['tmp_name'] ?? null; |
33 | 31 | $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(); |
47 | 34 | $this->type = $this->getClientMediaType(); |
| 35 | + $this->error = (int)($uploadedFile['error'] ?? \UPLOAD_ERR_OK); |
48 | 36 | } |
49 | 37 |
|
50 | 38 | public function getStream(): StreamInterface |
@@ -115,6 +103,24 @@ private function assertTargetPath($targetPath): void |
115 | 103 | @\mkdir($dirname, 0777, true); |
116 | 104 | } |
117 | 105 | } |
| 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 | + } |
118 | 124 | } |
119 | 125 |
|
120 | 126 |
|
@@ -145,9 +151,11 @@ public static function fileAlreadyMoved(): \RuntimeException |
145 | 151 | return new \RuntimeException('File is not available, because it was previously moved'); |
146 | 152 | } |
147 | 153 |
|
148 | | - public static function fileNotSupported(): \InvalidArgumentException |
| 154 | + public static function fileNotSupported(mixed $file): \InvalidArgumentException |
149 | 155 | { |
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 | + )); |
151 | 159 | } |
152 | 160 |
|
153 | 161 | public static function filenameCannotBeEmpty(): \InvalidArgumentException |
|
0 commit comments