8000 Use adapter for UploadedFile objects · symfony/symfony@a4f9f6d · GitHub
[go: up one dir, main page]

Skip to content

Commit a4f9f6d

Browse files
Use adapter for UploadedFile objects
1 parent ec7892b commit a4f9f6d

File tree

4 files changed

+79
-28
lines changed

4 files changed

+79
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CHANGELOG
22
=========
33

4-
* 1.3.0 (2019-11-26)
4+
* 1.3.0 (2019-11-25)
55

66
* Added support for streamed requests
77
* Added support for Symfony 5.0+

Factory/HttpFoundationFactory.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Psr\Http\Message\UriInterface;
1919
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
2020
use Symfony\Component\HttpFoundation\Cookie;
21-
use Symfony\Component\HttpFoundation\File\UploadedFile;
2221
use Symfony\Component\HttpFoundation\Request;
2322
use Symfony\Component\HttpFoundation\Response;
2423
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -78,10 +77,8 @@ public function createRequest(ServerRequestInterface $psrRequest, bool $streamed
7877

7978
/**
8079
* Converts to the input array to $_FILES structure.
81-
*
82-
* @return array
8380
*/
84-
private function getFiles(array $uploadedFiles)
81+
private function getFiles(array $uploadedFiles): array
8582
{
8683
$files = [];
8784

@@ -98,27 +95,10 @@ private function getFiles(array $uploadedFiles)
9895

9996
/**
10097
* Creates Symfony UploadedFile instance from PSR-7 ones.
101-
*
102-
* @return UploadedFile
10398
*/
104-
private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
99+
private function createUploadedFile(UploadedFileInterface $psrUploadedFile): UploadedFile
105100
{
106-
$temporaryPath = '';
107-
$clientFileName = '';
108-
if (UPLOAD_ERR_NO_FILE !== $psrUploadedFile->getError()) {
109-
$temporaryPath = $this->getTemporaryPath();
110-
$psrUploadedFile->moveTo($temporaryPath);
111-
112-
$clientFileName = $psrUploadedFile->getClientFilename();
113-
}
114-
115-
return new UploadedFile(
116-
$temporaryPath,
117-
null === $clientFileName ? '' : $clientFileName,
118-
$psrUploadedFile->getClientMediaType(),
119-
$psrUploadedFile->getError(),
120-
true
121-
);
101+
return new UploadedFile($psrUploadedFile, function () { return $this->getTemporaryPath(); });
122102
}
123103

124104
/**
@@ -167,11 +147,9 @@ public function createResponse(ResponseInterface $psrResponse, bool $streamed =
167147
*
168148
* Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
169149
*
170-
* @return Cookie
171-
*
172150
* @throws \InvalidArgumentException
173151
*/
174-
private function createCookie(string $cookie)
152+
private function createCookie(string $cookie): Cookie
175153
{
176154
foreach (explode(';', $cookie) as $part) {
177155
$part = trim($part);

Factory/UploadedFile.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PsrHttpMessage\Factory;
13+
14+
use Psr\Http\Message\UploadedFileInterface;
15+
use Symfony\Component\HttpFoundation\File\Exception\FileException;
16+
use Symfony\Component\HttpFoundation\File\File;
17+
use Symfony\Component\HttpFoundation\File\UploadedFile as BaseUploadedFile;
18+
19+
/**
20+
* @author Nicolas Grekas <p@tchwork.com>
21+
*/
22+
class UploadedFile extends BaseUploadedFile
23+
{
24+
private $psrUploadedFile;
25+
private $test = false;
26+
27+
public function __construct(UploadedFileInterface $psrUploadedFile, callable $getTemporaryPath)
28+
{
29+
$error = $psrUploadedFile->getError();
30+
$path = '';
31+
32+
if (UPLOAD_ERR_NO_FILE !== $error) {
33+
$path = $psrUploadedFile->getStream()->getMetadata('uri') ?? '';
34+
35+
if ($this->test = !\is_string($path) || !is_uploaded_file($path)) {
36+
$path = $getTemporaryPath();
37+
$psrUploadedFile->moveTo($path);
38+
}
39+
}
40+
41+
parent::__construct(
42+
$path,
43+
(string) $psrUploadedFile->getClientFilename(),
44+
$psrUploadedFile->getClientMediaType(),
45+
$psrUploadedFile->getError(),
46+
$this->test
47+
);
48+
49+
$this->psrUploadedFile = $psrUploadedFile;
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function move($directory, $name = null): File
56+
{
10229 57+
if (!$this->isValid() || $this->test) {
58+
return parent::move($directory, $name);
59+
}
60+
61+
$target = $this->getTargetFile($directory, $name);
62+
63+
try {
64+
$this->psrUploadedFile->moveTo($target);
65+
} catch (\RuntimeException $e) {
66+
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, $e->getMessage()), 0, $e);
67+
}
68+
69+
@chmod($target, 0666 & ~umask());
70+
71+
return $target;
72+
}
73+
}

Tests/Fixtures/UploadedFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct($filePath, $size = null, $error = UPLOAD_ERR_OK, $cl
3535

3636
public function getStream()
3737
{
38-
throw new \RuntimeException('No stream is available.');
38+
return new Stream(file_get_contents($this->filePath));
3939
}
4040

4141
public function moveTo($targetPath)

0 commit comments

Comments
 (0)
0