First we need to be aware that there are differnet ways to store images in shopware. You can store them on your local file system, or you can store them in Amazon S3 or Google Cloud Platform buckets.

For this article we’ll look into how the files are stored when they are save on the local filesystem.

Path strategies

In Shopware6 there are 4 different path strategies that can be choosen in the core.

The path strategy needs to be choosen when you install the store, and it is rather hard to change the strategy on a current installation. If you need to change the strategy you will need to rename all the files on the disk.

The default in Shopware6 is the physical_filename strategy. This can be changed by setting the env variable SHOPWARE_CDN_STRATEGY_DEFAULT.

The strategies

So what are the different strategies that can be used, and how do they work ?

Common for all strategies is that the physical path for the media enteties are not stored in the database, but are calculated each time you need a media entity.

The urls for media is calculated like this:

        return $this->toPathString([
            'media',
            $this->pathnameStrategy->generatePathHash($media),
            $this->pathnameStrategy->generatePathCacheBuster($media),
            $this->pathnameStrategy->generatePhysicalFilename($media),
        ]);

generatePathCashBuster - This part is the upload time in unixtimestamp. So if a file was uploaded on 4th of May 2022 at 13:37:00 the timestamp here would be 1651664220

generatePhysicalFilename - Uses the filename and extension from the database and returns the full name.

The only differences between the built-in strategies are the generatePathHash function.

physical_filename

Generate a md5-hash based on the uploaded timestamp and the name of the file. It then uses the 6 first characters and add a slash for every two character.

Example: You have a media where the timestamp is 1651664220 and the filename is superproduct. This strategy will do md5('1651664220/superproduct') and you will get the result 1af5f762a63c7f48ced23376a3dc5b8b where you will use 1af5f7 and the result for the generatePathHash function will be 1a/f5/f7 You will then end up with the full path media/1a/f5/f7/1651664220/superproduct.jpg

filename

Generate a hash based on the filename

Example: You have a media where the filename is superproduct.jpg. This strategy will do md5('superproduct') and you will get the result 3fe87fc4ca7c159664484ee79631ec8e where you will use 3fe87f and the result for the generatePathHash function will be 3f/e8/7f You will then end up with the full path media/3f/e8/7f/1651664220/superproduct.jpg

plain

Generate no hash

Example: You have a media where the filename is superproduct.jpg. You will then end up with the full path media/1651664220/superproduct.jpg

id

Generate a hash based on the ID of the media entity

Example: You have a media where the filename is superproduct.jpg and the id of the media entity is 897cd16e55b7450787192d100cc8320d. This strategy will do md5('897cd16e55b7450787192d100cc8320d') and you will get the result 400daaacfe5308e03f3dea7e4816e29e where you will use 400daa and the result for the generatePathHash function will be 40/0d/aa You will then end up with the full path media/40/0d/aa/1651664220/superproduct.jpg