You are here

function amazons3_stream_wrappers in AmazonS3 7.2

Same name and namespace in other branches
  1. 7 amazons3.module \amazons3_stream_wrappers()

Implements hook_stream_wrappers().

Create a stream wrapper for S3.

File

./amazons3.module, line 19
Hook implementations for the AmazonS3 module.

Code

function amazons3_stream_wrappers() {

  // 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 array();
  }

  // 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('amazons3', 'The Composer autoloader could not be registered. Run drush composer-rebuild and drush composer-manager update to update your vendor directory.');
    watchdog_exception('amazons3', $e);
    return array();
  }
  if (!class_exists('Drupal\\amazons3\\StreamWrapper')) {
    watchdog('amazons3', 'The AmazonS3 StreamWrapper class is missing. Make sure all module updates have run. Otherwise, run drush composer-rebuild and drush composer-manager update to update your vendor directory.');
    return array();
  }

  // If the module isn't configured yet, don't register the stream wrapper.
  try {
    StreamWrapperConfiguration::fromDrupalVariables();
  } catch (\InvalidArgumentException $e) {
    if (current_path() == 'admin/config/media/file-system') {
      drupal_set_message(t('The AmazonS3 module <a href="@s3-configure">needs to be configured</a> before setting it as a download method.', array(
        '@s3-configure' => url('admin/config/media/amazons3', array(
          'query' => array(
            'destination' => current_path(),
          ),
        )),
      )));
    }
    return array();
  }

  // getimagesize() calls require the stream to be seekable.
  stream_context_set_default([
    's3' => [
      'seekable' => TRUE,
    ],
  ]);
  return array(
    's3' => array(
      'name' => 'Amazon S3',
      'class' => 'Drupal\\amazons3\\StreamWrapper',
      'description' => t('Amazon Simple Storage Service'),
    ),
  );
}