Skip to content

Commit

Permalink
fix: uniq id does not change on each FormData::getAllFiles call
Browse files Browse the repository at this point in the history
  • Loading branch information
theus77 committed Oct 25, 2023
1 parent ec622fe commit 640736e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
10 changes: 7 additions & 3 deletions EMS/form-bundle/src/Submission/FormData.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ final class FormData
private array $raw;
/** @var array<string, UploadedFile> */
private array $files = [];
/** @var FormDataFile[] */
private array $allFiles = [];

/**
* @param FormInterface<FormInterface> $form
Expand All @@ -37,7 +39,9 @@ public function raw(): array
/** @return FormDataFile[] */
public function getAllFiles(): array
{
$files = [];
if (!empty($this->allFiles)) {
return $this->allFiles;
}

foreach ($this->raw as $formField => $value) {
$element = $this->formConfig->getElementByName($formField);
Expand All @@ -50,12 +54,12 @@ public function getAllFiles(): array

foreach ($uploadedFiles as $uploadedFile) {
if ($uploadedFile instanceof UploadedFile) {
$files[] = new FormDataFile($uploadedFile, $element);
$this->allFiles[] = new FormDataFile($uploadedFile, $element);
}
}
}

return $files;
return $this->allFiles;
}

public function filesAsUUid(): void
Expand Down
33 changes: 9 additions & 24 deletions EMS/form-bundle/src/Submission/FormDataFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@

final class FormDataFile
{
private string $filename;

public function __construct(private readonly UploadedFile $file, private readonly ElementInterface $formElement)
{
$filename = $file->getClientOriginalName();
$extension = MimeTypes::getDefault()->getExtensions($file->getClientMimeType())[0] ?? null;
if (null !== $extension && !\str_ends_with(\strtolower($filename), ".$extension")) {
$filename .= \sprintf('.%s', $extension);
}
$this->filename = \sprintf('%s.%s', \uniqid(\sprintf('%s.', $this->formElement->getName()), false), $filename);
}

public function base64(): ?string
Expand All @@ -29,35 +37,12 @@ public function getFile(): UploadedFile
/** @return array<string, int|string|false|null> */
public function toArray(): array
{
$fileName = $this->getFilename($this->file, $this->formElement->getName());

return [
'filename' => $fileName,
'filename' => $this->filename,
'pathname' => $this->file->getPathname(),
'mimeType' => $this->file->getMimeType(),
'size' => $this->file->getSize(),
'form_field' => $this->formElement->getName(),
];
}

private function getFilename(UploadedFile $uploadedFile, string $fieldName): string
{
$filename = $uploadedFile->getClientOriginalName();
$extension = MimeTypes::getDefault()->getExtensions($uploadedFile->getClientMimeType())[0] ?? null;
if (null !== $extension && !$this->helperEndsWith($filename, $extension)) {
$filename .= \sprintf('.%s', $extension);
}

return \sprintf('%s.%s', \uniqid(\sprintf('%s.', $fieldName), false), $filename);
}

private function helperEndsWith(string $haystack, string $needle): bool
{
$length = \strlen($needle);
if (0 === $length) {
return true;
}

return \substr($haystack, -$length) === $needle;
}
}

0 comments on commit 640736e

Please sign in to comment.