PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Psr7 / UploadedFile.php

UploadedFile.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/Psr7/UploadedFile.php

153 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare (strict_types=1);
4 namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
5
6 use InvalidArgumentException;
7 use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface;
8 use Dudlewebs\WPMCS\s3\Psr\Http\Message\UploadedFileInterface;
9 use RuntimeException;
10 class UploadedFile implements UploadedFileInterface
11 {
12 private const ERROR_MAP = [\UPLOAD_ERR_OK => 'UPLOAD_ERR_OK', \UPLOAD_ERR_INI_SIZE => 'UPLOAD_ERR_INI_SIZE', \UPLOAD_ERR_FORM_SIZE => 'UPLOAD_ERR_FORM_SIZE', \UPLOAD_ERR_PARTIAL => 'UPLOAD_ERR_PARTIAL', \UPLOAD_ERR_NO_FILE => 'UPLOAD_ERR_NO_FILE', \UPLOAD_ERR_NO_TMP_DIR => 'UPLOAD_ERR_NO_TMP_DIR', \UPLOAD_ERR_CANT_WRITE => 'UPLOAD_ERR_CANT_WRITE', \UPLOAD_ERR_EXTENSION => 'UPLOAD_ERR_EXTENSION'];
13 /**
14 * @var string|null
15 */
16 private $clientFilename;
17 /**
18 * @var string|null
19 */
20 private $clientMediaType;
21 /**
22 * @var int
23 */
24 private $error;
25 /**
26 * @var string|null
27 */
28 private $file;
29 /**
30 * @var bool
31 */
32 private $moved = \false;
33 /**
34 * @var int|null
35 */
36 private $size;
37 /**
38 * @var StreamInterface|null
39 */
40 private $stream;
41 /**
42 * @param StreamInterface|string|resource $streamOrFile
43 */
44 public function __construct($streamOrFile, ?int $size, int $errorStatus, ?string $clientFilename = null, ?string $clientMediaType = null)
45 {
46 $this->setError($errorStatus);
47 $this->size = $size;
48 $this->clientFilename = $clientFilename;
49 $this->clientMediaType = $clientMediaType;
50 if ($this->isOk()) {
51 $this->setStreamOrFile($streamOrFile);
52 }
53 }
54 /**
55 * Depending on the value set file or stream variable
56 *
57 * @param StreamInterface|string|resource $streamOrFile
58 *
59 * @throws InvalidArgumentException
60 */
61 private function setStreamOrFile($streamOrFile) : void
62 {
63 if (\is_string($streamOrFile)) {
64 $this->file = $streamOrFile;
65 } elseif (\is_resource($streamOrFile)) {
66 $this->stream = new Stream($streamOrFile);
67 } elseif ($streamOrFile instanceof StreamInterface) {
68 $this->stream = $streamOrFile;
69 } else {
70 throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile');
71 }
72 }
73 /**
74 * @throws InvalidArgumentException
75 */
76 private function setError(int $error) : void
77 {
78 if (!isset(UploadedFile::ERROR_MAP[$error])) {
79 throw new InvalidArgumentException('Invalid error status for UploadedFile');
80 }
81 $this->error = $error;
82 }
83 private static function isStringNotEmpty($param) : bool
84 {
85 return \is_string($param) && \false === empty($param);
86 }
87 /**
88 * Return true if there is no upload error
89 */
90 private function isOk() : bool
91 {
92 return $this->error === \UPLOAD_ERR_OK;
93 }
94 public function isMoved() : bool
95 {
96 return $this->moved;
97 }
98 /**
99 * @throws RuntimeException if is moved or not ok
100 */
101 private function validateActive() : void
102 {
103 if (\false === $this->isOk()) {
104 throw new RuntimeException(\sprintf('Cannot retrieve stream due to upload error (%s)', self::ERROR_MAP[$this->error]));
105 }
106 if ($this->isMoved()) {
107 throw new RuntimeException('Cannot retrieve stream after it has already been moved');
108 }
109 }
110 public function getStream() : StreamInterface
111 {
112 $this->validateActive();
113 if ($this->stream instanceof StreamInterface) {
114 return $this->stream;
115 }
116 /** @var string $file */
117 $file = $this->file;
118 return new LazyOpenStream($file, 'r+');
119 }
120 public function moveTo($targetPath) : void
121 {
122 $this->validateActive();
123 if (\false === self::isStringNotEmpty($targetPath)) {
124 throw new InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
125 }
126 if ($this->file) {
127 $this->moved = \PHP_SAPI === 'cli' ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath);
128 } else {
129 Utils::copyToStream($this->getStream(), new LazyOpenStream($targetPath, 'w'));
130 $this->moved = \true;
131 }
132 if (\false === $this->moved) {
133 throw new RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath));
134 }
135 }
136 public function getSize() : ?int
137 {
138 return $this->size;
139 }
140 public function getError() : int
141 {
142 return $this->error;
143 }
144 public function getClientFilename() : ?string
145 {
146 return $this->clientFilename;
147 }
148 public function getClientMediaType() : ?string
149 {
150 return $this->clientMediaType;
151 }
152 }
153