You are here

function hackedProjectWebFilesDownloader::download in Hacked! 8.2

Download the remote files to the local filesystem.

Overrides hackedProjectWebDownloader::download

File

src/hackedProjectWebFilesDownloader.php, line 20

Class

hackedProjectWebFilesDownloader
Downloads a project using a standard Drupal method.

Namespace

Drupal\hacked

Code

function download() {
  $dir = $this
    ->get_destination();
  if (!($release_url = $this
    ->download_link())) {
    return FALSE;
  }

  // If our directory already exists, we can just return the path to this cached version
  if (file_exists($dir) && count(hacked_file_scan_directory($dir, '/.*/', [
    '.',
    '..',
    'CVS',
    '.svn',
    '.git',
  ]))) {
    return $dir;
  }

  // Build the destination folder tree if it doesn't already exists.
  if (!\Drupal::service('file_system')
    ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
    $message = $this
      ->t('Failed to create temp directory: %dir', [
      '%dir' => $dir,
    ]);
    \Drupal::logger('hacked')
      ->error($message
      ->render());
    return FALSE;
  }
  if (!($local_file = $this
    ->file_get($release_url))) {
    $message = $this
      ->t('Could not download the project: @name from URL: @url', [
      '@name' => $this->project
        ->title(),
      '@url' => $release_url,
    ]);
    \Drupal::logger('hacked')
      ->error($message
      ->render());
    return FALSE;
  }
  try {
    $this
      ->archive_extract($local_file, $dir);
  } catch (Exception $e) {
    $message = $this
      ->t('Could not extract the project: @name. Error was: !error', [
      '@name' => $this->project
        ->title(),
      '!error' => $e
        ->getMessage(),
    ]);
    \Drupal::logger('hacked')
      ->error($message
      ->render());
    return FALSE;
  }
  return TRUE;
}