From 640736e009f81cc47b24944527910fa32b8e8cb6 Mon Sep 17 00:00:00 2001 From: Mathieu De Keyzer Date: Wed, 25 Oct 2023 18:37:51 +0200 Subject: [PATCH] fix: uniq id does not change on each FormData::getAllFiles call --- EMS/form-bundle/src/Submission/FormData.php | 10 ++++-- .../src/Submission/FormDataFile.php | 33 +++++-------------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/EMS/form-bundle/src/Submission/FormData.php b/EMS/form-bundle/src/Submission/FormData.php index 43e56beb0..812968e10 100644 --- a/EMS/form-bundle/src/Submission/FormData.php +++ b/EMS/form-bundle/src/Submission/FormData.php @@ -17,6 +17,8 @@ final class FormData private array $raw; /** @var array */ private array $files = []; + /** @var FormDataFile[] */ + private array $allFiles = []; /** * @param FormInterface $form @@ -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); @@ -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 diff --git a/EMS/form-bundle/src/Submission/FormDataFile.php b/EMS/form-bundle/src/Submission/FormDataFile.php index 3af56635b..dccbfb61f 100644 --- a/EMS/form-bundle/src/Submission/FormDataFile.php +++ b/EMS/form-bundle/src/Submission/FormDataFile.php @@ -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 @@ -29,35 +37,12 @@ public function getFile(): UploadedFile /** @return array */ 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; - } }