You are here

function video_file_download in Video 6.5

Same name and namespace in other branches
  1. 7.2 video.module \video_file_download()
  2. 7 video.module \video_file_download()

Implementation of hook_file_download().

File

./video.module, line 395
Main file of the Video module.

Code

function video_file_download($filepath) {
  $full_path = file_create_path($filepath);

  // Video thumbnails
  if (strpos($filepath, 'video_thumbs/') !== FALSE) {
    return array(
      'Content-Type: ' . file_get_mimetype($full_path),
      'Content-Length: ' . filesize($full_path),
    );
  }

  // Only handle converted videos. Originals are handled by filefield_file_download().
  if (strpos($filepath, '/converted/') === FALSE) {
    return NULL;
  }

  // The get_video_by_converted_path() call is expensive, avoid it if the requested file is not a video
  $extensions = array_keys(video_video_extensions());
  $extension = pathinfo($filepath, PATHINFO_EXTENSION);
  if (!in_array($extension, $extensions)) {
    return NULL;
  }
  $transcoder = video_get_transcoder();
  $video = $transcoder
    ->get_original_path_by_converted_path($full_path);
  if ($video == NULL) {
    return NULL;
  }

  // If FileField has returned headers, then the video has passed all access
  // control requirements, so we know it's okay to display.
  $headers = filefield_file_download($video->filepath);
  if (!is_array($headers)) {
    return $headers;
  }

  // Look to see if the requested file is one of the converted files and return the headers.
  foreach ($video->data as $converted) {
    if ($converted->filepath == $full_path) {
      $name = mime_header_encode($converted->filename);
      $type = mime_header_encode($converted->filemime);
      return array(
        'Content-Type: ' . $type,
        'Content-Length: ' . $converted->filesize,
        'Content-Disposition: inline; filename="' . $name . '"',
        'Cache-Control: private',
      );
    }
  }
}