You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
3.3 KiB
122 lines
3.3 KiB
<?php |
|
|
|
declare(strict_types=1); |
|
|
|
namespace Drupal\islandora_inplace_media; |
|
|
|
use Drupal\Core\File\FileSystemInterface; |
|
use Drupal\file\Entity\File; |
|
use Drupal\media\Entity\Media; |
|
use Drupal\Core\File\FileExists; |
|
|
|
/** |
|
* Utility class for managing media file operations in Islandora. |
|
*/ |
|
class Utils { |
|
|
|
/** |
|
* Processes individual file. |
|
*/ |
|
public static function processMediaBatch($file_name, $build_data, &$context) { |
|
if (!isset($context['results'])) { |
|
$context['results'] = []; |
|
} |
|
$media_type = $build_data['media_type']; |
|
$file_type = $build_data['file_type'] ?? 'field_media_file'; |
|
$logger = \Drupal::logger('islandora_inplace_media'); |
|
$fileSystem = \Drupal::service('file_system'); |
|
$source_path = $build_data['source_dir'] . '/' . $file_name; |
|
$destination_path = $build_data['destination_path'] . '/' . $file_name; |
|
|
|
if (!\file_exists($source_path)) { |
|
$logger->warning('File does not exist: @file', ['@file' => $source_path]); |
|
return; |
|
} |
|
if ($source_path === $destination_path) { |
|
$path = $destination_path; |
|
} |
|
else { |
|
$path = $fileSystem->copy($source_path, $destination_path, FileExists::Rename); |
|
} |
|
$file = \Drupal::service('file.repository')->loadByUri($path); |
|
if (!$file) { |
|
$absolute_path = $fileSystem->realpath($destination_path); |
|
if ($absolute_path) { |
|
chown($absolute_path, $build_data['ownership']); |
|
} |
|
|
|
$file = File::create([ |
|
'uri' => $path, |
|
'status' => 1, |
|
]); |
|
$file->save(); |
|
} |
|
|
|
$context['results'][$file->id()] = $path; |
|
|
|
if (preg_match('/^(\d+)_/', $file_name, $matches)) { |
|
$nid = $matches[1] ?? NULL; |
|
} |
|
|
|
$media = Media::create([ |
|
"bundle" => $media_type, |
|
"name" => $file_name, |
|
$file_type => [ |
|
"target_id" => $file->id(), |
|
], |
|
'field_media_use' => [ |
|
"target_id" => $build_data['media_use'], |
|
], |
|
"field_media_of" => $nid, |
|
]); |
|
$media->save(); |
|
|
|
} |
|
|
|
/** |
|
* Builds batch from input directory. |
|
*/ |
|
public static function buildMediaBatch($build_data) { |
|
$source_dir = $build_data['source_dir']; |
|
if ($source_dir && !is_dir($source_dir)) { |
|
\Drupal::logger('islandora_inplace_media')->error('Source directory does not exist: @dir', ['@dir' => $source_dir]); |
|
return; |
|
} |
|
$destination = $build_data['destination_path']; |
|
$fileSystem = \Drupal::service('file_system'); |
|
$fileSystem->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY); |
|
chown($destination, $build_data['ownership']); |
|
|
|
$files = scandir($source_dir); |
|
$files = array_diff($files, ['.', '..']); |
|
|
|
$batch = [ |
|
'title' => t('Processing media files...'), |
|
'operations' => [], |
|
// Static method reference. |
|
'finished' => [self::class, 'finishMediaBatch'], |
|
]; |
|
|
|
foreach ($files as $file_name) { |
|
$batch['operations'][] = [ |
|
[self::class, 'processMediaBatch'], |
|
[$file_name, $build_data], |
|
]; |
|
} |
|
|
|
batch_set($batch); |
|
} |
|
|
|
/** |
|
* Display results. |
|
*/ |
|
public static function finishMediaBatch($success, $results, $operations) { |
|
if ($success) { |
|
\Drupal::logger('islandora_inplace_media')->notice('Successfully processed media files.'); |
|
} |
|
else { |
|
\Drupal::logger('islandora_inplace_media')->error('There were errors processing media files.'); |
|
} |
|
} |
|
|
|
}
|
|
|