You are here

function _flysystem_config_form_copy_file in Flysystem 7

Copies a single file.

Parameters

string $scheme_from: The scheme to sync from.

string $scheme_to: The scheme to sync to.

string $filepath: The file to sync.

array &$context: The batch context.

1 string reference to '_flysystem_config_form_copy_file'
flysystem_config_form_submit in ./flysystem.admin.inc
Submit callback for flysystem_config_form().

File

./flysystem.admin.inc, line 108
Configuration page callbacks for Flysystem.

Code

function _flysystem_config_form_copy_file($scheme_from, $scheme_to, $filepath, array &$context) {
  $context['message'] = t('Copying: %file', array(
    '%file' => $filepath,
  ));
  $context['finished'] = 1;
  $factory = flysystem_factory();

  // Copying files could take a very long time. Using streams will keep memory
  // usage down, but we could still timeout.
  drupal_set_time_limit(0);
  try {
    $read_handle = $factory
      ->getFilesystem($scheme_from)
      ->readStream($filepath);
    if (!is_resource($read_handle)) {
      $args = array(
        '%scheme' => $scheme_from,
        '%file' => $filepath,
      );
      $context['results']['errors'][] = array(
        'The file %scheme://%file could not be opened.',
        $args,
      );
      return;
    }
    $success = $factory
      ->getFilesystem($scheme_to)
      ->putStream($filepath, $read_handle);
    if (!$success) {
      $args = array(
        '%scheme' => $scheme_to,
        '%file' => $filepath,
      );
      $context['results']['errors'][] = array(
        'The file %scheme://%file could not be saved.',
        $args,
      );
    }
  } catch (Exception $e) {
    $context['results']['errors'][] = array(
      'An eror occured while copying %file.',
      array(
        '%file' => $filepath,
      ),
    );
    $context['results']['errors'][] = $e
      ->getMessage();
    watchdog_exception('flysystem', $e);
  }
  if (isset($read_handle) && is_resource($read_handle)) {
    fclose($read_handle);
  }
}