You are here

flysystem.module in Flysystem 7

Provides access to various filesystem backends using Flysystem.

File

flysystem.module
View source
<?php

/**
 * @file
 * Provides access to various filesystem backends using Flysystem.
 */
use Drupal\flysystem\FlysystemFactory;
use Drupal\flysystem\Flysystem\Local;

/**
 * Implements hook_menu().
 */
function flysystem_menu() {
  $items = array();
  $items['_flysystem/%'] = array(
    'title' => 'Flysystem file download',
    'page callback' => 'file_download',
    'page arguments' => array(
      1,
    ),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/media/file-system/settings'] = array(
    'title' => 'Settings',
    'file path' => drupal_get_path('module', 'system'),
    'weight' => -10,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/config/media/file-system/flysystem'] = array(
    'title' => 'Flysystem',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'flysystem_config_form',
    ),
    'access arguments' => array(
      'administer flysystem',
    ),
    'file' => 'flysystem.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  if (module_exists('image')) {
    $items['_flysystem/%/styles/%image_style'] = array(
      'title' => 'Generate image style',
      'page callback' => 'image_style_deliver',
      'page arguments' => array(
        3,
      ),
      'access callback' => TRUE,
      'type' => MENU_CALLBACK,
    );
    if (flysystem_dependencies_check()) {
      foreach (variable_get('flysystem', array()) as $scheme => $conf) {
        $plugin = flysystem_factory()
          ->getPlugin($scheme);
        if ($plugin instanceof Local && ($public_path = $plugin
          ->getPublicPath())) {
          $items[$public_path . '/styles/%image_style'] = array(
            'title' => 'Generate image style',
            'page callback' => 'image_style_deliver',
            'page arguments' => array(
              count(explode('/', $public_path)) + 1,
            ),
            'access callback' => TRUE,
            'type' => MENU_CALLBACK,
          );
        }
      }
    }
  }
  return $items;
}

/**
 * Implements feeds_permission().
 */
function flysystem_permission() {
  return array(
    'administer flysystem' => array(
      'title' => t('Administer Flysystem'),
      'description' => t('Sync Flysystem filesystems.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Implements hook_cron().
 */
function flysystem_cron() {
  if (flysystem_dependencies_check()) {
    flysystem_factory()
      ->ensure();
  }
}

/**
 * Implements hook_file_download().
 */
function flysystem_file_download($uri) {
  if (!flysystem_dependencies_check()) {
    return;
  }
  $scheme = file_uri_scheme($uri);
  $schemes = variable_get('flysystem', array());
  if (!$scheme || !isset($schemes[$scheme])) {
    return;
  }
  $filesystem = flysystem_factory()
    ->getFilesystem($scheme);
  return array(
    'Content-Type' => file_get_mimetype($uri),
    'Content-Length' => $filesystem
      ->getSize(file_uri_target($uri)),
  );
}

/**
 * Implements hook_stream_wrappers().
 */
function flysystem_stream_wrappers() {
  $wrappers = array();

  // Code below stolen from https://www.drupal.org/project/amazons3.
  // This hook is called before hook_init(), so we have to manually register
  // the autoloader. We also need to handle module upgrades where
  // composer_manager might not be enabled yet.
  if (!module_exists('composer_manager')) {
    return $wrappers;
  }

  // If the module has been enabled, but the user didn't update composer
  // libraries, prevent failing entirely.
  try {
    composer_manager_register_autoloader();
  } catch (RuntimeException $e) {
    watchdog('flysystem', 'The Composer autoloader could not be registered. Run drush composer-rebuild and drush composer-manager update to update your vendor directory.');
    watchdog_exception('flysystem', $e);
    return $wrappers;
  }
  if (!flysystem_dependencies_check()) {
    watchdog('flysystem', 'The Flysystem dependencies are not installed correctly. Make sure all module updates have run. Otherwise, run drush composer-rebuild and drush composer-manager update to update your vendor directory.');
    return $wrappers;
  }
  foreach (variable_get('flysystem', array()) as $scheme => $settings) {
    $wrappers[$scheme] = array(
      'name' => t('Flysystem: @scheme', array(
        '@scheme' => $scheme,
      )),
      'class' => 'Drupal\\flysystem\\FlysystemBridge',
      'description' => t('Flysystem: @scheme', array(
        '@scheme' => $scheme,
      )),
      'type' => STREAM_WRAPPERS_WRITE_VISIBLE,
    );
  }
  return $wrappers;
}

/**
 * Implements hook_flysystem_plugins().
 */
function flysystem_flysystem_plugins() {
  $plugins = array(
    'missing' => array(
      'class' => 'Drupal\\flysystem\\Flysystem\\Missing',
    ),
    'local' => array(
      'class' => 'Drupal\\flysystem\\Flysystem\\Local',
    ),
  );
  if (extension_loaded('ftp')) {
    $plugins['ftp']['class'] = 'Drupal\\flysystem\\Flysystem\\Ftp';
  }
  return $plugins;
}

/**
 * Returns a plugin instance.
 *
 * @param string $id
 *   The plugin id.
 * @param array $configuration
 *   (optional) The plugin configuration array.
 *
 * @return \Drupal\flysystem\Plugin\FlysystemPluginInterface|false
 *   A flysystem plugin, or false on failure.
 */
function flysystem_get_plugin($id, array $configuration = array()) {
  if ($definition = flysystem_get_plugin_definition($id)) {
    $class = $definition['class'];
    if (class_exists($class)) {
      return $class::create($configuration);
    }
  }

  // We couldn't find the plugin.
  watchdog('flysystem', 'Unable to find the plugin: @id', array(
    '@id' => $id,
  ), WATCHDOG_ALERT);

  // Avoid infinite loop.
  if ($id === 'missing') {
    return FALSE;
  }
  return flysystem_get_plugin('missing');
}

/**
 * Returns a plugin definition.
 *
 * @param string $id
 *   The plugin id.
 *
 * @return array|false
 *   The plugin definition or false.
 */
function flysystem_get_plugin_definition($id) {
  $plugins = flysystem_get_plugin_definitions();
  return isset($plugins[$id]) ? $plugins[$id] : FALSE;
}

/**
 * Returns the plugin definitions.
 *
 * @return array
 *   The plugin definitions.
 */
function flysystem_get_plugin_definitions() {
  $plugins =& drupal_static(__FUNCTION__, NULL);
  if (!isset($plugins)) {
    $plugins = module_invoke_all('flysystem_plugins');
  }
  return $plugins;
}

/**
 * Returns the Flysystem factory.
 *
 * @return \Drupal\flysystem\FlysystemFactory
 *   Returns the Flysystem factory.
 */
function flysystem_factory() {
  $factory =& drupal_static(__FUNCTION__, NULL);
  if (!isset($factory)) {
    $factory = new FlysystemFactory(variable_get('flysystem', array()));
  }
  return $factory;
}

/**
 * Checks that dependencies are installed correctly.
 *
 * This should be needed during normal operation. Only during certain hooks that
 * might get called before dependencies are installed or loaded.
 *
 * @return bool
 *   Returns true on success, and false on failure.
 */
function flysystem_dependencies_check() {
  $checked =& drupal_static(__FUNCTION__, NULL);
  if (!isset($checked)) {
    $dependencies = array(
      'League\\Flysystem\\Filesystem',
      'League\\Flysystem\\Cached\\CachedAdapter',
      'League\\Flysystem\\Replicate\\ReplicateAdapter',
      'Twistor\\FlysystemStreamWrapper',
      'Drupal\\flysystem\\FlysystemFactory',
    );
    $checked = TRUE;
    foreach ($dependencies as $dependency) {
      $checked = $checked && class_exists($dependency);
    }
  }
  return $checked;
}

Functions

Namesort descending Description
flysystem_cron Implements hook_cron().
flysystem_dependencies_check Checks that dependencies are installed correctly.
flysystem_factory Returns the Flysystem factory.
flysystem_file_download Implements hook_file_download().
flysystem_flysystem_plugins Implements hook_flysystem_plugins().
flysystem_get_plugin Returns a plugin instance.
flysystem_get_plugin_definition Returns a plugin definition.
flysystem_get_plugin_definitions Returns the plugin definitions.
flysystem_menu Implements hook_menu().
flysystem_permission Implements feeds_permission().
flysystem_stream_wrappers Implements hook_stream_wrappers().