You are here

function image_file_download in Image 6

Same name and namespace in other branches
  1. 5.2 image.module \image_file_download()
  2. 5 image.module \image_file_download()

Implementation of hook_file_download().

Note that in Drupal 5, the upload.module's hook_file_download() checks its permissions for all files in the {files} table. We store our file information in {files} if private files transfers are selected and the upload.module is enabled, users will the 'view uploaded files' permission to view images.

File

./image.module, line 189

Code

function image_file_download($filename) {
  $filepath = file_create_path($filename);
  $result = db_query("SELECT i.nid, f.filemime, f.filesize FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE f.filepath = '%s'", $filepath);
  if ($file = db_fetch_object($result)) {
    $node = node_load(array(
      'type' => 'image',
      'nid' => $file->nid,
    ));
    if (node_access('view', $node)) {

      // The user either needs to have 'view original images' permission or
      // the path must be listed for something other than the node's original
      // size. This will be the case when the orignal is smaller than a
      // derivative size.
      $images = (array) $node->images;
      unset($images[IMAGE_ORIGINAL]);
      if (user_access('view original images') || in_array($filepath, $images)) {
        return array(
          'Content-Type: ' . mime_header_encode($file->filemime),
          'Content-Length: ' . (int) $file->filesize,
        );
      }
    }
    return -1;
  }
}