Steps to reproduce:

  • Enter an absolute "File system path" for in admin/settings/file-system, for example "/home/jpetso/www/drupal-5/". (Where "/home/jpetso/www" is your Apache web root directory - or UserDir, in my case.) Set "Public" as download method.
  • Enable the upload module, and upload some file (say, file.txt) to a node, with listing enabled.
  • Look at the file URL which can be seen in the node edit form in "File attachments", and more importantly as a link in the node view file listing.
  • The link looks something like "http://localhost/~jakob/drupal-5//home/jakob/www/drupal-5/files/file.txt", and obviously doesn't work.
  • This patch is an attempt to make this use case work. It can't work if the file system path is not inside Drupal's root directory, because then we don't know if there's a valid URL at all. But if the path is inside Drupal's root directory, we can strip it from the file path, and file uploads also work with public downloads, not only with private ones.

    Please review.

Comments

jpetso’s picture

Btw, I tested the patch with all combinations of Public/Private downloads and absolute/relative paths. Everything still works as expected.

keith.smith’s picture

Status: Needs review » Needs work

patch no longer applies

# patch -p0 < fix-absolute-paths.patch
patching file includes/file.inc
Hunk #1 FAILED at 27.
1 out of 1 hunk FAILED -- saving rejects to file includes/file.inc.rej

c960657’s picture

A more general approach is to provide a hook for file_create_url() that allow users to specify their own logic for generating URL's for uploaded files. This will allow all kinds of custom solutions for file storage, including 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.

jpetso’s picture

@c960657: That would certainly be nice, but I'd consider your proposal a wishlist feature while this issue can be considered a bug. Those two should be separated, imho.

c960657’s picture

Perhaps the easiest would be to pass the URLs generated by file_create_url() through custom_url_rewrite() / custom_url_rewrite_outbound().

AFAICS there is currently no way to rewrite the URLs of uploaded files like there is for other files.

c960657’s picture

@jpetso: Sorry, I didn't see your comment before making my last comment. You are right - the hook stuff belongs in another issue.

jpetso’s picture

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

Moving to 7.x... although, I'm currently not so motivated to work on this in the near future.

drewish’s picture

c960657's approach sounds a lot like #214934: file_url() and hook_file_server()

also wanted to cross link to #238299: file_create_url should return valid URLs which has similar concerns.

drewish’s picture

Status: Needs work » Needs review
StatusFileSize
new1.84 KB

here's a re-roll of jpetso's patch.

keith.smith’s picture

Status: Needs review » Needs work

One small note on the comment:

+      // Strip file_directory_path from $path. We only include relative paths in urls.

I think we normally capitalize URL, and have used "URLs" in lots of other places in core.

jpetso’s picture

Status: Needs work » Needs review
StatusFileSize
new1.56 KB

Yup, you're right. That comment was the one that you can see at the top of the patch (the reroll does not delete it, while the original patch does) and "urls" has been replaced by "URLs" since I first posted this patch ages ago. Another reroll removes the comment at the top in favor of that in the "private downloads" section, and capitalizes "URLs".

Thanks for the reroll, drewish. Oh, and I haven't yet chimed in with the praise choral for hook_file(), so here's three cheers (or eight) for you! Go Andrew go.

jpetso’s picture

I'm so glad I wrote those steps to reproduce. Tested again, still works as advertised.

jpetso’s picture

StatusFileSize
new1.53 KB

Better version, also works with absolute file directory paths + relative file paths, that combination did not work before. And if you're asking yourself how in God's name I discovered *that* rare case, you're right if you're thinking of...

jpetso’s picture

StatusFileSize
new10.72 KB

Test cases! Yay.

jpetso’s picture

I'm not sure how to handle the static $clean_url variable in url(), though. Commenting out the appropriate test cases might be feasible for personal testing, but probably not for being committed to Drupal core. The test cases should probably go to a separate issue anyways, but hey, it's 2 o'clock in the night, I leave that to someone else.

drewish’s picture

jpetso’s picture

Er, oops. Well at least the tests are sufficiently disjunct so I don't have to be sorry for spending the time to write them.

Anonymous’s picture

Status: Needs review » Needs work

The last submitted patch failed testing.

jpetso’s picture

Status: Needs work » Needs review
StatusFileSize
new7.71 KB

Thanks to #356721: Regression: Test failures with clean URLs being fixed, we can now enable all the tests. I cleaned up the test class for better read- and maintainability, it's a lot cleaner now. The fix itself still applies and works, as can be witnessed by executing the tests.

Please review and commit! Thanks!

jpetso’s picture

Note: this issue is pretty different from #238299: file_create_url should return valid URLs, and the tests are too. I therefore labeled mine as "basic tests" (just making sure the URLs come out as expected in all the different public/private vs. clean/unclean URL scenarios), whereas the other issue's tests cover edge cases and actual file storage. Maybe it makes sense to have all of this in a single class, but I'm sure about that, and I think this patch can be perfectly RTBC even without all of that advanced stuff.

Status: Needs review » Needs work

The last submitted patch failed testing.

jpetso’s picture

o rly? whyz that?
the tests work at my place... can someone have a look where they fail? please?

jpetso’s picture

Status: Needs work » Needs review
StatusFileSize
new9.23 KB

oh, I see, I didn't include the actual fix in the previous patch, just the tests. here's the patch with both tests and fix. please review and commit?

Status: Needs review » Needs work

The last submitted patch failed testing.

franz’s picture

Version: 7.x-dev » 8.x-dev
Status: Needs work » Needs review
Issue tags: +Needs backport to D6, +Needs backport to D7

Let's update and revive this. If I can read correctly, the bug also existed on 6.x, right?

franz’s picture

Status: Needs review » Needs work
Issue tags: +Needs backport to D6, +Needs backport to D7

The last submitted patch, fix-absolute-paths-including-tests.patch, failed testing.

franz’s picture

bump, @jpetso, is this issue still active for D8, this needs to be re-rolled, the function changed a lot.

chemical’s picture

The function file_create_url() has been rewritten for D7 and is the same for D8.

The last item on the list in the issue summary might be solve by the patch I have written for #1377840: Caching does not properly respect protocol (a problem when dealing with https).

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kim.pepper’s picture

Issue summary: View changes
Status: Needs work » Closed (outdated)

Closing as outdated. Please re-open if this is still relevant.