You are here

public function PhotosUpload::unzip in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 src/PhotosUpload.php \Drupal\photos\PhotosUpload::unzip()
  2. 8.4 src/PhotosUpload.php \Drupal\photos\PhotosUpload::unzip()

Unzip archive of image files.

Parameters

string $source: The zip file location.

array $params: Array of additional parameters like album id.

string $scheme: The file scheme.

Return value

int Uploaded files count.

Overrides PhotosUploadInterface::unzip

File

src/PhotosUpload.php, line 458

Class

PhotosUpload
Functions to help with uploading images to albums.

Namespace

Drupal\photos

Code

public function unzip($source, array $params, $scheme = 'default') {
  $fileConfig = $this->configFactory
    ->get('system.file');
  $file_count = 0;
  $photo_count = 0;
  if (isset($params['photo_count'])) {
    $photo_count = $params['photo_count'];
  }
  $album_photo_limit = NULL;
  if (isset($params['album_photo_limit'])) {
    $album_photo_limit = $params['album_photo_limit'];
  }
  if (version_compare(PHP_VERSION, '5') >= 0) {
    if (!is_file($source)) {
      $this->messenger
        ->addMessage($this
        ->t('Compressed file does not exist, please check the path: @src', [
        '@src' => $source,
      ]));
      return 0;
    }
    $fileType = [
      'jpg',
      'gif',
      'png',
      'jpeg',
      'JPG',
      'GIF',
      'PNG',
      'JPEG',
    ];
    $zip = new \ZipArchive();

    // Get relative path.
    $default_scheme = $fileConfig
      ->get('default_scheme');
    $relative_path = $this->fileSystem
      ->realpath($default_scheme . "://") . '/';
    $source = str_replace($default_scheme . '://', $relative_path, $source);

    // Open zip archive.
    if ($zip
      ->open($source) === TRUE) {
      for ($x = 0; $x < $zip->numFiles; ++$x) {
        $image = $zip
          ->statIndex($x);
        $filename_parts = explode('.', $image['name']);
        $ext = end($filename_parts);
        if (in_array($ext, $fileType)) {
          if ($album_photo_limit && $photo_count >= $album_photo_limit) {
            $this->messenger
              ->addWarning($this
              ->t('Maximum number of photos reached for this album.'));
            break;
          }
          $path = $this->fileSystem
            ->createFilename($image['name'], $this
            ->path($scheme));
          if ($temp_file = file_save_data($zip
            ->getFromIndex($x), $path)) {

            // Update file values.
            $temp_file->album_id = $params['album_id'];
            $temp_file->nid = $params['nid'];

            // Use image file name as title.
            $temp_file->title = $image['name'];
            $temp_file->des = $params['des'];

            // Prepare file entity.
            $file = $temp_file;
            try {

              // Save image.
              $file
                ->save();
              if ($this
                ->saveImage($file)) {
                $file_count++;
              }
            } catch (EntityStorageException $e) {
              watchdog_exception('photos', $e);
            }
          }
        }
      }
      $zip
        ->close();

      // Delete zip file.
      $this->fileSystem
        ->delete($source);
    }
    else {
      $this->messenger
        ->addWarning($this
        ->t('Compressed file does not exist, please try again: @src', [
        '@src' => $source,
      ]));
    }
  }
  return $file_count;
}