You are here

file_force.module in File Force Download 6

Same filename and directory in other branches
  1. 5 file_force.module
  2. 6.2 file_force.module
  3. 7 file_force.module

file_force.module

File Force module.

File

file_force.module
View source
<?php

/**
 * @file file_force.module
 * 
 * File Force module.
 */

/**
 * Implementation of hook_theme().
 */
function file_force_theme() {
  $theme = array();

  // FileField formatters
  if (module_exists('filefield')) {
    $theme['file_force_formatter_default_ff'] = array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'file_force_formatter.inc',
    );
    $theme['file_force_formatter_url_plain_ff'] = array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'file_force_formatter.inc',
    );
    $theme['file_force_item'] = array(
      'arguments' => array(
        'file' => NULL,
        'field' => NULL,
      ),
      'file' => 'file_force_formatter.inc',
    );
    $theme['file_force_file'] = array(
      'arguments' => array(
        'file' => NULL,
      ),
      'file' => 'file_force_formatter.inc',
    );
  }

  // ImageField formatters
  if (module_exists('imagefield')) {
    $theme['file_force_formatter_image_imagelink_ff'] = array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'file_force_formatter.inc',
    );
  }

  // ImageCache formatters
  if (module_exists('imagecache')) {
    foreach (imagecache_presets() as $preset) {
      $theme['file_force_formatter_' . $preset['presetname'] . '_imagelink_ff'] = array(
        'arguments' => array(
          'element' => NULL,
        ),
        'file' => 'file_force_formatter.inc',
        'function' => 'theme_file_force_formatter_imagecache_imagelink_ff',
      );
    }
  }
  return $theme;
}

/**
 * Implementation of hook_file_download().
 *
 * This is what adds the headers which activates the force downloading.
 */
function file_force_file_download($filepath) {
  if (!isset($_GET['download'])) {

    // Our menu hook wasn't called, so we should ignore this.
    return NULL;
  }
  $filepath = file_create_path($filepath);
  return array(
    'Content-Type: application/octet-stream',
    'Content-Disposition: attachment; filename="' . basename($filepath) . '";',
    // Content-Length is also a good header to send, as it allows the browser to
    // display a progress bar correctly.
    // There's a trick for determining the file size for files over 2 GB. Nobody
    // should be using this module with files that large, but… the sprintf()
    // trickery makes sure the value is correct for files larger than 2GB. See
    // note at http://php.net/filesize
    'Content-Length: ' . sprintf('%u', filesize($filepath)),
  );
}

/**
 * Implementation of hook_field_formatter_info().
 *
 * Add file_force formatters to CCK file/image fields if
 * the filefield/imagefield.module exists.
 */
function file_force_field_formatter_info() {
  $formatters = array();

  // Handle filefield files.
  if (module_exists('filefield')) {
    $formatters['default_ff'] = array(
      'label' => t('File Force: Generic files'),
      'field types' => array(
        'filefield',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
      'description' => t('Displays all kinds of files with an icon and a linked file description.'),
    );
    $formatters['url_plain_ff'] = array(
      'label' => t('File Force: URL to file'),
      'field types' => array(
        'filefield',
      ),
      'description' => t('Displays a full URL to the file.'),
    );
  }

  // Handle imagefield files.
  if (module_exists('imagefield')) {
    $formatters['image_imagelink_ff'] = array(
      'label' => t('File Force: Image linked to file'),
      'field types' => array(
        'image',
        'filefield',
      ),
      'description' => t('Displays image files in their original size.'),
    );
  }

  // Handle imagecache.
  if (module_exists('imagecache')) {
    foreach (imagecache_presets() as $preset) {
      $formatters[$preset['presetname'] . '_imagelink_ff'] = array(
        'label' => t('File Force: @preset image linked to image', array(
          '@preset' => $preset['presetname'],
        )),
        'field types' => array(
          'image',
          'filefield',
        ),
      );
    }
  }
  return $formatters;
}

Functions

Namesort descending Description
file_force_field_formatter_info Implementation of hook_field_formatter_info().
file_force_file_download Implementation of hook_file_download().
file_force_theme Implementation of hook_theme().