|
|
|
|
@ -4,14 +4,21 @@ namespace Drupal\islandora_inplace_media\Commands;
|
|
|
|
|
|
|
|
|
|
use Drush\Commands\DrushCommands; |
|
|
|
|
use Drupal\islandora_inplace_media\Service\InplaceMediaProcessor; |
|
|
|
|
use Drupal\Core\State\StateInterface; |
|
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
|
|
|
|
|
|
|
|
|
class IslandoraInplaceMediaCommands extends DrushCommands { |
|
|
|
|
|
|
|
|
|
protected InplaceMediaProcessor $processor; |
|
|
|
|
protected StateInterface $state; |
|
|
|
|
|
|
|
|
|
public function __construct( |
|
|
|
|
protected InplaceMediaProcessor $processor |
|
|
|
|
InplaceMediaProcessor $processor, |
|
|
|
|
StateInterface $state, |
|
|
|
|
) { |
|
|
|
|
parent::__construct(); |
|
|
|
|
$this->processor = $processor; |
|
|
|
|
$this->state = $state; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -36,7 +43,10 @@ class IslandoraInplaceMediaCommands extends DrushCommands {
|
|
|
|
|
* Number of files to skip before processing. |
|
|
|
|
* @option queue |
|
|
|
|
* Queue files instead of processing immediately. |
|
|
|
|
* @option shards |
|
|
|
|
* Total number of queue shards. |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
public function run(array $options = [ |
|
|
|
|
'source_dir' => NULL, |
|
|
|
|
'destination_path' => NULL, |
|
|
|
|
@ -46,26 +56,48 @@ class IslandoraInplaceMediaCommands extends DrushCommands {
|
|
|
|
|
'limit' => NULL, |
|
|
|
|
'offset' => 0, |
|
|
|
|
'queue' => FALSE, |
|
|
|
|
'shards' => 1, |
|
|
|
|
]) { |
|
|
|
|
$files = array_values(array_diff(scandir($options['source_dir']), ['.', '..'])); |
|
|
|
|
$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']); |
|
|
|
|
$job_id = hash('sha256', serialize([ |
|
|
|
|
$options['source_dir'], |
|
|
|
|
$options['destination_path'], |
|
|
|
|
$options['media_type'], |
|
|
|
|
$options['media_use'], |
|
|
|
|
$options['file_type'], |
|
|
|
|
])); |
|
|
|
|
|
|
|
|
|
$state_key = "islandora_inplace_media.completed.$job_id"; |
|
|
|
|
$completed = $this->state->get($state_key, []); |
|
|
|
|
|
|
|
|
|
$files = array_values(array_diff($files, $completed)); |
|
|
|
|
|
|
|
|
|
if ($options['queue']) { |
|
|
|
|
foreach ($files as $file) { |
|
|
|
|
$shard = crc32($file) % (int) $options['shards']; |
|
|
|
|
\Drupal::queue('islandora_inplace_media') |
|
|
|
|
->createItem([ |
|
|
|
|
'file' => $file, |
|
|
|
|
'options' => $options, |
|
|
|
|
'job_id' => $job_id, |
|
|
|
|
'shard' => $shard, |
|
|
|
|
]); |
|
|
|
|
} |
|
|
|
|
$this->output()->writeln('Files queued with sharding.'); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$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); |
|
|
|
|
if ($this->processor->processFile($file, $options)) { |
|
|
|
|
$completed[] = $file; |
|
|
|
|
$this->state->set($state_key, $completed); |
|
|
|
|
} |
|
|
|
|
$progress->advance(); |
|
|
|
|
} |
|
|
|
|
|