mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-30 13:34:28 +02:00
Use the name of an uploaded file for an attachment when no explicit name was set.
This commit is contained in:
parent
4a30819ea5
commit
52e459ec60
3 changed files with 40 additions and 6 deletions
|
@ -25,6 +25,7 @@ namespace App\Entity\Attachments;
|
|||
use App\Entity\Base\AbstractNamedDBElement;
|
||||
use App\Validator\Constraints\Selectable;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use function in_array;
|
||||
use InvalidArgumentException;
|
||||
|
@ -95,6 +96,14 @@ abstract class Attachment extends AbstractNamedDBElement
|
|||
*/
|
||||
protected string $path = '';
|
||||
|
||||
/**
|
||||
* @var string the name of this element
|
||||
* @ORM\Column(type="string")
|
||||
* @Assert\NotBlank(message="validator.attachment.name_not_blank")
|
||||
* @Groups({"simple", "extended", "full"})
|
||||
*/
|
||||
protected string $name = '';
|
||||
|
||||
/**
|
||||
* ORM mapping is done in sub classes (like PartAttachment).
|
||||
*/
|
||||
|
|
|
@ -44,6 +44,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
|||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Validator\Constraints\File;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
use Symfony\Component\Validator\Constraints\Url;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
|
@ -72,8 +73,11 @@ class AttachmentFormType extends AbstractType
|
|||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder->add('name', TextType::class, [
|
||||
$builder
|
||||
->add('name', TextType::class, [
|
||||
'label' => 'attachment.edit.name',
|
||||
'required' => false,
|
||||
'empty_data' => '',
|
||||
])
|
||||
->add('attachment_type', StructuralEntityType::class, [
|
||||
'label' => 'attachment.edit.attachment_type',
|
||||
|
@ -134,6 +138,7 @@ class AttachmentFormType extends AbstractType
|
|||
],
|
||||
]);
|
||||
|
||||
|
||||
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event): void {
|
||||
$form = $event->getForm();
|
||||
$attachment = $form->getData();
|
||||
|
@ -141,13 +146,27 @@ class AttachmentFormType extends AbstractType
|
|||
$file_form = $form->get('file');
|
||||
$file = $file_form->getData();
|
||||
|
||||
if ($attachment instanceof Attachment && $file instanceof UploadedFile && $attachment->getAttachmentType(
|
||||
) && !$this->submitHandler->isValidFileExtension($attachment->getAttachmentType(), $file)) {
|
||||
if (!$attachment instanceof Attachment) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$file instanceof UploadedFile) {
|
||||
return;
|
||||
}
|
||||
|
||||
//Ensure that the file extension is allowed for the selected attachment type
|
||||
if ($attachment->getAttachmentType()
|
||||
&& !$this->submitHandler->isValidFileExtension($attachment->getAttachmentType(), $file)) {
|
||||
$event->getForm()->get('file')->addError(
|
||||
new FormError($this->translator->trans('validator.file_ext_not_allowed'))
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
//If the name is empty, use the original file name as attachment name
|
||||
if (empty($attachment->getName())) {
|
||||
$attachment->setName($file->getClientOriginalName());
|
||||
}
|
||||
}, 100000);
|
||||
|
||||
//Check the secure file checkbox, if file is in securefile location
|
||||
$builder->get('secureFile')->addEventListener(
|
||||
|
|
|
@ -299,5 +299,11 @@
|
|||
<target>The part name does not match the regular expression stated by the category: %regex%</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="m8kMFhf" name="validator.attachment.name_not_blank">
|
||||
<segment>
|
||||
<source>validator.attachment.name_not_blank</source>
|
||||
<target>Set a value here, or upload a file to automatically use its filename as name for the attachment.</target>
|
||||
</segment>
|
||||
</unit>
|
||||
</file>
|
||||
</xliff>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue