You are here

public function S3fsFileMigrationBatch::execute in S3 File System 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Batch/S3fsFileMigrationBatch.php \Drupal\s3fs\Batch\S3fsFileMigrationBatch::execute()

Copies all the local files from the specified file system into S3.

Parameters

array $config: An s3fs configuration array.

string $scheme: Allowed values: 'public' | 'private' Scheme to copy.

array $uploadOptions: Options to control upload operations.

Overrides S3fsFileMigrationBatchInterface::execute

File

src/Batch/S3fsFileMigrationBatch.php, line 29

Class

S3fsFileMigrationBatch
Batch migrate files to a S3 bucket.

Namespace

Drupal\s3fs\Batch

Code

public function execute(array $config, $scheme, array $uploadOptions) {
  if ($scheme === 'public') {
    $source_folder = realpath(PublicStream::basePath());
    $target_folder = !empty($config['public_folder']) ? $config['public_folder'] . '/' : 's3fs-public/';
  }
  elseif ($scheme === 'private') {
    $source_folder = PrivateStream::basePath() ? PrivateStream::basePath() : '';
    $source_folder_real = realpath($source_folder);
    if (empty($source_folder) || empty($source_folder_real)) {
      $this
        ->messenger()
        ->addError($this
        ->t('Private file system base path is unknown. Unable to perform S3 copy.'));
      return;
    }
    $target_folder = !empty($config['private_folder']) ? $config['private_folder'] . '/' : 's3fs-private/';
  }
  else {
    $this
      ->messenger()
      ->addError($this
      ->t('Scheme @scheme is not allowed', [
      '%scheme' => $scheme,
    ]));
    return;
  }
  if (!empty($config['root_folder'])) {
    $target_folder = $config['root_folder'] . '/' . $target_folder;
  }
  $file_paths = $this
    ->dirScan($source_folder);
  if (!empty($file_paths)) {

    // Create batch.
    $batch = $this
      ->getBatch();
    $total = count($file_paths);
    $file_paths_chunks = array_chunk($file_paths, 50, TRUE);
    unset($file_paths);
    foreach ($file_paths_chunks as $chunk) {
      $batch['operations'][] = [
        [
          get_class($this),
          'copyOperation',
        ],
        [
          $config,
          $chunk,
          $total,
          $source_folder,
          $target_folder,
          $scheme,
          $uploadOptions,
        ],
      ];
    }
    batch_set($batch);
    $batch =& batch_get();
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t("There weren't files to migrate."), 'ok');
  }
}