You are here

alt_stream_wrappers.module in Alternative Stream Wrappers 7

Same filename and directory in other branches
  1. 8 alt_stream_wrappers.module

Provides one or more configurable alternative stream wrappers for file storage.

File

alt_stream_wrappers.module
View source
<?php

/**
 * @file
 * Provides one or more configurable alternative stream wrappers for
 * file storage.
 * 
 * @see https://drupal.org/node/560424
 * @see https://api.drupal.org/api/drupal/includes!stream_wrappers.inc/7
 */

// These constants are for convenience, but probably will not be defined
//  in time to be used in settings.php
if (!defined('ALT_STREAM_WRAPPERS_HIDDEN') && !defined('ALT_STREAM_WRAPPERS_NORMAL')) {
  define('ALT_STREAM_WRAPPERS_HIDDEN', 13);
  define('ALT_STREAM_WRAPPERS_NORMAL', 29);
}

/**
 * Implements hook_stream_wrappers().
 */
function alt_stream_wrappers_stream_wrappers() {
  $default = array(
    'alt-temp' => array(
      'name' => t('Alternative temporary files'),
      'class' => 'DrupalAltStreamWrapper',
      'description' => t('Alternative temporary local files.'),
      'type' => STREAM_WRAPPERS_LOCAL_HIDDEN,
    ),
  );
  $all_wrappers = variable_get('alt_stream_wrappers_wrappers', $default);
  $configured_wrappers = array();
  foreach ($all_wrappers as $scheme => $wrapper) {

    // Only register the stream wrapper if a file path has been set.
    if (variable_get('alt_stream_wrappers_' . $scheme . '_path', FALSE)) {
      $configured_wrappers[$scheme] = $wrapper;
    }
  }
  return $configured_wrappers;
}

/**
 * Drupal alternative stream wrapper class.
 *
 * Provides support for alternative file storage locations.
 * 
 * This is a generic class which should work for all of the one or more
 * Alternative Stream Wrappers provided by this module.
 *
 * Extends DrupalLocalStreamWrapper.
 */
class DrupalAltStreamWrapper extends DrupalLocalStreamWrapper {

  /**
   * Overrides getLocalPath().
   */
  protected function getLocalPath($uri = NULL) {
    if (empty($this->uri) && !empty($uri)) {

      // sometimes necessary for the file_uri_scheme and variable_get
      //  in DrupalAltStreamWrapper's getDirectoryPath method to work
      //  when called from parent::getLocalPath
      $this->uri = $uri;
    }
    return parent::getLocalPath($uri);
  }

  /**
   * Implements abstract public function getDirectoryPath()
   */
  public function getDirectoryPath() {
    $scheme = file_uri_scheme($this->uri);

    // todo: is temp a sensible default here or should it just be FALSE?
    return variable_get('alt_stream_wrappers_' . $scheme . '_path', file_directory_temp());
  }

  /**
   * Overrides getExternalUrl().
   */
  public function getExternalUrl() {
    $scheme = file_uri_scheme($this->uri);

    // What kind of wrapper is this?
    $wrappers = file_get_stream_wrappers();
    $wrapper = isset($wrappers[$scheme]) ? $wrappers[$scheme] : FALSE;
    if ($wrapper) {
      if ($wrapper['type'] & STREAM_WRAPPERS_LOCAL_NORMAL) {

        // Same as public:// stream wrapper
        $path = str_replace('\\', '/', $this
          ->getTarget());
        return $GLOBALS['base_url'] . '/' . self::getDirectoryPath() . '/' . drupal_encode_path($path);
      }
      else {

        // Go through the system module like temporary:// and private://
        //  In order for this file to be downloadable, a module will have to
        //  implement hook_file_download() and return headers for this scheme.
        $path = str_replace('\\', '/', $this
          ->getTarget());
        return url('system/' . $scheme . '/' . $path, array(
          'absolute' => TRUE,
        ));
      }
    }
  }

}

Functions

Classes

Namesort descending Description
DrupalAltStreamWrapper Drupal alternative stream wrapper class.