12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace Symfony\Bridge\PsrHttpMessage\Factory;
- use Psr\Http\Message\UploadedFileInterface;
- use Symfony\Component\HttpFoundation\File\Exception\FileException;
- use Symfony\Component\HttpFoundation\File\File;
- use Symfony\Component\HttpFoundation\File\UploadedFile as BaseUploadedFile;
- class UploadedFile extends BaseUploadedFile
- {
- private $psrUploadedFile;
- private $test = false;
- public function __construct(UploadedFileInterface $psrUploadedFile, callable $getTemporaryPath)
- {
- $error = $psrUploadedFile->getError();
- $path = '';
- if (\UPLOAD_ERR_NO_FILE !== $error) {
- $path = $psrUploadedFile->getStream()->getMetadata('uri') ?? '';
- if ($this->test = !\is_string($path) || !is_uploaded_file($path)) {
- $path = $getTemporaryPath();
- $psrUploadedFile->moveTo($path);
- }
- }
- parent::__construct(
- $path,
- (string) $psrUploadedFile->getClientFilename(),
- $psrUploadedFile->getClientMediaType(),
- $psrUploadedFile->getError(),
- $this->test
- );
- $this->psrUploadedFile = $psrUploadedFile;
- }
-
- public function move($directory, $name = null): File
- {
- if (!$this->isValid() || $this->test) {
- return parent::move($directory, $name);
- }
- $target = $this->getTargetFile($directory, $name);
- try {
- $this->psrUploadedFile->moveTo($target);
- } catch (\RuntimeException $e) {
- throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, $e->getMessage()), 0, $e);
- }
- @chmod($target, 0666 & ~umask());
- return $target;
- }
- }
|