New hook triggered on file download

Created on 13 March 2008, almost 17 years ago
Updated 15 December 2023, about 1 year ago

In file.inc, file_download() invokes all hook_file_download hooks to determine if a file is allowed to be transfered and set transfer headers. But there is no hook invoked if the transfer is allowed to proceed. For example, let's say I want to be able to keep file download statistics, but only if a transfer is allowed. If I implement a hook_file_download, I don't know if hooks invoked before or after my module will deny the transfer, and my statistics will be off.

I suggest introducing a new hook (e.g. hook_file_transferring or something more appropriate) which is invoked on all modules in file_download() just before the call to file_transfer():

  if (file_exists(file_create_path($filepath))) {
    $headers = module_invoke_all('file_download', $filepath);
    if (in_array(-1, $headers)) {
        return drupal_access_denied();
    }
    if (count($headers)) {
        module_invoke_all('file_transferring', $filepath, $headers);  // This is the new invocation
        file_transfer($filepath, $headers);
    }

It's up to people smarter than me to determine what return values from hook_file_transferring would be useful and appropriate.

My apologies for wasting your time if this has already been suggested or implemented.

✨ Feature request
Status

Closed: outdated

Version

9.5

Component
File systemΒ  β†’

Last updated 33 minutes ago

Created by

πŸ‡ΊπŸ‡ΈUnited States fersman4

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs backport to D7

    After being applied to the 8.x branch, it should be considered for backport to the 7.x branch. Note: This tag should generally remain even after the backport has been written, approved, and committed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024