custom_url_rewrite_outbound() allows rewriting of URLs generated by url(). However, URLs of uploaded files are generated by file_create_url() and these are not subject to rewriting by this function.

It would be useful to be able to rewrite the URL of uploaded files also. In addition to the use cases of custom_url_rewrite_outbound() for url(), this is also useful when using various custom solutions for file storage, e.g. absolute paths to different disk partitions/disk drives, externally hosted files on e.g. Amazon S3 storage, files stored in a database rather than in the filesystem, site-specific file directories for multi-site installations, issues in multi-server setups etc.

I have attached a first shot at an implementation for D6. If people like this idea, I can make a patch for D5 as well (D5 uses custom_url_rewrite() instead of custom_url_rewrite_outbound(), so here a slightly different approach is needed).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Pancho’s picture

Version: 6.x-dev » 7.x-dev

Moving feature requests to D7 queue. Sorry...

c960657’s picture

The patch still applies to HEAD.

People wanting to rewrite file URLs in Drupal 6 can switch the download method to "private". This will make the URLs be generated using url() that utilizes custom_url_rewrite_outbound().

c960657’s picture

Status: Needs work » Needs review

Updated for HEAD.

Now also escapes $path as requested in #154245: problem with filenames with '%' character. I have tested this on Apache, but it probably also requires testing on other webservers to make sure that the %-encoding is handled properly as UTF-8.

Wim Leers’s picture

Subscribing.

c960657’s picture

FileSize
1.96 KB

Looks like I forgot to attach the patch in comment 3.

c960657’s picture

FileSize
1.95 KB

Chasing HEAD.

Susurrus’s picture

This should probably have some tests attached to it.

Summit’s picture

Subscribing, greetings, Martijn

Anonymous’s picture

Status: Needs review » Needs work

The last submitted patch failed testing.

c960657’s picture

Status: Needs work » Needs review
FileSize
9.68 KB

Updated patch - now with tests.

The tests are structured so that they can be easily merged with #238299: file_create_url should return valid URLs - please help review the patch over there too :-). There is also a small change to url() that allows changing the clean_url setting in the middle of a request (this change is discussed in #238299).

The tests aren't very extensive. custom_url_rewrite_outbound() should be thoroughly tested elsewhere. These tests only verify that file URLs are sent to custom_url_rewrite_outbound for rewriting. If I missed relevant used cases for file URL rewriting, please let me know.

Anonymous’s picture

Status: Needs review » Needs work

The last submitted patch failed testing.

c960657’s picture

Hmm, on my HEAD checkout all tests pass with this patch. Any ideas how to debug this?

c960657’s picture

Status: Needs work » Needs review
FileSize
9.7 KB

The problem occured when Drupal was installed in a subdirectory. The test is now updated to use $base_path.

c960657’s picture

grendzy’s picture

I definitely like the idea of making Drupal easier to integrate with a CDN. I've been tinkering with this a bit lately, and I didn't realize that you couldn't rewrite public file URLs. I've been working on a patch that let's files pass through url(), which would have the side effect of addressing this issue. #278770: file_create_url only returns absolute URL (containing the domain name)

c960657’s picture

Reroll.

Status: Needs review » Needs work

The last submitted patch failed testing.

Dave Reid’s picture

Issue tags: +custom_url_rewrite

Might be of interested to cross-link this with #320331: Turn custom_url_rewrite_inbound and custom_url_rewrite_outbound into hooks so we probably wouldn't need to create a new hook. Maybe file_create_url() could pass $options['file'] = TRUE to url() for use with the url_rewrite hooks

grendzy’s picture

Status: Needs work » Closed (duplicate)

I believe this was fixed in #517814: File API Stream Wrapper Conversion.