From 67fe47bbb9d9e6a22689cb08144e8e0145257570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Tue, 24 Sep 2019 18:39:11 +0200 Subject: [PATCH] Generate internal filenames based on attachment name. The filename of the uploaded file is now safed in the database. --- src/Entity/Attachments/Attachment.php | 1 + src/Services/AttachmentHelper.php | 17 +++-------------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/Entity/Attachments/Attachment.php b/src/Entity/Attachments/Attachment.php index b2b92772..409320e1 100644 --- a/src/Entity/Attachments/Attachment.php +++ b/src/Entity/Attachments/Attachment.php @@ -237,6 +237,7 @@ abstract class Attachment extends NamedDBElement public function setFilename(?string $new_filename): Attachment { $this->original_filename = $new_filename; + return $this; } /** diff --git a/src/Services/AttachmentHelper.php b/src/Services/AttachmentHelper.php index a7033b8f..743dab7d 100644 --- a/src/Services/AttachmentHelper.php +++ b/src/Services/AttachmentHelper.php @@ -261,20 +261,7 @@ class AttachmentHelper //Sanatize filename $originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); - $safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename); - $newFilename = $safeFilename . '.' . $file->getClientOriginalExtension(); - - //If a file with this name is already existing add a number to the filename - if (file_exists($folder . DIRECTORY_SEPARATOR . $newFilename)) { - $bak = $newFilename; - - $number = 1; - $newFilename = $folder . DIRECTORY_SEPARATOR . $safeFilename . '-' . $number . '.' . $file->getClientOriginalExtension(); - while (file_exists($newFilename)) { - $number++; - $newFilename = $folder . DIRECTORY_SEPARATOR . $safeFilename . '-' . $number . '.' . $file->getClientOriginalExtension(); - } - } + $newFilename = $attachment->getName() . '-' . uniqid('', false) . '.' . $file->guessExtension(); //Move our temporay attachment to its final location $file_path = $file->move($folder, $newFilename)->getRealPath(); @@ -284,6 +271,8 @@ class AttachmentHelper //Save the path to the attachment $attachment->setPath($file_path); + //And save original filename + $attachment->setFilename($file->getClientOriginalName()); return $attachment; }