PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor_prefixed / guzzlehttp / psr7 / src / UploadedFile.php

UploadedFile.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor_prefixed/guzzlehttp/psr7/src/UploadedFile.php

132 lines 4.6 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 WCPOS\Vendor\GuzzleHttp\Psr7;
5
6 use InvalidArgumentException;
7 use WCPOS\Vendor\Psr\Http\Message\StreamInterface;
8 use WCPOS\Vendor\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 private ?string $clientFilename;
14 private ?string $clientMediaType;
15 private int $error;
16 private ?string $file = null;
17 private bool $moved = \false;
18 private ?int $size;
19 private ?StreamInterface $stream = null;
20 /**
21 * @param StreamInterface|string|resource $streamOrFile
22 */
23 public function __construct($streamOrFile, ?int $size, int $errorStatus, ?string $clientFilename = null, ?string $clientMediaType = null)
24 {
25 $this->setError($errorStatus);
26 $this->size = Integers::assertOptionalNonNegativeSize($size, 'Uploaded file size');
27 $this->clientFilename = $clientFilename;
28 $this->clientMediaType = $clientMediaType;
29 if ($this->isOk()) {
30 $this->setStreamOrFile($streamOrFile);
31 }
32 }
33 /**
34 * Depending on the value set file or stream variable
35 *
36 * @param StreamInterface|string|resource $streamOrFile
37 *
38 * @throws InvalidArgumentException
39 */
40 private function setStreamOrFile($streamOrFile) : void
41 {
42 if (\is_string($streamOrFile)) {
43 $this->file = $streamOrFile;
44 } elseif (\is_resource($streamOrFile)) {
45 $this->stream = new Stream($streamOrFile);
46 } elseif ($streamOrFile instanceof StreamInterface) {
47 $this->stream = $streamOrFile;
48 } else {
49 throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile');
50 }
51 }
52 /**
53 * @throws InvalidArgumentException
54 */
55 private function setError(int $error) : void
56 {
57 if (!isset(UploadedFile::ERROR_MAP[$error])) {
58 throw new InvalidArgumentException('Invalid error status for UploadedFile');
59 }
60 $this->error = $error;
61 }
62 /**
63 * Return true if there is no upload error
64 */
65 private function isOk() : bool
66 {
67 return $this->error === \UPLOAD_ERR_OK;
68 }
69 public function isMoved() : bool
70 {
71 return $this->moved;
72 }
73 /**
74 * @throws RuntimeException if is moved or not ok
75 */
76 private function validateActive() : void
77 {
78 if (\false === $this->isOk()) {
79 throw new RuntimeException(\sprintf('Cannot retrieve stream due to upload error (%s)', self::ERROR_MAP[$this->error]));
80 }
81 if ($this->isMoved()) {
82 throw new RuntimeException('Cannot retrieve stream after it has already been moved');
83 }
84 }
85 public function getStream() : StreamInterface
86 {
87 $this->validateActive();
88 if ($this->stream instanceof StreamInterface) {
89 return $this->stream;
90 }
91 /** @var string $file */
92 $file = $this->file;
93 return new LazyOpenStream($file, 'r+');
94 }
95 public function moveTo(string $targetPath) : void
96 {
97 $this->validateActive();
98 if ($targetPath === '') {
99 throw new InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
100 }
101 if ($this->file) {
102 $this->moved = \PHP_SAPI === 'cli' ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath);
103 } else {
104 $stream = $this->getStream();
105 if ($stream->isSeekable()) {
106 $stream->rewind();
107 }
108 Utils::copyToStream($stream, new LazyOpenStream($targetPath, 'w'));
109 $this->moved = \true;
110 }
111 if (\false === $this->moved) {
112 throw new RuntimeException(\sprintf('Uploaded file could not be moved to %s', DiagnosticValue::escape($targetPath)));
113 }
114 }
115 public function getSize() : ?int
116 {
117 return $this->size;
118 }
119 public function getError() : int
120 {
121 return $this->error;
122 }
123 public function getClientFilename() : ?string
124 {
125 return $this->clientFilename;
126 }
127 public function getClientMediaType() : ?string
128 {
129 return $this->clientMediaType;
130 }
131 }
132