Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
When setting the stage_file_proxy_origin_dir to '/' or '', extra slashes appear in the URL
e.g: http://example.com//path/to/file.jpg
I understand this is quite specific to my use case but I'll attach a patch that prevents this in case anyone else has this issue
Comment | File | Size | Author |
---|---|---|---|
#2 | stage_file_proxy-double-slashes-1862796-2.patch | 1.29 KB | davidgrayston |
#1 | stage_file_proxy-double-slashes-1862796-1.patch.patch | 1.29 KB | davidgrayston |
Comments
Comment #1
davidgrayston CreditAttribution: davidgrayston commentedAttached patch to prevent double slashes
Comment #2
davidgrayston CreditAttribution: davidgrayston commentedAttached patch with correct filename to prevent double slashes
Comment #3
gregglesIt does seem uncommon to put your files in / but that should be supported.
Should we document somewhere that an empty string is the right value to use instead of /? Needs work for that, otherwise RTBC from me.
Comment #4
davidgrayston CreditAttribution: davidgrayston commentedI think it would make sense to document an empty string as being the correct value. This falls inline with the current documentation that states the default value is 'sites/default/files', which has no leading or trailing slash.
It should already work with either '/' or '' as slashes get trimmed anyway.
Comment #5
gregglesAnother thought: what about just doing a preg_replace of // for / - then we can remove any documentation that mentions the need to keep or remove a trailing slash.
Comment #6
davidgrayston CreditAttribution: davidgrayston commentedIt should already work if you have leading and trailing slashes as they are removed with this line:
$remote_file_dir = trim(variable_get('stage_file_proxy_origin_dir', $file_dir),'/');
So '/sites/default/files/' becomes 'sites/default/files' and '/' becomes ''
The problem was arising because the $remote_dir_path was being concatenated between 2 slashes even when it was empty:
$url = $server . '/' . $remote_dir_path . '/' . rawurlencode($filename);
The patch is a minor tweak to this:
$url = $server . '/' . ($remote_dir_path != '' ? $remote_dir_path . '/' : '') . rawurlencode($filename);
The patch could be changed to preg_replace // for / whenever a URL is constructed like this:
$url = $server . preg_replace('|//|', '/', '/' . $remote_dir_path . '/' . rawurlencode($filename));
It's an alternative way of achieving the same result, which do you prefer?
Comment #7
gregglesAh, good point. Maybe we remove that trim and switch to the preg_replace solution a bit later in the code? That seems most likely to work and cleanest.
Comment #8
Dave Reid#2351485: Use hook_file_url_alter() to fetch files (fixes incompatibility with picture module) would also resolve this.
Comment #9
markdorisonIs this still a valid issue with #2351485: Use hook_file_url_alter() to fetch files (fixes incompatibility with picture module) committed?
Comment #10
gregglesBased on lack of response I'm closing this as a duplicate of #2351485: Use hook_file_url_alter() to fetch files (fixes incompatibility with picture module).