Rename assets when they are upload


#1

Hello,

I want to rename my assets when they are upload. The goal is to remove all illegal characters.
I have already the perfect PHP code for this job, but all I need is to find a way to execute this code each time an asset is uploaded.

For example, if my customers upload the file “Céline is beautiful.jpg”, it should be automatically renamed “celine-is-beautiful.jpg”. And when I look at the tab Assets, the file should be saved as “celine-is-beautiful.jpg”, not “Céline is beautiful.jpg”.

So no more worries about space, illegal characters, ect.

Can you help me?

Best regards,
Jerome


#2

Hi,

you can add onAssetPreAdd listener eg like below:

public function onAssetPreAdd (ElementEventInterface $e)
    {        
        if ($e instanceof AssetEvent)
        {
            $asset=$e->getAsset();
            $assetName=$asset->getFilename();
            ...
            $asset->setFilename($newFilename);   
        }
    }

This will update the key of asset as well as the file in server.
I.


#3

Thanks for your help.

You are talking about listener. Do I have to add something in the services.yml ?

Jerome


#4

Create your listener eg MyAssetListener in AppBundle/EventListener folder and add this to services.yml:

    AppBundle\EventListener\MyAssetListener:
           tags:
                - { name: kernel.event_listener, event: pimcore.asset.preAdd, method: onAssetPreAdd }

MyAssetListener then might look like:

    <?php
    namespace AppBundle\EventListener;
      
    use Pimcore\Event\Model\ElementEventInterface;
    use Pimcore\Event\Model\AssetEvent;

    class MyAssetListener {
         
        public function onAssetPreAdd (ElementEventInterface $e)
        {        
            if ($e instanceof AssetEvent)
            {
                $asset=$e->getAsset();
                $assetName=$asset->getFilename();
                 …
                 $asset->setFilename($newFilename);       
            }
        }
    }

#5

Owwh! Thanks you SO MUCH!

For sure, this will help me (and other people) !

Jerome


#6

Hi,
you also could listen to this event … which is fired every time a element key is updated just before Pimcore is adjusting the element keys to the generic rules.

So this would also apply, when someone changes filenames after adding.

BR
Christian


#7

@mertaivan

I tested your code. It works well but it does not work if the user rename the Assets. This is a problem for the customers.

So I looked at the fash’s answer but I do not have enough knowledge to make it work, sorry.


#8

Hi @Kreeger,

That is a good idea to use this event. I tested and below works for me.

services.yml

AppBundle\EventListener\MyAssetListener:
        tags:
            - { name: kernel.event_listener, event: pimcore.system.service.preGetValidKey, method: onPreGetValidKey }

MyAssetListener.php


<?php
namespace AppBundle\EventListener;
      
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\AssetEvent;
use Symfony\Component\EventDispatcher\GenericEvent;

class MyAssetListener {
         
    public function onPreGetValidKey (GenericEvent $e)
    {        
        $type = $e->getArgument('type');
        if($type == 'asset')
        {
            $assetName = $e->getArgument('key');
             …
            $e->setArgument('key',$newAssetName));       
        }
    }
}

Best regards,
Ivan


#9

Hi Ivan,

Thanks for your help.
I am going to test your code on my next projects.


#10

Hi Ivan!

Your code works great when I upload a file or rename it!

But there is a little problem if I upload a file and the $newAssetName already exists. Is it possible to check if an asset already exists under the name $newAssetName and if yes, add something link “_copy” at the end of the filename, before the extension?

Jerome