Fix: Sanitize cache keys to avoid reserved characters validation error by camilleislasse · Pull Request #39 · jolicode/MediaBundle
Problem
When uploading files to subfolders (e.g., user-uploads/2024/image.jpg), cache keys generated by MediaPropertyAccessor and MediaVariationPropertyAccessor contain forward slashes (/), which are reserved characters in PSR-6 cache implementations.
This causes errors like:
This causes validation errors with real cache adapters:
Cache key "joli_media_property_default_subfolder/file.jpg_lastModified" contains reserved characters "{}()/:"
Solution
- Created
CacheKeySanitizerhelper class to sanitize cache keys by replacing PSR-6 reserved characters ({}()/\@:) with underscores - Applied sanitization to
libraryName,path, andvariation->getName()in cache key generation - The sanitization only affects cache keys, not the actual file paths used to access files
Testing
To test with a real cache adapter, replace the mock in tests/src/BaseTestCase.php with:
$cache = new ArrayAdapter();
This will show the cache key validation errors before the fix, and confirm they're resolved after.