function photos_access_move_files in Album Photos 8.4
Same name and namespace in other branches
- 8.5 photos_access/photos_access.module \photos_access_move_files()
- 6.0.x photos_access/photos_access.module \photos_access_move_files()
Move files from public to private and private to public as needed.
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 228 - Implementation of photos_access.module.
Code
function photos_access_move_files($node, $public = TRUE) {
$nid = $node
->id();
$db = \Drupal::database();
// Get user account.
$album_uid = $db
->query("SELECT uid FROM {node_field_data} WHERE nid = :nid", [
':nid' => $nid,
])
->fetchField();
$account = \Drupal::entityTypeManager()
->getStorage('user')
->load($album_uid);
// Query all files in album.
$results = $db
->query("SELECT fid FROM {photos_image} WHERE pid = :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 = [];
$clear_private_styles = [];
foreach ($results as $result) {
$fid = $result->fid;
/** @var \Drupal\file\FileInterface $file */
$file = File::load($fid);
$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;
}
// Prepare file directory.
PhotosUpload::path($new_scheme, $file, $account);
// Move file.
$old_file_wrapper = \Drupal::service('stream_wrapper_manager')
->getScheme($uri) . '://';
if ($old_file_wrapper != $file_wrapper) {
$new_uri = str_replace($old_file_wrapper, $file_wrapper, $uri);
file_move($file, $new_uri);
if ($new_scheme == 'public') {
$pathinfo = pathinfo($uri);
$ext = strtolower($pathinfo['extension']);
$clear_private_styles[] = 'image_' . $fid . '.' . $ext;
}
// Clear image page cache.
$cache_tags[] = 'photos:image:' . $fid;
}
}
// Check private image style settings.
$private_styles_setting = \Drupal::config('photos.settings')
->get('photos_private_file_styles');
if (!empty($clear_private_styles) && $private_styles_setting == 'automatic') {
// Delete private image styles.
$private_image_styles_path = $private_scheme . '://photos/tmp_images';
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
foreach ($clear_private_styles as $basename) {
// Find all derivatives for this image.
$file_uris = $file_system
->scanDirectory($private_image_styles_path, '~\\b' . $basename . '\\b~');
foreach ($file_uris as $uri => $data) {
// Delete.
\Drupal::service('file_system')
->delete($uri);
}
}
}
// Clear album page cache.
$cache_tags[] = 'photos:album:' . $nid;
// Invalidate image page and album page cache as needed.
Cache::invalidateTags($cache_tags);
}