You are here

public function StageFileProxyCommands::dl in Stage File Proxy 8

Download all managed files from the origin.

@command stage_file_proxy:dl @aliases stage-file-proxy-dl,sfdl @option skip-progress-bar Skip displaying a progress bar.

File

src/Commands/StageFileProxyCommands.php, line 93

Class

StageFileProxyCommands
Drush commands for Stage File Proxy.

Namespace

Drupal\stage_file_proxy\Commands

Code

public function dl(array $command_options = [
  'skip-progress-bar' => FALSE,
]) {
  $logger = $this
    ->logger();
  $server = $this->moduleConfig
    ->get('origin');
  if (empty($server)) {
    throw new \Exception('Configure stage_file_proxy.settings.origin in your settings.php (see INSTALL.txt).');
  }
  $query = $this->database
    ->select('file_managed', 'fm');
  $results = $query
    ->fields('fm', [
    'uri',
  ])
    ->orderBy('fm.fid', 'DESC')
    ->execute()
    ->fetchCol();
  $fileDir = $this->fetchManager
    ->filePublicPath();
  $remoteFileDir = trim($this->moduleConfig
    ->get('origin_dir'));
  if (!$remoteFileDir) {
    $remoteFileDir = $fileDir;
  }
  $gotFilesNumber = 0;
  $errorFilesNumber = 0;
  $notPublicFilesNumber = 0;
  $results_number = count($results);
  $publicPrefix = 'public://';
  $logger
    ->notice('Downloading {count} files.', [
    'count' => $results_number,
  ]);
  $options = [
    'verify' => $this->moduleConfig
      ->get('verify'),
  ];
  $progress_bar = NULL;
  if (!$command_options['skip-progress-bar']) {
    $progress_bar = new ProgressBar($this
      ->output(), $results_number);
  }
  foreach ($results as $uri) {
    if (strpos($uri, $publicPrefix) !== 0) {
      $notPublicFilesNumber++;
      if ($progress_bar) {
        $progress_bar
          ->advance();
      }
      continue;
    }
    $relativePath = mb_substr($uri, mb_strlen($publicPrefix));
    if (file_exists("{$this->root}/{$fileDir}/{$relativePath}")) {
      if ($progress_bar) {
        $progress_bar
          ->advance();
      }
      continue;
    }
    try {
      if ($this->fetchManager
        ->fetch($server, $remoteFileDir, $relativePath, $options)) {
        $gotFilesNumber++;
      }
      else {
        $errorFilesNumber++;
        $logger
          ->error('Stage File Proxy encountered an unknown error by retrieving file {file}', [
          'file' => $server . '/' . UrlHelper::encodePath("{$remoteFileDir}/{$relativePath}"),
        ]);
      }
    } catch (ClientException $e) {
      $errorFilesNumber++;
      $logger
        ->error($e
        ->getMessage());
    }
    if ($progress_bar) {
      $progress_bar
        ->advance();
    }
  }
  if ($progress_bar) {
    $progress_bar
      ->finish();
  }
  $logger
    ->notice('{gotFilesNumber} downloaded files.', [
    'gotFilesNumber' => $gotFilesNumber,
  ]);
  if ($errorFilesNumber) {
    $logger
      ->error('{count} file(s) having an error, see log.', [
      'count' => $errorFilesNumber,
    ]);
  }
  if ($notPublicFilesNumber) {
    $logger
      ->error('{count} file(s) not in public directory.', [
      'count' => $notPublicFilesNumber,
    ]);
  }
}