mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-03-09 08:48:26 +01:00
Generate internal filenames based on attachment name.
The filename of the uploaded file is now safed in the database.
This commit is contained in:
@@ -237,6 +237,7 @@ abstract class Attachment extends NamedDBElement
|
||||
public function setFilename(?string $new_filename): Attachment
|
||||
{
|
||||
$this->original_filename = $new_filename;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user