You are here

public static function ConfigForm::copyFile in Flysystem 8

Same name and namespace in other branches
  1. 3.x src/Form/ConfigForm.php \Drupal\flysystem\Form\ConfigForm::copyFile()
  2. 2.0.x src/Form/ConfigForm.php \Drupal\flysystem\Form\ConfigForm::copyFile()
  3. 3.0.x src/Form/ConfigForm.php \Drupal\flysystem\Form\ConfigForm::copyFile()

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.

4 calls to ConfigForm::copyFile()
ConfigFormTest::testCopyFile in tests/src/Unit/Form/ConfigFormTest.php
@covers ::copyFile
ConfigFormTest::testCopyFileException in tests/src/Unit/Form/ConfigFormTest.php
@covers ::copyFile
ConfigFormTest::testCopyFileFailedRead in tests/src/Unit/Form/ConfigFormTest.php
@covers ::copyFile
ConfigFormTest::testCopyFileFailedWrite in tests/src/Unit/Form/ConfigFormTest.php
@covers ::copyFile

File

src/Form/ConfigForm.php, line 137

Class

ConfigForm
Configure file system settings for this site.

Namespace

Drupal\flysystem\Form

Code

public static function copyFile($scheme_from, $scheme_to, $filepath, array &$context) {
  $context['message'] = \Drupal::translation()
    ->translate('Copying: %file', [
    '%file' => $filepath,
  ]);
  $context['finished'] = 1;
  $factory = \Drupal::service('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 = [
        '%scheme' => $scheme_from,
        '%file' => $filepath,
      ];
      $context['results']['errors'][] = [
        'The file %scheme://%file could not be opened.',
        $args,
      ];
      return;
    }
    $success = $factory
      ->getFilesystem($scheme_to)
      ->putStream($filepath, $read_handle);
    if (!$success) {
      $args = [
        '%scheme' => $scheme_to,
        '%file' => $filepath,
      ];
      $context['results']['errors'][] = [
        'The file %scheme://%file could not be saved.',
        $args,
      ];
    }
  } catch (\Exception $e) {
    $context['results']['errors'][] = [
      'An eror occured while copying %file.',
      [
        '%file' => $filepath,
      ],
    ];
    $context['results']['errors'][] = $e
      ->getMessage();
    watchdog_exception('flysystem', $e);
  }
  if (isset($read_handle) && is_resource($read_handle)) {
    fclose($read_handle);
  }
}