Skip to content

Commit 495da2c

Browse files
Updated & fixed issues in the uploaded file class
1 parent 8681fd3 commit 495da2c

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/UploadedFile.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ class UploadedFile implements UploadedFileInterface
3232
/** @var int|null */
3333
private $size;
3434

35+
36+
/** @var bool */
37+
private $isMoved = false;
38+
3539
/**
3640
* @param resource|StreamInterface|string $streamOrFile
3741
*/
3842
public function __construct($streamOrFile, ?int $size, int $errorStatus = null, string $clientFilename = null, string $clientMediaType = null)
3943
{
40-
if ($streamOrFile instanceof StreamInterface) {
41-
$streamOrFile = $streamOrFile->detach();
44+
if (is_resource($streamOrFile)) {
45+
$streamOrFile = \stream_get_meta_data($streamOrFile)['uri'];
46+
} elseif ($streamOrFile instanceof StreamInterface) {
47+
$streamOrFile = $streamOrFile->getMetadata('uri');
4248
}
4349

4450
$this->size = $size;
@@ -48,27 +54,39 @@ public function __construct($streamOrFile, ?int $size, int $errorStatus = null,
4854
/**
4955
* Exchanges the underlying uploadedFile with another.
5056
*/
51-
public function withUploadFile(FileUploadedFile $uploadedFile): UploadedFileInterface
57+
public function withUploadedFile(FileUploadedFile $uploadedFile): UploadedFileInterface
5258
{
5359
$new = clone $this;
5460
$new->uploadedFile = $uploadedFile;
5561

5662
return $this;
5763
}
5864

65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function getUploadedFile(): FileUploadedFile
69+
{
70+
return $this->uploadedFile;
71+
}
72+
5973
/**
6074
* {@inheritdoc}
6175
*/
6276
public function getStream(): StreamInterface
6377
{
78+
if ($this->isMoved) {
79+
throw new \RuntimeException('The stream is not available because it has been moved.');
80+
}
81+
6482
if (null !== $this->stream) {
6583
return $this->stream;
6684
}
6785

6886
try {
69-
return $this->stream = new Stream(\fopen($this->uploadedFile->getPath(), 'r'));
87+
return $this->stream = new Stream($this->uploadedFile->getPath());
7088
} catch (\Throwable $e) {
71-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $this->file));
89+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $this->uploadedFile->getPath()));
7290
}
7391
}
7492

@@ -77,7 +95,12 @@ public function getStream(): StreamInterface
7795
*/
7896
public function moveTo($targetPath): void
7997
{
98+
if ($this->isMoved) {
99+
throw new \RuntimeException('The file cannot be moved because it has already been moved.');
100+
}
101+
80102
$this->uploadedFile->move($targetPath);
103+
$this->isMoved = true;
81104
}
82105

83106
/**

0 commit comments

Comments
 (0)