diff --git a/multiversion.module b/multiversion.module index 9bb38e9..4a0f314 100644 --- a/multiversion.module +++ b/multiversion.module @@ -369,14 +369,13 @@ function multiversion_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $f * When the directory exists and it is writable returns TRUE. * * @param string $destination - * @param \Drupal\Core\StreamWrapper\StreamWrapperInterface $stream_wrapper * * @return bool */ -function multiversion_prepare_file_destination($destination, StreamWrapperInterface $stream_wrapper) { +function multiversion_prepare_file_destination($destination) { $dirname = \Drupal::service('file_system')->dirname($destination); $logger = \Drupal::logger('multiversion'); - if (!is_dir($dirname) && !$stream_wrapper->mkdir($dirname, NULL, TRUE)) { + if (!is_dir($dirname) && !\Drupal::service('file_system')->mkdir($dirname, NULL, TRUE)) { // If the directory does not exists and cannot be created. $logger->error('The directory %directory does not exist and could not be created.', array('%directory' => $dirname)); } diff --git a/src/Entity/Storage/ContentEntityStorageTrait.php b/src/Entity/Storage/ContentEntityStorageTrait.php index 8f7d55a..08c4645 100644 --- a/src/Entity/Storage/ContentEntityStorageTrait.php +++ b/src/Entity/Storage/ContentEntityStorageTrait.php @@ -132,10 +132,7 @@ trait ContentEntityStorageTrait { // Prepare the file directory. if ($entity instanceof FileInterface) { - $uri = $entity->getFileUri(); - $scheme = \Drupal::service('file_system')->uriScheme($uri) ?: 'public'; - $stream_wrapper_name = 'stream_wrapper.' . $scheme; - multiversion_prepare_file_destination($uri, \Drupal::service($stream_wrapper_name)); + multiversion_prepare_file_destination($entity->getFileUri()); } try { diff --git a/src/MultiversionMigration.php b/src/MultiversionMigration.php index 5dea711..7836905 100644 --- a/src/MultiversionMigration.php +++ b/src/MultiversionMigration.php @@ -103,18 +103,17 @@ class MultiversionMigration implements MultiversionMigrationInterface { * {@inheritdoc} */ public function copyFilesToMigrateDirectory(FileStorageInterface $storage) { - $scheme = 'migrate://'; - $entities = $storage->loadMultiple(); - if ($entities) { - foreach ($entities as $entity) { - $uri = $entity->getFileUri(); - $destination = $scheme; - if ($target = file_uri_target($uri)) { - $destination = $destination . $target; - } - if (multiversion_prepare_file_destination($destination, \Drupal::service('multiversion.stream_wrapper.migrate'))) { + foreach ($storage->loadMultiple() as $entity) { + $uri = $entity->getFileUri(); + + $target = file_uri_target($uri); + + if ($target !== FALSE) { + $destination = 'migrate://' . $target; + + if (multiversion_prepare_file_destination($destination)) { // Copy the file to a folder from 'migrate://' directory. - file_unmanaged_copy($entity->getFileUri(), $destination, FILE_EXISTS_REPLACE); + file_unmanaged_copy($uri, $destination, FILE_EXISTS_REPLACE); } } } diff --git a/src/Plugin/migrate/destination/ContentEntityBase.php b/src/Plugin/migrate/destination/ContentEntityBase.php index 3963078..3ae1764 100644 --- a/src/Plugin/migrate/destination/ContentEntityBase.php +++ b/src/Plugin/migrate/destination/ContentEntityBase.php @@ -90,16 +90,20 @@ class ContentEntityBase extends EntityContentBase { } if ($entity->getEntityTypeId() == 'file') { $destinations = $row->getDestination(); + if (isset($destinations['uri'])) { - $destination = 'public://'; - if ($target = file_uri_target($destinations['uri'])) { - $destination = $destination . $target; - } - if (multiversion_prepare_file_destination($destination, \Drupal::service('stream_wrapper.public'))) { - // Move the file to a folder from 'public://' directory. - file_unmanaged_move($destinations['uri'], $destination, FILE_EXISTS_REPLACE); + $target = file_uri_target($destinations['uri']); + + if ($target !== FALSE) { + $destination = 'public://' . $target; + if (multiversion_prepare_file_destination($destination)) { + // Move the file to a folder from 'public://' directory. + file_unmanaged_move($destinations['uri'], $destination, FILE_EXISTS_REPLACE); + } + + // @todo Should this sitll set the destination if the move fails? + $entity->uri->setValue($destination); } - $entity->uri->setValue($destination); } } return $this->save($entity, $old_destination_id_values);