Debian Codegraph @

lib/3rdParty/composer/nyholm/psr7/src/UploadedFile.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Nyholm\Psr7;
6
7 use Psr\Http\Message\{StreamInterface, UploadedFileInterface};
8
9 /**
10 * @author Michael Dowling and contributors to guzzlehttp/psr7
11 * @author Tobias Nyholm <[email protected]>
12 * @author Martijn van der Ven <[email protected]>
13 *
14 * @final This class should never be extended. See https://github.com/Nyholm/psr7/blob/master/doc/final.md
15 */
16 class UploadedFile implements UploadedFileInterface
17 {
18 /** @var array */
19 private const ERRORS = [
20 \UPLOAD_ERR_OK => 1,
21 \UPLOAD_ERR_INI_SIZE => 1,
22 \UPLOAD_ERR_FORM_SIZE => 1,
23 \UPLOAD_ERR_PARTIAL => 1,
24 \UPLOAD_ERR_NO_FILE => 1,
25 \UPLOAD_ERR_NO_TMP_DIR => 1,
26 \UPLOAD_ERR_CANT_WRITE => 1,
27 \UPLOAD_ERR_EXTENSION => 1,
28 ];
29
30 /** @var string */
31 private $clientFilename;
32
33 /** @var string */
34 private $clientMediaType;
35
36 /** @var int */
37 private $error;
38
39 /** @var string|null */
40 private $file;
41
42 /** @var bool */
43 private $moved = false;
44
45 /** @var int */
46 private $size;
47
48 /** @var StreamInterface|null */
49 private $stream;
50
51 /**
52 * @param StreamInterface|string|resource $streamOrFile
53 * @param int $size
54 * @param int $errorStatus
55 * @param string|null $clientFilename
56 * @param string|null $clientMediaType
57 */
58 public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null)
59 {
60 if (false === \is_int($errorStatus) || !isset(self::ERRORS[$errorStatus])) {
61 throw new \InvalidArgumentException('Upload file error status must be an integer value and one of the "UPLOAD_ERR_*" constants');
62 }
63
64 if (false === \is_int($size)) {
65 throw new \InvalidArgumentException('Upload file size must be an integer');
66 }
67
68 if (null !== $clientFilename && !\is_string($clientFilename)) {
69 throw new \InvalidArgumentException('Upload file client filename must be a string or null');
70 }
71
72 if (null !== $clientMediaType && !\is_string($clientMediaType)) {
73 throw new \InvalidArgumentException('Upload file client media type must be a string or null');
74 }
75
76 $this->error = $errorStatus;
77 $this->size = $size;
78 $this->clientFilename = $clientFilename;
79 $this->clientMediaType = $clientMediaType;
80
81 if (\UPLOAD_ERR_OK === $this->error) {
82 // Depending on the value set file or stream variable.
83 if (\is_string($streamOrFile) && '' !== $streamOrFile) {
84 $this->file = $streamOrFile;
85 } elseif (\is_resource($streamOrFile)) {
86 $this->stream = Stream::create($streamOrFile);
87 } elseif ($streamOrFile instanceof StreamInterface) {
88 $this->stream = $streamOrFile;
89 } else {
90 throw new \InvalidArgumentException('Invalid stream or file provided for UploadedFile');
91 }
92 }
93 }
94
95 /**
96 * @throws \RuntimeException if is moved or not ok
97 */
98 private function validateActive(): void
99 {
100 if (\UPLOAD_ERR_OK !== $this->error) {
101 throw new \RuntimeException('Cannot retrieve stream due to upload error');
102 }
103
104 if ($this->moved) {
105 throw new \RuntimeException('Cannot retrieve stream after it has already been moved');
106 }
107 }
108
109 public function getStream(): StreamInterface
110 {
111 $this->validateActive();
112
113 if ($this->stream instanceof StreamInterface) {
114 return $this->stream;
115 }
116
117 if (false === $resource = @\fopen($this->file, 'r')) {
118 throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $this->file, \error_get_last()['message'] ?? ''));
119 }
120
121 return Stream::create($resource);
122 }
123
124 public function moveTo($targetPath): void
125 {
126 $this->validateActive();
127
128 if (!\is_string($targetPath) || '' === $targetPath) {
129 throw new \InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
130 }
131
132 if (null !== $this->file) {
133 $this->moved = 'cli' === \PHP_SAPI ? @\rename($this->file, $targetPath) : @\move_uploaded_file($this->file, $targetPath);
134
135 if (false === $this->moved) {
136 throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, \error_get_last()['message'] ?? ''));
137 }
138 } else {
139 $stream = $this->getStream();
140 if ($stream->isSeekable()) {
141 $stream->rewind();
142 }
143
144 if (false === $resource = @\fopen($targetPath, 'w')) {
145 throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $targetPath, \error_get_last()['message'] ?? ''));
146 }
147
148 $dest = Stream::create($resource);
149
150 while (!$stream->eof()) {
151 if (!$dest->write($stream->read(1048576))) {
152 break;
153 }
154 }
155
156 $this->moved = true;
157 }
158 }
159
160 public function getSize(): int
161 {
162 return $this->size;
163 }
164
165 public function getError(): int
166 {
167 return $this->error;
168 }
169
170 public function getClientFilename(): ?string
171 {
172 return $this->clientFilename;
173 }
174
175 public function getClientMediaType(): ?string
176 {
177 return $this->clientMediaType;
178 }
179 }