fix: encode percent signs in filenames when generating URL#3930
fix: encode percent signs in filenames when generating URL#3930freekmurze merged 4 commits intospatie:mainfrom
Conversation
freekmurze
left a comment
There was a problem hiding this comment.
Thanks for the PR, and for catching the bug. I don't think we can merge this as-is though, because getPathRelativeToRoot() is used for much more than URL generation. Adding rawurlencode() there will break filesystem operations for any file whose name contains a %.
Callers of getPathRelativeToRoot() that are not URL-related:
DefaultUrlGenerator::getPath()– returns an absolute filesystem path (getRootOfDisk() . getPathRelativeToRoot())Support\FileRemover\DefaultFileRemoverandFileBaseFileRemover– delete files from diskMediaCollections\Filesystem::copyToMediaLibrary()– copies source filesMedia::getDiskSize()– callsStorage::disk(...)->size(->getPathRelativeToRoot(...))Media::toMailAttachment()– builds an email attachment from the disk path
With this change, a file saved as IMG_5405%20copy.jpg on disk would have its URL correct, but deleting it, getting its size, attaching it to a mail, or reading its absolute path would all fail because the disk would be asked for IMG_5405%2520copy.jpg.
The encoding should happen only when building a URL. Two things to change:
- Encode in the URL-generating methods instead of in
getPathRelativeToRoot(). The most targeted spots areDefaultUrlGenerator::getUrl()andDefaultUrlGenerator::getTemporaryUrl(). You could either URL-encode the path segment by segment there, or add a small helper likegetUrlEncodedPathRelativeToRoot()on the base class that the URL generator uses.rawurlencodeon the whole path is wrong because it would also encode the/separators; split on/, encode each segment, and join again. - The conversion branch has the same issue. A conversion URL for a media with
%in the name (or a conversion file name that contains one) is currently unfixed. The encoding needs to apply there too.
Also small nits in the test file: missing newline at end of file, and a blank line before the new it(...) block to match the file style.
Happy to help once those are addressed.
|
@freekmurze Thanks for the detailed feedback! I've addressed all the points:
|
freekmurze
left a comment
There was a problem hiding this comment.
Thanks for addressing the feedback — this looks good now. Encoding happens only in the URL-generating methods, per-segment, so filesystem operations are unaffected and conversion URLs are covered via the same helper.
|
@moemadeldin @freekmurze This change causes some of images (thumbs - conversions) not to shown anymore over S3. This seems weird to me. This breaks the path into multiple segments, but I don't know for sure if S3 has issues with this? Could it be file names are already encoded on creation, and now it cannot make a match anymore? |
|
@francoism90 Thanks for catching this! Tracked it down and opened #3933 with a proposed fix. |
Fixes #3926