diff --git a/drush.services.yml b/drush.services.yml new file mode 100644 index 0000000..eb34834 --- /dev/null +++ b/drush.services.yml @@ -0,0 +1,7 @@ +services: + islandora_inplace_media.commands: + class: Drupal\islandora_inplace_media\Commands\IslandoraInplaceMediaCommands + arguments: + - '@islandora_inplace_media.processor' + tags: + - { name: drush.command } diff --git a/islandora_inplace_media.info.yml b/islandora_inplace_media.info.yml index a93da23..5947ab0 100644 --- a/islandora_inplace_media.info.yml +++ b/islandora_inplace_media.info.yml @@ -1,7 +1,7 @@ name: 'Islandora Inplace Media' type: module -description: 'Allows Uploaded files to be attched to exisitng nodes as media' -package: Custom +description: 'Allows Uploaded files to be attached to existing nodes as media' +package: Islandora core_version_requirement: ^10 || ^11 dependencies: - islandora:islandora diff --git a/islandora_inplace_media.services.yml b/islandora_inplace_media.services.yml index e819314..1fd59cc 100644 --- a/islandora_inplace_media.services.yml +++ b/islandora_inplace_media.services.yml @@ -1,4 +1,13 @@ services: - islandora_inplace_media.utils: - class: Drupal\islandora_inplace_media\Utils - arguments: ['@entity_type.manager', '@file_system', '@logger.factory', '@entity_field.manager'] + + logger.channel.islandora_inplace_media: + class: Drupal\Core\Logger\LoggerChannel + factory: logger.factory:get + arguments: ['islandora_inplace_media'] + + islandora_inplace_media.processor: + class: Drupal\islandora_inplace_media\Service\InplaceMediaProcessor + arguments: + - '@file_system' + - '@file.repository' + - '@logger.channel.islandora_inplace_media' diff --git a/src/Commands/IslandoraInplaceMediaCommands.php b/src/Commands/IslandoraInplaceMediaCommands.php new file mode 100644 index 0000000..688fdc8 --- /dev/null +++ b/src/Commands/IslandoraInplaceMediaCommands.php @@ -0,0 +1,77 @@ + NULL, + 'destination_path' => NULL, + 'media_type' => NULL, + 'media_use' => NULL, + 'file_type' => 'field_media_file', + 'limit' => NULL, + 'offset' => 0, + 'queue' => FALSE, + ]) { + $files = array_values(array_diff(scandir($options['source_dir']), ['.', '..'])); + + if ($options['offset']) { + $files = array_slice($files, (int) $options['offset']); + } + if ($options['limit']) { + $files = array_slice($files, 0, (int) $options['limit']); + } + + $progress = new ProgressBar($this->output(), count($files)); + $progress->start(); + + foreach ($files as $file) { + if ($options['queue']) { + \Drupal::queue('islandora_inplace_media') + ->createItem(['file' => $file, 'options' => $options]); + } + else { + $this->processor->processFile($file, $options); + } + $progress->advance(); + } + + $progress->finish(); + $this->output()->writeln(''); + } + +} diff --git a/src/Plugin/QueueWorker/InplaceMediaQueueWorker.php b/src/Plugin/QueueWorker/InplaceMediaQueueWorker.php new file mode 100644 index 0000000..1375a02 --- /dev/null +++ b/src/Plugin/QueueWorker/InplaceMediaQueueWorker.php @@ -0,0 +1,21 @@ +processFile($data['file'], $data['options']); + } + +} diff --git a/src/Service/InplaceMediaProcessor.php b/src/Service/InplaceMediaProcessor.php new file mode 100644 index 0000000..cd5ffd6 --- /dev/null +++ b/src/Service/InplaceMediaProcessor.php @@ -0,0 +1,49 @@ +logger->warning('Missing file @file', ['@file' => $source]); + return; + } + + $uri = ($source === $dest) + ? $dest + : $this->fileSystem->copy($source, $dest, FileExists::Rename); + + $fileEntity = $this->fileRepository->loadByUri($uri) + ?? File::create(['uri' => $uri, 'status' => 1, 'uid' => 1]); + $fileEntity->save(); + preg_match('/^(\d+)_/', $file, $m); + $nid = $m[1] ?? NULL; + + Media::create([ + 'bundle' => $opts['media_type'], + 'name' => $file, + 'uid' => 1, + $opts['file_type'] => ['target_id' => $fileEntity->id()], + 'field_media_use' => ['target_id' => $opts['media_use']], + 'field_media_of' => $nid, + ])->save(); + } + +}