You are here

public static function PhotosDirectoryImportForm::moveImageFiles in Album Photos 8.5

Same name and namespace in other branches
  1. 6.0.x src/Form/PhotosDirectoryImportForm.php \Drupal\photos\Form\PhotosDirectoryImportForm::moveImageFiles()

Assist batch operation by moving or copying image files to album.

Parameters

array $files: The files to be moved or copied.

\Drupal\user\Entity\User $account: The selected user account.

int $nid: The album node id.

string $scheme: The file system scheme.

bool $copy: If TRUE copy files, if FALSE move files.

array $context: The batch context array.

File

src/Form/PhotosDirectoryImportForm.php, line 271

Class

PhotosDirectoryImportForm
Defines a form to upload photos to this site.

Namespace

Drupal\photos\Form

Code

public static function moveImageFiles(array $files, User $account, $nid, $scheme, $copy, array &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_id'] = 0;
    $context['sandbox']['max'] = count($files);
    $context['results']['images_processed'] = 0;
    $context['results']['nid'] = $nid;
    $context['results']['uid'] = $account
      ->id();
    $context['results']['copy'] = $copy;
  }
  $limit = 20;
  $process_files = array_slice($files, $context['sandbox']['current_id'], $limit);
  $count = 0;
  foreach ($process_files as $dir_file) {
    $ext = mb_substr($dir_file->uri, -3);
    if ($ext != 'zip' && $ext != 'ZIP') {

      // Prepare directory.
      $photos_path = \Drupal::service('photos.upload')
        ->path($scheme);
      $photos_name = $dir_file->filename;
      $file_uri = \Drupal::service('file_system')
        ->getDestinationFilename($photos_path . 'photos/' . $photos_name, FileSystemInterface::EXISTS_RENAME);

      // Display current file name.
      $context['message'] = t('Processing:') . ' ' . Html::escape($photos_name);
      if ($copy) {
        $file_processed = \Drupal::service('file_system')
          ->copy($dir_file->uri, $file_uri);
      }
      else {
        $file_processed = \Drupal::service('file_system')
          ->move($dir_file->uri, $file_uri);
      }
      if ($file_processed) {

        // Save file to album. Include title and description.

        /* @var \Drupal\Core\Image\Image $image */
        $image = \Drupal::service('image.factory')
          ->get($file_uri);
        if ($image
          ->getWidth()) {

          // Create a file entity.
          $file = File::create([
            'uri' => $file_uri,
            'uid' => $account
              ->id(),
            'status' => FILE_STATUS_PERMANENT,
            'album_id' => $nid,
            'nid' => $nid,
            'filename' => $photos_name,
            'filesize' => $image
              ->getFileSize(),
            'filemime' => $image
              ->getMimeType(),
          ]);
          try {
            $file
              ->save();
            \Drupal::service('photos.upload')
              ->saveImage($file);
            $count++;
          } catch (EntityStorageException $e) {
            watchdog_exception('photos', $e);
          }
        }
      }
    }
    else {

      // Process zip file.
      if (!\Drupal::config('photos.settings')
        ->get('photos_upzip')) {
        \Drupal::messenger()
          ->addError(t('Please update settings to allow zip uploads.'));
      }
      else {
        $directory = \Drupal::service('photos.upload')
          ->path();
        \Drupal::service('file_system')
          ->prepareDirectory($directory);

        // Display current file name.
        $context['message'] = t('Processing:') . ' ' . Html::escape($dir_file->uri);
        $zip = \Drupal::service('file_system')
          ->getDestinationFilename($directory . '/' . trim(basename($dir_file->uri)), FileSystemInterface::EXISTS_RENAME);

        // @todo large zip files could fail here.
        if ($copy) {
          $file_processed = \Drupal::service('file_system')
            ->copy($dir_file->uri, $zip);
        }
        else {
          $file_processed = \Drupal::service('file_system')
            ->move($dir_file->uri, $zip);
        }
        if ($file_processed) {
          $params = [];
          $params['album_id'] = $nid;
          $params['nid'] = $nid;
          $params['des'] = '';
          $params['title'] = $dir_file->filename;
          if (!($file_count = \Drupal::service('photos.upload')
            ->unzip($zip, $params, $scheme))) {

            // Upload failed.
          }
          else {
            $count = $count + $file_count;
          }
        }
      }
    }

    // Update progress.
    $context['sandbox']['progress']++;
    $context['sandbox']['current_id']++;
  }
  $context['results']['images_processed'] += $count;

  // Check if complete.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}