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).
Comment | File | Size | Author |
---|---|---|---|
#16 | custom_url_rewrite_outbound-4.patch | 9.47 KB | c960657 |
#14 | custom_url_rewrite_outbound-3.patch | 9.61 KB | c960657 |
#13 | custom_url_rewrite_outbound-2.patch | 9.7 KB | c960657 |
#10 | custom_url_rewrite_outbound-1.patch | 9.68 KB | c960657 |
#6 | file_3.patch | 1.95 KB | c960657 |
Comments
Comment #1
PanchoMoving feature requests to D7 queue. Sorry...
Comment #2
c960657 CreditAttribution: c960657 commentedThe 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 utilizescustom_url_rewrite_outbound()
.Comment #3
c960657 CreditAttribution: c960657 commentedUpdated 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.
Comment #4
Wim LeersSubscribing.
Comment #5
c960657 CreditAttribution: c960657 commentedLooks like I forgot to attach the patch in comment 3.
Comment #6
c960657 CreditAttribution: c960657 commentedChasing HEAD.
Comment #7
Susurrus CreditAttribution: Susurrus commentedThis should probably have some tests attached to it.
Comment #8
Summit CreditAttribution: Summit commentedSubscribing, greetings, Martijn
Comment #9
Anonymous (not verified) CreditAttribution: Anonymous commentedThe last submitted patch failed testing.
Comment #10
c960657 CreditAttribution: c960657 commentedUpdated 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.
Comment #11
Anonymous (not verified) CreditAttribution: Anonymous commentedThe last submitted patch failed testing.
Comment #12
c960657 CreditAttribution: c960657 commentedHmm, on my HEAD checkout all tests pass with this patch. Any ideas how to debug this?
Comment #13
c960657 CreditAttribution: c960657 commentedThe problem occured when Drupal was installed in a subdirectory. The test is now updated to use $base_path.
Comment #14
c960657 CreditAttribution: c960657 commentedComments removed (see #338403: Use {@inheritdoc} on all class methods (including tests)).
Comment #15
grendzy CreditAttribution: grendzy commentedI 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)
Comment #16
c960657 CreditAttribution: c960657 commentedReroll.
Comment #18
Dave ReidMight 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
Comment #19
grendzy CreditAttribution: grendzy commentedI believe this was fixed in #517814: File API Stream Wrapper Conversion.