-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadedFile.php
165 lines (143 loc) · 5.05 KB
/
UploadedFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded\Http;
use Koded\Exceptions\KodedException;
use Psr\Http\Message\{StreamInterface, UploadedFileInterface};
use function Koded\Stdlib\randomstring;
class UploadedFile implements UploadedFileInterface
{
private mixed $file;
private mixed $name;
private ?string $type;
private ?int $size;
private int $error;
private bool $moved = false;
public function __construct(array $uploadedFile)
{
$this->file = $uploadedFile['tmp_name'] ?? null;
$this->name = $uploadedFile['name'] ?? randomstring(9);
$this->size = $uploadedFile['size'] ?? null;
$this->prepareFile();
$this->type = $this->getClientMediaType();
$this->error = (int)($uploadedFile['error'] ?? \UPLOAD_ERR_OK);
}
public function getStream(): StreamInterface
{
if ($this->moved) {
throw UploadedFileException::streamNotAvailable();
}
return new FileStream($this->file, 'w+b');
}
public function moveTo($targetPath)
{
$this->assertUploadError();
$this->assertTargetPath($targetPath);
// @codeCoverageIgnoreStart
try {
$this->moved = ('cli' === \php_sapi_name())
? \rename($this->file, $targetPath)
: \move_uploaded_file($this->file, $targetPath);
@\unlink($this->file);
} catch (\Throwable $e) {
throw new \RuntimeException($e->getMessage());
}
// @codeCoverageIgnoreEnd
}
public function getSize(): ?int
{
return $this->size;
}
public function getError(): int
{
return $this->error;
}
public function getClientFilename(): ?string
{
return $this->name;
}
public function getClientMediaType(): ?string
{
try {
return @(new \finfo(\FILEINFO_MIME_TYPE))->file($this->file);
} catch (\Throwable) {
return $this->type;
}
}
private function assertUploadError(): void
{
if ($this->error !== \UPLOAD_ERR_OK) {
throw new UploadedFileException($this->error);
}
}
private function assertTargetPath($targetPath): void
{
if ($this->moved) {
throw UploadedFileException::fileAlreadyMoved();
}
if (false === \is_string($targetPath) || 0 === \mb_strlen($targetPath)) {
throw UploadedFileException::targetPathIsInvalid();
}
if (false === \is_dir($dirname = \dirname($targetPath))) {
@\mkdir($dirname, 0777, true);
}
}
private function prepareFile(): void
{
if ($this->file instanceof StreamInterface) {
// Create a temporary file out of the stream object
$this->size = $this->file->getSize();
$file = \sys_get_temp_dir() . '/' . $this->name;
\file_put_contents($file, $this->file->getContents());
$this->file = $file;
return;
}
if (false === \is_string($this->file)) {
throw UploadedFileException::fileNotSupported($this->file);
}
if (0 === \mb_strlen($this->file)) {
throw UploadedFileException::filenameCannotBeEmpty();
}
}
}
class UploadedFileException extends KodedException
{
protected array $messages = [
\UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the "upload_max_filesize" directive in php.ini',
\UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the "MAX_FILE_SIZE" directive that was specified in the HTML form',
\UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded',
\UPLOAD_ERR_NO_FILE => 'No file was uploaded',
\UPLOAD_ERR_NO_TMP_DIR => 'The temporary directory to write to is missing',
\UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk',
\UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload',
];
public static function streamNotAvailable(): \RuntimeException
{
return new \RuntimeException('Stream is not available, because the file was previously moved');
}
public static function targetPathIsInvalid(): \InvalidArgumentException
{
return new \InvalidArgumentException('The provided path for moveTo operation is not valid');
}
public static function fileAlreadyMoved(): \RuntimeException
{
return new \RuntimeException('File is not available, because it was previously moved');
}
public static function fileNotSupported(mixed $file): \InvalidArgumentException
{
return new \InvalidArgumentException(sprintf(
'The uploaded file is not supported, expected string, %s given', \get_debug_type($file)
));
}
public static function filenameCannotBeEmpty(): \InvalidArgumentException
{
return new \InvalidArgumentException('Filename cannot be empty');
}
}