public static function PhotosUpload::moveImageFiles in Album Photos 8.4
Assist batch operation by moving or copying image files to album.
File
- src/
PhotosUpload.php, line 231
Class
- PhotosUpload
- Functions to help with uploading images to albums.
Namespace
Drupal\photosCode
public static function moveImageFiles($files, $account, $nid, $scheme, $allow_zip, $file_extensions, $copy, &$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 = PhotosUpload::path($scheme, '', $account);
$photos_name = PhotosUpload::rename($dir_file->filename);
$file_uri = \Drupal::service('file_system')
->getDestinationFilename($photos_path . '/' . $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.
$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,
'pid' => $nid,
'nid' => $nid,
'filename' => $photos_name,
'filesize' => $image
->getFileSize(),
'filemime' => $image
->getMimeType(),
]);
if (PhotosUpload::saveFile($file)) {
PhotosUpload::saveImage($file);
$count++;
}
}
}
}
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 = PhotosUpload::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) {
$value = new \stdClass();
$value->pid = $nid;
$value->nid = $nid;
$value->des = '';
$value->title = $dir_file->filename;
if (!($file_count = PhotosUpload::unzip($zip, $value, $scheme, $account))) {
// 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'];
}
}