You are here

function photos_access_move_files in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 photos_access/photos_access.module \photos_access_move_files()
  2. 8.4 photos_access/photos_access.module \photos_access_move_files()

Move files from public to private and private to public as needed.

Parameters

\Drupal\node\NodeInterface $node: The node.

bool $public: Whether to use the default filesystem scheme or not.

1 call to photos_access_move_files()
photos_access_move_files_form_submit in photos_access/photos_access.module
Form submission handler for private file management.

File

photos_access/photos_access.module, line 301
Implementation of photos_access.module.

Code

function photos_access_move_files(NodeInterface $node, $public = TRUE) {
  $nid = $node
    ->id();
  $db = \Drupal::database();

  // Get user account.
  $albumUid = $db
    ->query("SELECT uid FROM {node_field_data} WHERE nid = :nid", [
    ':nid' => $nid,
  ])
    ->fetchField();
  $account = NULL;
  try {
    $account = \Drupal::entityTypeManager()
      ->getStorage('user')
      ->load($albumUid);
  } catch (InvalidPluginDefinitionException $e) {
    watchdog_exception('photos_access', $e);
  } catch (PluginNotFoundException $e) {
    watchdog_exception('photos_access', $e);
  }

  // Query all files in album.
  $results = $db
    ->query("SELECT id FROM {photos_image_field_data} WHERE album_id = :nid", [
    ':nid' => $nid,
  ]);

  // Check file wrapper.
  $default_scheme = \Drupal::config('system.file')
    ->get('default_scheme');
  $private_scheme = 'private';
  $file_wrapper = $private_scheme . '://';
  $new_scheme = $private_scheme;
  if ($public) {

    // Move files to default filesystem.
    $file_wrapper = $default_scheme . '://';
    $new_scheme = $default_scheme;
  }
  $cache_tags = [];

  // @todo add a warning that changing privacy settings will move files.
  foreach ($results as $result) {
    $photosImage = NULL;
    try {

      /** @var \Drupal\photos\Entity\PhotosImage $photosImage */
      $photosImage = \Drupal::entityTypeManager()
        ->getStorage('photos_image')
        ->load($result->id);
    } catch (InvalidPluginDefinitionException $e) {
      watchdog_exception('photos_access', $e);
    } catch (PluginNotFoundException $e) {
      watchdog_exception('photos_access', $e);
    }
    if ($photosImage && ($fids = $photosImage
      ->getFids())) {
      foreach ($fids as $fid) {
        $file = NULL;
        try {

          /** @var \Drupal\file\FileInterface $file */
          $file = \Drupal::entityTypeManager()
            ->getStorage('file')
            ->load($fid);
        } catch (InvalidPluginDefinitionException $e) {
          watchdog_exception('photos_access', $e);
        } catch (PluginNotFoundException $e) {
          watchdog_exception('photos_access', $e);
        }
        if (!$file) {

          // File not found.
          continue;
        }
        $uri = $file
          ->GetFileUri();
        if (strstr($uri, $file_wrapper)) {

          // File is already in correct place.
          // @note continue (check all) or break (assume the rest are also the
          // same)? Mixed private and public files could be in the same album if
          // private file path is setup after uploading some images.
          continue;
        }

        // Move file.
        $old_file_wrapper = \Drupal::service('stream_wrapper_manager')
          ->getScheme($uri) . '://';
        if ($old_file_wrapper != $file_wrapper) {
          $file_system = \Drupal::service('file_system');

          // All we do here is change the file system scheme if needed.
          $new_uri = str_replace($old_file_wrapper, $file_wrapper, $file
            ->getFileUri());
          $dirname = $file_system
            ->dirname($new_uri);
          $file_system
            ->prepareDirectory($dirname, FileSystemInterface::CREATE_DIRECTORY);
          file_move($file, $new_uri);

          // Clear image page cache.
          $cache_tags[] = 'photos:image:' . $fid;
        }
      }
    }
  }

  // Clear album page cache.
  $cache_tags[] = 'photos:album:' . $nid;

  // Invalidate image page and album page cache as needed.
  Cache::invalidateTags($cache_tags);
}