Skip to content

Commit

Permalink
FileElement: Fix that uploaded files are not preserved across requests
Browse files Browse the repository at this point in the history
Also fixes a small validation issue where invalid uploaded files
were attempted to store which led to a stacktrace.

fixes #126
  • Loading branch information
nilmerg committed Aug 28, 2023
1 parent 5a70262 commit 28da52e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,6 @@ parameters:
count: 1
path: src/FormElement/FileElement.php

-
message: "#^Only iterables can be unpacked, array\\<array\\<Psr\\\\Http\\\\Message\\\\UploadedFileInterface\\>\\|Psr\\\\Http\\\\Message\\\\UploadedFileInterface\\>\\|Psr\\\\Http\\\\Message\\\\UploadedFileInterface given in argument \\#1\\.$#"
count: 1
path: src/FormElement/FileElement.php

-
message: "#^Parameter \\#1 \\$content of static method ipl\\\\Html\\\\Text\\:\\:create\\(\\) expects string, string\\|null given\\.$#"
count: 1
Expand Down
51 changes: 26 additions & 25 deletions src/FormElement/FileElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,6 @@ public function hasValue()
return $this->value !== null;
}

public function getValue()
{
if (! $this->hasValue()) {
return null;
}

if (! $this->hasFiles()) {
$files = $this->value;
if (! $this->isMultiple()) {
$files = [$files];
}

$storedFiles = $this->storeFiles(...$files);
if (! $this->isMultiple()) {
$storedFiles = $storedFiles[0];
}

$this->value = $storedFiles;
}

return $this->value;
}

public function setValue($value)
{
if (! empty($value)) {
Expand All @@ -154,7 +131,21 @@ public function setValue($value)
}

if ($fileToTest->getError() === UPLOAD_ERR_NO_FILE && ! $fileToTest->getClientFilename()) {
// This is checked here as it's only about file elements for which no value has been chosen
$value = null;
} else {
$files = $value;
if (! $this->isMultiple()) {
$files = [$files];
}

/** @var UploadedFileInterface[] $files */
$storedFiles = $this->storeFiles(...$files);
if (! $this->isMultiple()) {
$storedFiles = $storedFiles[0] ?? null;
}

$value = $storedFiles;
}
} else {
$value = null;
Expand Down Expand Up @@ -222,23 +213,33 @@ protected function storeFiles(UploadedFileInterface ...$files): array
return $files;
}

$storedFiles = [];
foreach ($files as $file) {
$name = $file->getClientFilename();
$path = $this->getFilePath($name);

if ($file->getError() !== UPLOAD_ERR_OK) {
// The file is still returned as otherwise it won't be validated
$storedFiles[] = $file;
continue;
}

$file->moveTo($path);

// Re-created to ensure moveTo() still works if called externally
$this->files[$name] = new UploadedFile(
$file = new UploadedFile(
$path,
$file->getSize(),
0,
$name,
$file->getClientMediaType()
);

$this->files[$name] = $file;
$storedFiles[] = $file;
}

return array_values($this->files);
return $storedFiles;
}

/**
Expand Down

0 comments on commit 28da52e

Please sign in to comment.