| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,211 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+declare(strict_types=1); |
|
| 4 |
+ |
|
| 5 |
+namespace GuzzleHttp\Psr7; |
|
| 6 |
+ |
|
| 7 |
+use InvalidArgumentException; |
|
| 8 |
+use Psr\Http\Message\StreamInterface; |
|
| 9 |
+use Psr\Http\Message\UploadedFileInterface; |
|
| 10 |
+use RuntimeException; |
|
| 11 |
+ |
|
| 12 |
+class UploadedFile implements UploadedFileInterface |
|
| 13 |
+{
|
|
| 14 |
+ private const ERROR_MAP = [ |
|
| 15 |
+ UPLOAD_ERR_OK => 'UPLOAD_ERR_OK', |
|
| 16 |
+ UPLOAD_ERR_INI_SIZE => 'UPLOAD_ERR_INI_SIZE', |
|
| 17 |
+ UPLOAD_ERR_FORM_SIZE => 'UPLOAD_ERR_FORM_SIZE', |
|
| 18 |
+ UPLOAD_ERR_PARTIAL => 'UPLOAD_ERR_PARTIAL', |
|
| 19 |
+ UPLOAD_ERR_NO_FILE => 'UPLOAD_ERR_NO_FILE', |
|
| 20 |
+ UPLOAD_ERR_NO_TMP_DIR => 'UPLOAD_ERR_NO_TMP_DIR', |
|
| 21 |
+ UPLOAD_ERR_CANT_WRITE => 'UPLOAD_ERR_CANT_WRITE', |
|
| 22 |
+ UPLOAD_ERR_EXTENSION => 'UPLOAD_ERR_EXTENSION', |
|
| 23 |
+ ]; |
|
| 24 |
+ |
|
| 25 |
+ /** |
|
| 26 |
+ * @var string|null |
|
| 27 |
+ */ |
|
| 28 |
+ private $clientFilename; |
|
| 29 |
+ |
|
| 30 |
+ /** |
|
| 31 |
+ * @var string|null |
|
| 32 |
+ */ |
|
| 33 |
+ private $clientMediaType; |
|
| 34 |
+ |
|
| 35 |
+ /** |
|
| 36 |
+ * @var int |
|
| 37 |
+ */ |
|
| 38 |
+ private $error; |
|
| 39 |
+ |
|
| 40 |
+ /** |
|
| 41 |
+ * @var string|null |
|
| 42 |
+ */ |
|
| 43 |
+ private $file; |
|
| 44 |
+ |
|
| 45 |
+ /** |
|
| 46 |
+ * @var bool |
|
| 47 |
+ */ |
|
| 48 |
+ private $moved = false; |
|
| 49 |
+ |
|
| 50 |
+ /** |
|
| 51 |
+ * @var int|null |
|
| 52 |
+ */ |
|
| 53 |
+ private $size; |
|
| 54 |
+ |
|
| 55 |
+ /** |
|
| 56 |
+ * @var StreamInterface|null |
|
| 57 |
+ */ |
|
| 58 |
+ private $stream; |
|
| 59 |
+ |
|
| 60 |
+ /** |
|
| 61 |
+ * @param StreamInterface|string|resource $streamOrFile |
|
| 62 |
+ */ |
|
| 63 |
+ public function __construct( |
|
| 64 |
+ $streamOrFile, |
|
| 65 |
+ ?int $size, |
|
| 66 |
+ int $errorStatus, |
|
| 67 |
+ ?string $clientFilename = null, |
|
| 68 |
+ ?string $clientMediaType = null |
|
| 69 |
+ ) {
|
|
| 70 |
+ $this->setError($errorStatus); |
|
| 71 |
+ $this->size = $size; |
|
| 72 |
+ $this->clientFilename = $clientFilename; |
|
| 73 |
+ $this->clientMediaType = $clientMediaType; |
|
| 74 |
+ |
|
| 75 |
+ if ($this->isOk()) {
|
|
| 76 |
+ $this->setStreamOrFile($streamOrFile); |
|
| 77 |
+ } |
|
| 78 |
+ } |
|
| 79 |
+ |
|
| 80 |
+ /** |
|
| 81 |
+ * Depending on the value set file or stream variable |
|
| 82 |
+ * |
|
| 83 |
+ * @param StreamInterface|string|resource $streamOrFile |
|
| 84 |
+ * |
|
| 85 |
+ * @throws InvalidArgumentException |
|
| 86 |
+ */ |
|
| 87 |
+ private function setStreamOrFile($streamOrFile): void |
|
| 88 |
+ {
|
|
| 89 |
+ if (is_string($streamOrFile)) {
|
|
| 90 |
+ $this->file = $streamOrFile; |
|
| 91 |
+ } elseif (is_resource($streamOrFile)) {
|
|
| 92 |
+ $this->stream = new Stream($streamOrFile); |
|
| 93 |
+ } elseif ($streamOrFile instanceof StreamInterface) {
|
|
| 94 |
+ $this->stream = $streamOrFile; |
|
| 95 |
+ } else {
|
|
| 96 |
+ throw new InvalidArgumentException( |
|
| 97 |
+ 'Invalid stream or file provided for UploadedFile' |
|
| 98 |
+ ); |
|
| 99 |
+ } |
|
| 100 |
+ } |
|
| 101 |
+ |
|
| 102 |
+ /** |
|
| 103 |
+ * @throws InvalidArgumentException |
|
| 104 |
+ */ |
|
| 105 |
+ private function setError(int $error): void |
|
| 106 |
+ {
|
|
| 107 |
+ if (!isset(UploadedFile::ERROR_MAP[$error])) {
|
|
| 108 |
+ throw new InvalidArgumentException( |
|
| 109 |
+ 'Invalid error status for UploadedFile' |
|
| 110 |
+ ); |
|
| 111 |
+ } |
|
| 112 |
+ |
|
| 113 |
+ $this->error = $error; |
|
| 114 |
+ } |
|
| 115 |
+ |
|
| 116 |
+ private static function isStringNotEmpty($param): bool |
|
| 117 |
+ {
|
|
| 118 |
+ return is_string($param) && false === empty($param); |
|
| 119 |
+ } |
|
| 120 |
+ |
|
| 121 |
+ /** |
|
| 122 |
+ * Return true if there is no upload error |
|
| 123 |
+ */ |
|
| 124 |
+ private function isOk(): bool |
|
| 125 |
+ {
|
|
| 126 |
+ return $this->error === UPLOAD_ERR_OK; |
|
| 127 |
+ } |
|
| 128 |
+ |
|
| 129 |
+ public function isMoved(): bool |
|
| 130 |
+ {
|
|
| 131 |
+ return $this->moved; |
|
| 132 |
+ } |
|
| 133 |
+ |
|
| 134 |
+ /** |
|
| 135 |
+ * @throws RuntimeException if is moved or not ok |
|
| 136 |
+ */ |
|
| 137 |
+ private function validateActive(): void |
|
| 138 |
+ {
|
|
| 139 |
+ if (false === $this->isOk()) {
|
|
| 140 |
+ throw new RuntimeException(\sprintf('Cannot retrieve stream due to upload error (%s)', self::ERROR_MAP[$this->error]));
|
|
| 141 |
+ } |
|
| 142 |
+ |
|
| 143 |
+ if ($this->isMoved()) {
|
|
| 144 |
+ throw new RuntimeException('Cannot retrieve stream after it has already been moved');
|
|
| 145 |
+ } |
|
| 146 |
+ } |
|
| 147 |
+ |
|
| 148 |
+ public function getStream(): StreamInterface |
|
| 149 |
+ {
|
|
| 150 |
+ $this->validateActive(); |
|
| 151 |
+ |
|
| 152 |
+ if ($this->stream instanceof StreamInterface) {
|
|
| 153 |
+ return $this->stream; |
|
| 154 |
+ } |
|
| 155 |
+ |
|
| 156 |
+ /** @var string $file */ |
|
| 157 |
+ $file = $this->file; |
|
| 158 |
+ |
|
| 159 |
+ return new LazyOpenStream($file, 'r+'); |
|
| 160 |
+ } |
|
| 161 |
+ |
|
| 162 |
+ public function moveTo($targetPath): void |
|
| 163 |
+ {
|
|
| 164 |
+ $this->validateActive(); |
|
| 165 |
+ |
|
| 166 |
+ if (false === self::isStringNotEmpty($targetPath)) {
|
|
| 167 |
+ throw new InvalidArgumentException( |
|
| 168 |
+ 'Invalid path provided for move operation; must be a non-empty string' |
|
| 169 |
+ ); |
|
| 170 |
+ } |
|
| 171 |
+ |
|
| 172 |
+ if ($this->file) {
|
|
| 173 |
+ $this->moved = PHP_SAPI === 'cli' |
|
| 174 |
+ ? rename($this->file, $targetPath) |
|
| 175 |
+ : move_uploaded_file($this->file, $targetPath); |
|
| 176 |
+ } else {
|
|
| 177 |
+ Utils::copyToStream( |
|
| 178 |
+ $this->getStream(), |
|
| 179 |
+ new LazyOpenStream($targetPath, 'w') |
|
| 180 |
+ ); |
|
| 181 |
+ |
|
| 182 |
+ $this->moved = true; |
|
| 183 |
+ } |
|
| 184 |
+ |
|
| 185 |
+ if (false === $this->moved) {
|
|
| 186 |
+ throw new RuntimeException( |
|
| 187 |
+ sprintf('Uploaded file could not be moved to %s', $targetPath)
|
|
| 188 |
+ ); |
|
| 189 |
+ } |
|
| 190 |
+ } |
|
| 191 |
+ |
|
| 192 |
+ public function getSize(): ?int |
|
| 193 |
+ {
|
|
| 194 |
+ return $this->size; |
|
| 195 |
+ } |
|
| 196 |
+ |
|
| 197 |
+ public function getError(): int |
|
| 198 |
+ {
|
|
| 199 |
+ return $this->error; |
|
| 200 |
+ } |
|
| 201 |
+ |
|
| 202 |
+ public function getClientFilename(): ?string |
|
| 203 |
+ {
|
|
| 204 |
+ return $this->clientFilename; |
|
| 205 |
+ } |
|
| 206 |
+ |
|
| 207 |
+ public function getClientMediaType(): ?string |
|
| 208 |
+ {
|
|
| 209 |
+ return $this->clientMediaType; |
|
| 210 |
+ } |
|
| 211 |
+} |
| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,325 +0,0 @@ |
| 1 |
-<?php |
|
| 2 |
- |
|
| 3 |
-namespace GuzzleHttp\Psr7; |
|
| 4 |
- |
|
| 5 |
-use InvalidArgumentException; |
|
| 6 |
-use Psr\Http\Message\StreamInterface; |
|
| 7 |
-use Psr\Http\Message\UploadedFileInterface; |
|
| 8 |
-use RuntimeException; |
|
| 9 |
- |
|
| 10 |
-class UploadedFile implements UploadedFileInterface |
|
| 11 |
-{
|
|
| 12 |
- /** |
|
| 13 |
- * @var int[] |
|
| 14 |
- */ |
|
| 15 |
- private static $errors = [ |
|
| 16 |
- UPLOAD_ERR_OK, |
|
| 17 |
- UPLOAD_ERR_INI_SIZE, |
|
| 18 |
- UPLOAD_ERR_FORM_SIZE, |
|
| 19 |
- UPLOAD_ERR_PARTIAL, |
|
| 20 |
- UPLOAD_ERR_NO_FILE, |
|
| 21 |
- UPLOAD_ERR_NO_TMP_DIR, |
|
| 22 |
- UPLOAD_ERR_CANT_WRITE, |
|
| 23 |
- UPLOAD_ERR_EXTENSION, |
|
| 24 |
- ]; |
|
| 25 |
- |
|
| 26 |
- /** |
|
| 27 |
- * @var string |
|
| 28 |
- */ |
|
| 29 |
- private $clientFilename; |
|
| 30 |
- |
|
| 31 |
- /** |
|
| 32 |
- * @var string |
|
| 33 |
- */ |
|
| 34 |
- private $clientMediaType; |
|
| 35 |
- |
|
| 36 |
- /** |
|
| 37 |
- * @var int |
|
| 38 |
- */ |
|
| 39 |
- private $error; |
|
| 40 |
- |
|
| 41 |
- /** |
|
| 42 |
- * @var null|string |
|
| 43 |
- */ |
|
| 44 |
- private $file; |
|
| 45 |
- |
|
| 46 |
- /** |
|
| 47 |
- * @var bool |
|
| 48 |
- */ |
|
| 49 |
- private $moved = false; |
|
| 50 |
- |
|
| 51 |
- /** |
|
| 52 |
- * @var int |
|
| 53 |
- */ |
|
| 54 |
- private $size; |
|
| 55 |
- |
|
| 56 |
- /** |
|
| 57 |
- * @var StreamInterface|null |
|
| 58 |
- */ |
|
| 59 |
- private $stream; |
|
| 60 |
- |
|
| 61 |
- /** |
|
| 62 |
- * @param StreamInterface|string|resource $streamOrFile |
|
| 63 |
- * @param int $size |
|
| 64 |
- * @param int $errorStatus |
|
| 65 |
- * @param string|null $clientFilename |
|
| 66 |
- * @param string|null $clientMediaType |
|
| 67 |
- */ |
|
| 68 |
- public function __construct( |
|
| 69 |
- $streamOrFile, |
|
| 70 |
- $size, |
|
| 71 |
- $errorStatus, |
|
| 72 |
- $clientFilename = null, |
|
| 73 |
- $clientMediaType = null |
|
| 74 |
- ) {
|
|
| 75 |
- $this->setError($errorStatus); |
|
| 76 |
- $this->setSize($size); |
|
| 77 |
- $this->setClientFilename($clientFilename); |
|
| 78 |
- $this->setClientMediaType($clientMediaType); |
|
| 79 |
- |
|
| 80 |
- if ($this->isOk()) {
|
|
| 81 |
- $this->setStreamOrFile($streamOrFile); |
|
| 82 |
- } |
|
| 83 |
- } |
|
| 84 |
- |
|
| 85 |
- /** |
|
| 86 |
- * Depending on the value set file or stream variable |
|
| 87 |
- * |
|
| 88 |
- * @param mixed $streamOrFile |
|
| 89 |
- * |
|
| 90 |
- * @throws InvalidArgumentException |
|
| 91 |
- */ |
|
| 92 |
- private function setStreamOrFile($streamOrFile) |
|
| 93 |
- {
|
|
| 94 |
- if (is_string($streamOrFile)) {
|
|
| 95 |
- $this->file = $streamOrFile; |
|
| 96 |
- } elseif (is_resource($streamOrFile)) {
|
|
| 97 |
- $this->stream = new Stream($streamOrFile); |
|
| 98 |
- } elseif ($streamOrFile instanceof StreamInterface) {
|
|
| 99 |
- $this->stream = $streamOrFile; |
|
| 100 |
- } else {
|
|
| 101 |
- throw new InvalidArgumentException( |
|
| 102 |
- 'Invalid stream or file provided for UploadedFile' |
|
| 103 |
- ); |
|
| 104 |
- } |
|
| 105 |
- } |
|
| 106 |
- |
|
| 107 |
- /** |
|
| 108 |
- * @param int $error |
|
| 109 |
- * |
|
| 110 |
- * @throws InvalidArgumentException |
|
| 111 |
- */ |
|
| 112 |
- private function setError($error) |
|
| 113 |
- {
|
|
| 114 |
- if (false === is_int($error)) {
|
|
| 115 |
- throw new InvalidArgumentException( |
|
| 116 |
- 'Upload file error status must be an integer' |
|
| 117 |
- ); |
|
| 118 |
- } |
|
| 119 |
- |
|
| 120 |
- if (false === in_array($error, UploadedFile::$errors)) {
|
|
| 121 |
- throw new InvalidArgumentException( |
|
| 122 |
- 'Invalid error status for UploadedFile' |
|
| 123 |
- ); |
|
| 124 |
- } |
|
| 125 |
- |
|
| 126 |
- $this->error = $error; |
|
| 127 |
- } |
|
| 128 |
- |
|
| 129 |
- /** |
|
| 130 |
- * @param int $size |
|
| 131 |
- * |
|
| 132 |
- * @throws InvalidArgumentException |
|
| 133 |
- */ |
|
| 134 |
- private function setSize($size) |
|
| 135 |
- {
|
|
| 136 |
- if (false === is_int($size)) {
|
|
| 137 |
- throw new InvalidArgumentException( |
|
| 138 |
- 'Upload file size must be an integer' |
|
| 139 |
- ); |
|
| 140 |
- } |
|
| 141 |
- |
|
| 142 |
- $this->size = $size; |
|
| 143 |
- } |
|
| 144 |
- |
|
| 145 |
- /** |
|
| 146 |
- * @param mixed $param |
|
| 147 |
- * @return boolean |
|
| 148 |
- */ |
|
| 149 |
- private function isStringOrNull($param) |
|
| 150 |
- {
|
|
| 151 |
- return in_array(gettype($param), ['string', 'NULL']); |
|
| 152 |
- } |
|
| 153 |
- |
|
| 154 |
- /** |
|
| 155 |
- * @param mixed $param |
|
| 156 |
- * @return boolean |
|
| 157 |
- */ |
|
| 158 |
- private function isStringNotEmpty($param) |
|
| 159 |
- {
|
|
| 160 |
- return is_string($param) && false === empty($param); |
|
| 161 |
- } |
|
| 162 |
- |
|
| 163 |
- /** |
|
| 164 |
- * @param string|null $clientFilename |
|
| 165 |
- * |
|
| 166 |
- * @throws InvalidArgumentException |
|
| 167 |
- */ |
|
| 168 |
- private function setClientFilename($clientFilename) |
|
| 169 |
- {
|
|
| 170 |
- if (false === $this->isStringOrNull($clientFilename)) {
|
|
| 171 |
- throw new InvalidArgumentException( |
|
| 172 |
- 'Upload file client filename must be a string or null' |
|
| 173 |
- ); |
|
| 174 |
- } |
|
| 175 |
- |
|
| 176 |
- $this->clientFilename = $clientFilename; |
|
| 177 |
- } |
|
| 178 |
- |
|
| 179 |
- /** |
|
| 180 |
- * @param string|null $clientMediaType |
|
| 181 |
- * |
|
| 182 |
- * @throws InvalidArgumentException |
|
| 183 |
- */ |
|
| 184 |
- private function setClientMediaType($clientMediaType) |
|
| 185 |
- {
|
|
| 186 |
- if (false === $this->isStringOrNull($clientMediaType)) {
|
|
| 187 |
- throw new InvalidArgumentException( |
|
| 188 |
- 'Upload file client media type must be a string or null' |
|
| 189 |
- ); |
|
| 190 |
- } |
|
| 191 |
- |
|
| 192 |
- $this->clientMediaType = $clientMediaType; |
|
| 193 |
- } |
|
| 194 |
- |
|
| 195 |
- /** |
|
| 196 |
- * Return true if there is no upload error |
|
| 197 |
- * |
|
| 198 |
- * @return boolean |
|
| 199 |
- */ |
|
| 200 |
- private function isOk() |
|
| 201 |
- {
|
|
| 202 |
- return $this->error === UPLOAD_ERR_OK; |
|
| 203 |
- } |
|
| 204 |
- |
|
| 205 |
- /** |
|
| 206 |
- * @return boolean |
|
| 207 |
- */ |
|
| 208 |
- public function isMoved() |
|
| 209 |
- {
|
|
| 210 |
- return $this->moved; |
|
| 211 |
- } |
|
| 212 |
- |
|
| 213 |
- /** |
|
| 214 |
- * @throws RuntimeException if is moved or not ok |
|
| 215 |
- */ |
|
| 216 |
- private function validateActive() |
|
| 217 |
- {
|
|
| 218 |
- if (false === $this->isOk()) {
|
|
| 219 |
- throw new RuntimeException('Cannot retrieve stream due to upload error');
|
|
| 220 |
- } |
|
| 221 |
- |
|
| 222 |
- if ($this->isMoved()) {
|
|
| 223 |
- throw new RuntimeException('Cannot retrieve stream after it has already been moved');
|
|
| 224 |
- } |
|
| 225 |
- } |
|
| 226 |
- |
|
| 227 |
- /** |
|
| 228 |
- * {@inheritdoc}
|
|
| 229 |
- * |
|
| 230 |
- * @throws RuntimeException if the upload was not successful. |
|
| 231 |
- */ |
|
| 232 |
- public function getStream() |
|
| 233 |
- {
|
|
| 234 |
- $this->validateActive(); |
|
| 235 |
- |
|
| 236 |
- if ($this->stream instanceof StreamInterface) {
|
|
| 237 |
- return $this->stream; |
|
| 238 |
- } |
|
| 239 |
- |
|
| 240 |
- return new LazyOpenStream($this->file, 'r+'); |
|
| 241 |
- } |
|
| 242 |
- |
|
| 243 |
- /** |
|
| 244 |
- * {@inheritdoc}
|
|
| 245 |
- * |
|
| 246 |
- * @see http://php.net/is_uploaded_file |
|
| 247 |
- * @see http://php.net/move_uploaded_file |
|
| 248 |
- * |
|
| 249 |
- * @param string $targetPath Path to which to move the uploaded file. |
|
| 250 |
- * |
|
| 251 |
- * @throws RuntimeException if the upload was not successful. |
|
| 252 |
- * @throws InvalidArgumentException if the $path specified is invalid. |
|
| 253 |
- * @throws RuntimeException on any error during the move operation, or on |
|
| 254 |
- * the second or subsequent call to the method. |
|
| 255 |
- */ |
|
| 256 |
- public function moveTo($targetPath) |
|
| 257 |
- {
|
|
| 258 |
- $this->validateActive(); |
|
| 259 |
- |
|
| 260 |
- if (false === $this->isStringNotEmpty($targetPath)) {
|
|
| 261 |
- throw new InvalidArgumentException( |
|
| 262 |
- 'Invalid path provided for move operation; must be a non-empty string' |
|
| 263 |
- ); |
|
| 264 |
- } |
|
| 265 |
- |
|
| 266 |
- if ($this->file) {
|
|
| 267 |
- $this->moved = php_sapi_name() == 'cli' |
|
| 268 |
- ? rename($this->file, $targetPath) |
|
| 269 |
- : move_uploaded_file($this->file, $targetPath); |
|
| 270 |
- } else {
|
|
| 271 |
- Utils::copyToStream( |
|
| 272 |
- $this->getStream(), |
|
| 273 |
- new LazyOpenStream($targetPath, 'w') |
|
| 274 |
- ); |
|
| 275 |
- |
|
| 276 |
- $this->moved = true; |
|
| 277 |
- } |
|
| 278 |
- |
|
| 279 |
- if (false === $this->moved) {
|
|
| 280 |
- throw new RuntimeException( |
|
| 281 |
- sprintf('Uploaded file could not be moved to %s', $targetPath)
|
|
| 282 |
- ); |
|
| 283 |
- } |
|
| 284 |
- } |
|
| 285 |
- |
|
| 286 |
- /** |
|
| 287 |
- * {@inheritdoc}
|
|
| 288 |
- * |
|
| 289 |
- * @return int|null The file size in bytes or null if unknown. |
|
| 290 |
- */ |
|
| 291 |
- public function getSize() |
|
| 292 |
- {
|
|
| 293 |
- return $this->size; |
|
| 294 |
- } |
|
| 295 |
- |
|
| 296 |
- /** |
|
| 297 |
- * {@inheritdoc}
|
|
| 298 |
- * |
|
| 299 |
- * @see http://php.net/manual/en/features.file-upload.errors.php |
|
| 300 |
- * @return int One of PHP's UPLOAD_ERR_XXX constants. |
|
| 301 |
- */ |
|
| 302 |
- public function getError() |
|
| 303 |
- {
|
|
| 304 |
- return $this->error; |
|
| 305 |
- } |
|
| 306 |
- |
|
| 307 |
- /** |
|
| 308 |
- * {@inheritdoc}
|
|
| 309 |
- * |
|
| 310 |
- * @return string|null The filename sent by the client or null if none |
|
| 311 |
- * was provided. |
|
| 312 |
- */ |
|
| 313 |
- public function getClientFilename() |
|
| 314 |
- {
|
|
| 315 |
- return $this->clientFilename; |
|
| 316 |
- } |
|
| 317 |
- |
|
| 318 |
- /** |
|
| 319 |
- * {@inheritdoc}
|
|
| 320 |
- */ |
|
| 321 |
- public function getClientMediaType() |
|
| 322 |
- {
|
|
| 323 |
- return $this->clientMediaType; |
|
| 324 |
- } |
|
| 325 |
-} |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,325 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace GuzzleHttp\Psr7; |
|
| 4 |
+ |
|
| 5 |
+use InvalidArgumentException; |
|
| 6 |
+use Psr\Http\Message\StreamInterface; |
|
| 7 |
+use Psr\Http\Message\UploadedFileInterface; |
|
| 8 |
+use RuntimeException; |
|
| 9 |
+ |
|
| 10 |
+class UploadedFile implements UploadedFileInterface |
|
| 11 |
+{
|
|
| 12 |
+ /** |
|
| 13 |
+ * @var int[] |
|
| 14 |
+ */ |
|
| 15 |
+ private static $errors = [ |
|
| 16 |
+ UPLOAD_ERR_OK, |
|
| 17 |
+ UPLOAD_ERR_INI_SIZE, |
|
| 18 |
+ UPLOAD_ERR_FORM_SIZE, |
|
| 19 |
+ UPLOAD_ERR_PARTIAL, |
|
| 20 |
+ UPLOAD_ERR_NO_FILE, |
|
| 21 |
+ UPLOAD_ERR_NO_TMP_DIR, |
|
| 22 |
+ UPLOAD_ERR_CANT_WRITE, |
|
| 23 |
+ UPLOAD_ERR_EXTENSION, |
|
| 24 |
+ ]; |
|
| 25 |
+ |
|
| 26 |
+ /** |
|
| 27 |
+ * @var string |
|
| 28 |
+ */ |
|
| 29 |
+ private $clientFilename; |
|
| 30 |
+ |
|
| 31 |
+ /** |
|
| 32 |
+ * @var string |
|
| 33 |
+ */ |
|
| 34 |
+ private $clientMediaType; |
|
| 35 |
+ |
|
| 36 |
+ /** |
|
| 37 |
+ * @var int |
|
| 38 |
+ */ |
|
| 39 |
+ private $error; |
|
| 40 |
+ |
|
| 41 |
+ /** |
|
| 42 |
+ * @var null|string |
|
| 43 |
+ */ |
|
| 44 |
+ private $file; |
|
| 45 |
+ |
|
| 46 |
+ /** |
|
| 47 |
+ * @var bool |
|
| 48 |
+ */ |
|
| 49 |
+ private $moved = false; |
|
| 50 |
+ |
|
| 51 |
+ /** |
|
| 52 |
+ * @var int |
|
| 53 |
+ */ |
|
| 54 |
+ private $size; |
|
| 55 |
+ |
|
| 56 |
+ /** |
|
| 57 |
+ * @var StreamInterface|null |
|
| 58 |
+ */ |
|
| 59 |
+ private $stream; |
|
| 60 |
+ |
|
| 61 |
+ /** |
|
| 62 |
+ * @param StreamInterface|string|resource $streamOrFile |
|
| 63 |
+ * @param int $size |
|
| 64 |
+ * @param int $errorStatus |
|
| 65 |
+ * @param string|null $clientFilename |
|
| 66 |
+ * @param string|null $clientMediaType |
|
| 67 |
+ */ |
|
| 68 |
+ public function __construct( |
|
| 69 |
+ $streamOrFile, |
|
| 70 |
+ $size, |
|
| 71 |
+ $errorStatus, |
|
| 72 |
+ $clientFilename = null, |
|
| 73 |
+ $clientMediaType = null |
|
| 74 |
+ ) {
|
|
| 75 |
+ $this->setError($errorStatus); |
|
| 76 |
+ $this->setSize($size); |
|
| 77 |
+ $this->setClientFilename($clientFilename); |
|
| 78 |
+ $this->setClientMediaType($clientMediaType); |
|
| 79 |
+ |
|
| 80 |
+ if ($this->isOk()) {
|
|
| 81 |
+ $this->setStreamOrFile($streamOrFile); |
|
| 82 |
+ } |
|
| 83 |
+ } |
|
| 84 |
+ |
|
| 85 |
+ /** |
|
| 86 |
+ * Depending on the value set file or stream variable |
|
| 87 |
+ * |
|
| 88 |
+ * @param mixed $streamOrFile |
|
| 89 |
+ * |
|
| 90 |
+ * @throws InvalidArgumentException |
|
| 91 |
+ */ |
|
| 92 |
+ private function setStreamOrFile($streamOrFile) |
|
| 93 |
+ {
|
|
| 94 |
+ if (is_string($streamOrFile)) {
|
|
| 95 |
+ $this->file = $streamOrFile; |
|
| 96 |
+ } elseif (is_resource($streamOrFile)) {
|
|
| 97 |
+ $this->stream = new Stream($streamOrFile); |
|
| 98 |
+ } elseif ($streamOrFile instanceof StreamInterface) {
|
|
| 99 |
+ $this->stream = $streamOrFile; |
|
| 100 |
+ } else {
|
|
| 101 |
+ throw new InvalidArgumentException( |
|
| 102 |
+ 'Invalid stream or file provided for UploadedFile' |
|
| 103 |
+ ); |
|
| 104 |
+ } |
|
| 105 |
+ } |
|
| 106 |
+ |
|
| 107 |
+ /** |
|
| 108 |
+ * @param int $error |
|
| 109 |
+ * |
|
| 110 |
+ * @throws InvalidArgumentException |
|
| 111 |
+ */ |
|
| 112 |
+ private function setError($error) |
|
| 113 |
+ {
|
|
| 114 |
+ if (false === is_int($error)) {
|
|
| 115 |
+ throw new InvalidArgumentException( |
|
| 116 |
+ 'Upload file error status must be an integer' |
|
| 117 |
+ ); |
|
| 118 |
+ } |
|
| 119 |
+ |
|
| 120 |
+ if (false === in_array($error, UploadedFile::$errors)) {
|
|
| 121 |
+ throw new InvalidArgumentException( |
|
| 122 |
+ 'Invalid error status for UploadedFile' |
|
| 123 |
+ ); |
|
| 124 |
+ } |
|
| 125 |
+ |
|
| 126 |
+ $this->error = $error; |
|
| 127 |
+ } |
|
| 128 |
+ |
|
| 129 |
+ /** |
|
| 130 |
+ * @param int $size |
|
| 131 |
+ * |
|
| 132 |
+ * @throws InvalidArgumentException |
|
| 133 |
+ */ |
|
| 134 |
+ private function setSize($size) |
|
| 135 |
+ {
|
|
| 136 |
+ if (false === is_int($size)) {
|
|
| 137 |
+ throw new InvalidArgumentException( |
|
| 138 |
+ 'Upload file size must be an integer' |
|
| 139 |
+ ); |
|
| 140 |
+ } |
|
| 141 |
+ |
|
| 142 |
+ $this->size = $size; |
|
| 143 |
+ } |
|
| 144 |
+ |
|
| 145 |
+ /** |
|
| 146 |
+ * @param mixed $param |
|
| 147 |
+ * @return boolean |
|
| 148 |
+ */ |
|
| 149 |
+ private function isStringOrNull($param) |
|
| 150 |
+ {
|
|
| 151 |
+ return in_array(gettype($param), ['string', 'NULL']); |
|
| 152 |
+ } |
|
| 153 |
+ |
|
| 154 |
+ /** |
|
| 155 |
+ * @param mixed $param |
|
| 156 |
+ * @return boolean |
|
| 157 |
+ */ |
|
| 158 |
+ private function isStringNotEmpty($param) |
|
| 159 |
+ {
|
|
| 160 |
+ return is_string($param) && false === empty($param); |
|
| 161 |
+ } |
|
| 162 |
+ |
|
| 163 |
+ /** |
|
| 164 |
+ * @param string|null $clientFilename |
|
| 165 |
+ * |
|
| 166 |
+ * @throws InvalidArgumentException |
|
| 167 |
+ */ |
|
| 168 |
+ private function setClientFilename($clientFilename) |
|
| 169 |
+ {
|
|
| 170 |
+ if (false === $this->isStringOrNull($clientFilename)) {
|
|
| 171 |
+ throw new InvalidArgumentException( |
|
| 172 |
+ 'Upload file client filename must be a string or null' |
|
| 173 |
+ ); |
|
| 174 |
+ } |
|
| 175 |
+ |
|
| 176 |
+ $this->clientFilename = $clientFilename; |
|
| 177 |
+ } |
|
| 178 |
+ |
|
| 179 |
+ /** |
|
| 180 |
+ * @param string|null $clientMediaType |
|
| 181 |
+ * |
|
| 182 |
+ * @throws InvalidArgumentException |
|
| 183 |
+ */ |
|
| 184 |
+ private function setClientMediaType($clientMediaType) |
|
| 185 |
+ {
|
|
| 186 |
+ if (false === $this->isStringOrNull($clientMediaType)) {
|
|
| 187 |
+ throw new InvalidArgumentException( |
|
| 188 |
+ 'Upload file client media type must be a string or null' |
|
| 189 |
+ ); |
|
| 190 |
+ } |
|
| 191 |
+ |
|
| 192 |
+ $this->clientMediaType = $clientMediaType; |
|
| 193 |
+ } |
|
| 194 |
+ |
|
| 195 |
+ /** |
|
| 196 |
+ * Return true if there is no upload error |
|
| 197 |
+ * |
|
| 198 |
+ * @return boolean |
|
| 199 |
+ */ |
|
| 200 |
+ private function isOk() |
|
| 201 |
+ {
|
|
| 202 |
+ return $this->error === UPLOAD_ERR_OK; |
|
| 203 |
+ } |
|
| 204 |
+ |
|
| 205 |
+ /** |
|
| 206 |
+ * @return boolean |
|
| 207 |
+ */ |
|
| 208 |
+ public function isMoved() |
|
| 209 |
+ {
|
|
| 210 |
+ return $this->moved; |
|
| 211 |
+ } |
|
| 212 |
+ |
|
| 213 |
+ /** |
|
| 214 |
+ * @throws RuntimeException if is moved or not ok |
|
| 215 |
+ */ |
|
| 216 |
+ private function validateActive() |
|
| 217 |
+ {
|
|
| 218 |
+ if (false === $this->isOk()) {
|
|
| 219 |
+ throw new RuntimeException('Cannot retrieve stream due to upload error');
|
|
| 220 |
+ } |
|
| 221 |
+ |
|
| 222 |
+ if ($this->isMoved()) {
|
|
| 223 |
+ throw new RuntimeException('Cannot retrieve stream after it has already been moved');
|
|
| 224 |
+ } |
|
| 225 |
+ } |
|
| 226 |
+ |
|
| 227 |
+ /** |
|
| 228 |
+ * {@inheritdoc}
|
|
| 229 |
+ * |
|
| 230 |
+ * @throws RuntimeException if the upload was not successful. |
|
| 231 |
+ */ |
|
| 232 |
+ public function getStream() |
|
| 233 |
+ {
|
|
| 234 |
+ $this->validateActive(); |
|
| 235 |
+ |
|
| 236 |
+ if ($this->stream instanceof StreamInterface) {
|
|
| 237 |
+ return $this->stream; |
|
| 238 |
+ } |
|
| 239 |
+ |
|
| 240 |
+ return new LazyOpenStream($this->file, 'r+'); |
|
| 241 |
+ } |
|
| 242 |
+ |
|
| 243 |
+ /** |
|
| 244 |
+ * {@inheritdoc}
|
|
| 245 |
+ * |
|
| 246 |
+ * @see http://php.net/is_uploaded_file |
|
| 247 |
+ * @see http://php.net/move_uploaded_file |
|
| 248 |
+ * |
|
| 249 |
+ * @param string $targetPath Path to which to move the uploaded file. |
|
| 250 |
+ * |
|
| 251 |
+ * @throws RuntimeException if the upload was not successful. |
|
| 252 |
+ * @throws InvalidArgumentException if the $path specified is invalid. |
|
| 253 |
+ * @throws RuntimeException on any error during the move operation, or on |
|
| 254 |
+ * the second or subsequent call to the method. |
|
| 255 |
+ */ |
|
| 256 |
+ public function moveTo($targetPath) |
|
| 257 |
+ {
|
|
| 258 |
+ $this->validateActive(); |
|
| 259 |
+ |
|
| 260 |
+ if (false === $this->isStringNotEmpty($targetPath)) {
|
|
| 261 |
+ throw new InvalidArgumentException( |
|
| 262 |
+ 'Invalid path provided for move operation; must be a non-empty string' |
|
| 263 |
+ ); |
|
| 264 |
+ } |
|
| 265 |
+ |
|
| 266 |
+ if ($this->file) {
|
|
| 267 |
+ $this->moved = php_sapi_name() == 'cli' |
|
| 268 |
+ ? rename($this->file, $targetPath) |
|
| 269 |
+ : move_uploaded_file($this->file, $targetPath); |
|
| 270 |
+ } else {
|
|
| 271 |
+ Utils::copyToStream( |
|
| 272 |
+ $this->getStream(), |
|
| 273 |
+ new LazyOpenStream($targetPath, 'w') |
|
| 274 |
+ ); |
|
| 275 |
+ |
|
| 276 |
+ $this->moved = true; |
|
| 277 |
+ } |
|
| 278 |
+ |
|
| 279 |
+ if (false === $this->moved) {
|
|
| 280 |
+ throw new RuntimeException( |
|
| 281 |
+ sprintf('Uploaded file could not be moved to %s', $targetPath)
|
|
| 282 |
+ ); |
|
| 283 |
+ } |
|
| 284 |
+ } |
|
| 285 |
+ |
|
| 286 |
+ /** |
|
| 287 |
+ * {@inheritdoc}
|
|
| 288 |
+ * |
|
| 289 |
+ * @return int|null The file size in bytes or null if unknown. |
|
| 290 |
+ */ |
|
| 291 |
+ public function getSize() |
|
| 292 |
+ {
|
|
| 293 |
+ return $this->size; |
|
| 294 |
+ } |
|
| 295 |
+ |
|
| 296 |
+ /** |
|
| 297 |
+ * {@inheritdoc}
|
|
| 298 |
+ * |
|
| 299 |
+ * @see http://php.net/manual/en/features.file-upload.errors.php |
|
| 300 |
+ * @return int One of PHP's UPLOAD_ERR_XXX constants. |
|
| 301 |
+ */ |
|
| 302 |
+ public function getError() |
|
| 303 |
+ {
|
|
| 304 |
+ return $this->error; |
|
| 305 |
+ } |
|
| 306 |
+ |
|
| 307 |
+ /** |
|
| 308 |
+ * {@inheritdoc}
|
|
| 309 |
+ * |
|
| 310 |
+ * @return string|null The filename sent by the client or null if none |
|
| 311 |
+ * was provided. |
|
| 312 |
+ */ |
|
| 313 |
+ public function getClientFilename() |
|
| 314 |
+ {
|
|
| 315 |
+ return $this->clientFilename; |
|
| 316 |
+ } |
|
| 317 |
+ |
|
| 318 |
+ /** |
|
| 319 |
+ * {@inheritdoc}
|
|
| 320 |
+ */ |
|
| 321 |
+ public function getClientMediaType() |
|
| 322 |
+ {
|
|
| 323 |
+ return $this->clientMediaType; |
|
| 324 |
+ } |
|
| 325 |
+} |