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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

davidgrayston’s picture

Attached patch to prevent double slashes

davidgrayston’s picture

Status: Active » Needs review
FileSize
1.29 KB

Attached patch with correct filename to prevent double slashes

greggles’s picture

Status: Needs review » Needs work

It 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.

davidgrayston’s picture

I 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.

greggles’s picture

Another 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.

davidgrayston’s picture

It 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?

greggles’s picture

Ah, 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.

Dave Reid’s picture

markdorison’s picture

Status: Needs work » Postponed (maintainer needs more info)
greggles’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)