You are here

function file_entity_download_page in File Entity (fieldable files) 7.3

Same name and namespace in other branches
  1. 7.2 file_entity.pages.inc \file_entity_download_page()

Menu callback; download a single file entity.

1 string reference to 'file_entity_download_page'
file_entity_menu in ./file_entity.module
Implements hook_menu().

File

./file_entity.pages.inc, line 26
Supports file operations including View, Edit, and Delete.

Code

function file_entity_download_page($file) {

  // Ensure there is a valid token to download this file.
  if (!variable_get('file_entity_allow_insecure_download', FALSE)) {
    if (!isset($_GET['token']) || $_GET['token'] !== file_entity_get_download_token($file)) {
      return MENU_ACCESS_DENIED;
    }
  }

  // If the file does not exist it can cause problems with file_transfer().
  if (!is_file($file->uri)) {
    return MENU_NOT_FOUND;
  }

  // @todo Why basename? Why not drupal_basename which was working up until recently.
  $headers = array(
    'Content-Type' => mime_header_encode($file->filemime),
    'Content-Disposition' => 'attachment; filename="' . mime_header_encode(basename($file->uri)) . '"',
    'Content-Length' => $file->filesize,
    'Content-Transfer-Encoding' => 'binary',
    'Pragma' => 'no-cache',
    'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
    'Expires' => '0',
  );

  // Let other modules alter the download headers.
  drupal_alter('file_download_headers', $headers, $file);

  // Let other modules know the file is being downloaded.
  module_invoke_all('file_transfer', $file->uri, $headers);
  if (file_entity_file_is_local($file)) {

    // For local files, transfer the file and do not reveal the actual URL.
    file_transfer($file->uri, $headers);
  }
  else {
    if (file_uri_scheme($file->uri) == 's3') {
      $url = file_create_url($file->uri);
      drupal_goto($url);
    }
    else {

      // For remote files, just redirect the user to that file's actual URL.
      $headers['Location'] = file_create_url($file->uri);

      // If using S3 as a replacement for the file system, default headers
      // for downloading will cause the server to not respond. Remove them.
      if (module_exists('s3fs')) {
        unset($headers['Content-Length']);
        unset($headers['Content-Transfer-Encoding']);
      }
      foreach ($headers as $name => $value) {
        drupal_add_http_header($name, $value);
      }
      drupal_send_headers();
      drupal_exit();
    }
  }
}