1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thruster\Component\Http; |
4
|
|
|
|
5
|
|
|
use Doctrine\Instantiator\Exception\InvalidArgumentException; |
6
|
|
|
use GuzzleHttp\Psr7\Stream; |
7
|
|
|
use Psr\Http\Message\StreamInterface; |
8
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
9
|
|
|
use SebastianBergmann\GlobalState\RuntimeException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class UploadedFile |
13
|
|
|
* |
14
|
|
|
* @package Thruster\Component\Http |
15
|
|
|
* @author Aurimas Niekis <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class UploadedFile implements UploadedFileInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $clientFilename; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $clientMediaType; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $error; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $file; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $fileSize; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected $moved = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var StreamInterface |
51
|
|
|
*/ |
52
|
|
|
protected $stream; |
53
|
|
|
|
54
|
|
|
public function __construct( |
55
|
|
|
string $tempFile, |
56
|
|
|
int $fileSize, |
57
|
|
|
int $errorStatus, |
58
|
|
|
string $clientFilename = null, |
59
|
|
|
string $clientMediaType = null |
60
|
|
|
) { |
61
|
|
|
if ($errorStatus === UPLOAD_ERR_OK) { |
62
|
|
|
if (false === file_exists($tempFile)) { |
63
|
|
|
throw new InvalidArgumentException( |
64
|
|
|
'Temp File does not exists' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->file = $tempFile; |
69
|
|
|
$this->fileSize = $fileSize; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (false === is_int($errorStatus) |
73
|
|
|
|| 0 > $errorStatus |
74
|
|
|
|| 8 < $errorStatus |
75
|
|
|
) { |
76
|
|
|
throw new InvalidArgumentException( |
77
|
|
|
'Invalid error status for UploadedFile; must be an UPLOAD_ERR_* constant' |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->error = $errorStatus; |
82
|
|
|
|
83
|
|
|
if (null !== $clientFilename && false === is_string($clientFilename)) { |
84
|
|
|
throw new InvalidArgumentException( |
85
|
|
|
'Invalid client filename provided for UploadedFile; must be null or a string' |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->clientFilename = $clientFilename; |
90
|
|
|
|
91
|
|
|
if (null !== $clientMediaType && false === is_string($clientMediaType)) { |
92
|
|
|
throw new InvalidArgumentException( |
93
|
|
|
'Invalid client media type provided for UploadedFile; must be null or a string' |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->clientMediaType = $clientMediaType; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
* @throws \RuntimeException if the upload was not successful. |
103
|
|
|
*/ |
104
|
|
|
public function getStream() |
105
|
|
|
{ |
106
|
|
|
if ($this->error !== UPLOAD_ERR_OK) { |
107
|
|
|
throw new RuntimeException('Cannot retrieve stream due to upload error'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($this->moved) { |
111
|
|
|
throw new RuntimeException('Cannot retrieve stream after it has already been moved'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($this->stream) { |
115
|
|
|
return $this->stream; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->stream = new Stream(fopen($this->file, 'rb')); |
119
|
|
|
|
120
|
|
|
return $this->stream; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
* |
126
|
|
|
* @see http://php.net/is_uploaded_file |
127
|
|
|
* @see http://php.net/move_uploaded_file |
128
|
|
|
* |
129
|
|
|
* @param string $targetPath Path to which to move the uploaded file. |
130
|
|
|
* |
131
|
|
|
* @throws \RuntimeException if the upload was not successful. |
132
|
|
|
* @throws \InvalidArgumentException if the $path specified is invalid. |
133
|
|
|
* @throws \RuntimeException on any error during the move operation, or on |
134
|
|
|
* the second or subsequent call to the method. |
135
|
|
|
*/ |
136
|
|
|
public function moveTo($targetPath) |
137
|
|
|
{ |
138
|
|
|
if ($this->error !== UPLOAD_ERR_OK) { |
139
|
|
|
throw new RuntimeException('Cannot retrieve stream due to upload error'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (false !== is_string($targetPath)) { |
143
|
|
|
throw new InvalidArgumentException( |
144
|
|
|
'Invalid path provided for move operation; must be a string' |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (true === empty($targetPath)) { |
149
|
|
|
throw new InvalidArgumentException( |
150
|
|
|
'Invalid path provided for move operation; must be a non-empty string' |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (true === $this->moved) { |
155
|
|
|
throw new RuntimeException('Cannot move file; already moved!'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$status = rename($this->file, $targetPath); |
159
|
|
|
if (false === $status) { |
160
|
|
|
throw new RuntimeException('Cannot move file!'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$this->moved = true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function getSize() |
170
|
|
|
{ |
171
|
|
|
return $this->fileSize; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
* |
177
|
|
|
* @see http://php.net/manual/en/features.file-upload.errors.php |
178
|
|
|
* @return int One of PHP's UPLOAD_ERR_XXX constants. |
179
|
|
|
*/ |
180
|
|
|
public function getError() |
181
|
|
|
{ |
182
|
|
|
return $this->error; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
public function getClientFilename() |
189
|
|
|
{ |
190
|
|
|
return $this->clientFilename; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
|
|
public function getClientMediaType() |
197
|
|
|
{ |
198
|
|
|
return $this->clientMediaType; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|