0

My problem is simple but complicated at the same time.

Basically when you upload a image with easy_admin. The image get's a hash.

Like so:

/uploads/images/5f17449f4932f_image004.jpg

Is there any way to remove the generated hash before the image name ?

Here is my Entity:

use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

 /**
 * Image
 *
 * @ORM\Column(type="string", length=255)
 * @var string
 */
private $image = '';

/**
 * ImageFile
 *
 * @Vich\UploadableField(mapping="images", fileNameProperty="image")
 * @var File
 */
private $imageFile;

Is there a setting that I may use in the easy_admin.yml config ?

 form:
    fields:
      - { property: 'imageFile', label: 'Image', type: 'vich_image'}

Let me know if any other information is needed. Thank you.

UPDATE: The class


namespace App\Service\FileNamer;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\PropertyMapping;
use Vich\UploaderBundle\Naming\NamerInterface;

class FileNamer implements NamerInterface
{
   public function name($object, PropertyMapping $mapping): string
   {
       /* @var $file UploadedFile */
       $file = $mapping->getFile($object);

       return  $file->getClientOriginalName();
   }
}

Easy_admin

    db_driver: orm
    mappings:
        images:
            uri_prefix:         '%upload_images_folder%'
            upload_destination: '%kernel.root_dir%/../public%upload_images_folder%'
            namer:
                service: App\Service\FileNamer
        videos:
            uri_prefix:         '%upload_videos_folder%'
            upload_destination: '%kernel.root_dir%/../public%upload_videos_folder%'
            namer:
                service: vich_uploader.namer_origname
        pdfs:
            uri_prefix:         '%upload_pdfs_folder%'
            upload_destination: '%kernel.root_dir%/../public%upload_pdfs_folder%'
            namer:
                service: vich_uploader.namer_origname
MewTwo
  • 465
  • 4
  • 14
  • The reason hash is added to file names is to prevent conflicts when files with same name are uploaded, I guess you understand this. So the question is: what do you want to achieve? E.g. what is the reason you want hash removed? – Desert Fox Jul 21 '20 at 20:05
  • Well it would just make my life more easier. Do you have any tipps ? – MewTwo Jul 21 '20 at 20:39

1 Answers1

2

Create your own custom namer class. Just implement Vich\UploaderBundle\Naming\NamerInterface and add it to vich_uploader configuration. https://github.com/dustin10/VichUploaderBundle/blob/master/docs/file_namer/howto/create_a_custom_file_namer.md

Leprechaun
  • 759
  • 5
  • 14
  • Hey when i try your method i get the following when i try to debug:container ```Service "App\Service\FileNamer.images": Parent definition "App\Service\FileNamer" does not exist.``` . Here is how i added it ```namer: service: App\Service\FileNamer``` – MewTwo Jul 22 '20 at 08:02
  • Did you create class `App\Naming\MyNamer`? – Leprechaun Jul 22 '20 at 08:28
  • It's called ```namespace App\Service\FileNamer;``` – MewTwo Jul 22 '20 at 08:29
  • Yes, sorry. So you created `App\Service\FileNamer` that is implementing `Vich\UploaderBundle\Naming\NamerInterface`, right? – Leprechaun Jul 22 '20 at 08:31
  • Hey i added it to the post. So you can see it. Should clear things up :D – MewTwo Jul 22 '20 at 08:32
  • You need to register `FileNamer` as a service and refer to it by service name. Check this for reference: https://stackoverflow.com/questions/48441537/vichuploaderbundle-namer-parent-definition-does-not-exist – Leprechaun Jul 22 '20 at 08:45