New hook triggered on file download

fersman4 - March 13, 2008 - 22:36
Project:Drupal
Version:7.x-dev
Component:file system
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

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.

 
 

Drupal is a registered trademark of Dries Buytaert.