diff --git a/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/destination/EntityFile.php b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/destination/EntityFile.php index 6ae4f30..3fbf1ae 100644 --- a/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/destination/EntityFile.php +++ b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/destination/EntityFile.php @@ -12,6 +12,7 @@ use Drupal\migrate\Entity\MigrationInterface; use Drupal\migrate\Plugin\MigratePluginManager; use Drupal\migrate\Row; +use Drupal\migrate\MigrateException; /** * @MigrateDestination( @@ -29,6 +30,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition 'source_path_property' => 'filepath', 'destination_path_property' => 'uri', 'move' => FALSE, + 'urlencode' => FALSE, ); parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $plugin_manager, $field_info); } @@ -47,14 +49,53 @@ public function import(Row $row, array $old_destination_id_values = array()) { } } $dirname = drupal_dirname($destination); - file_prepare_directory($dirname, FILE_CREATE_DIRECTORY); + if (!file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) { + throw new MigrateException(t('Could not create directory %dirname', + array('%dirname' => $dirname))); + } if ($this->configuration['move']) { - file_unmanaged_move($source, $destination, $replace); + $copied = file_unmanaged_move($source, $destination, $replace); + } + else { + // Determine whether we can perform this operation based on overwrite rules. + $original_destination = $destination; + $destination = file_destination($destination, $replace); + if ($destination === FALSE) { + throw new MigrateException(t('File %file could not be copied because a file by that name already exists in the destination directory (%destination)', array('%file' => $source, '%destination' => $original_destination))); + } + $source = $this->urlencode($source); + $copied = copy($source, $destination); + } + if ($copied) { + return parent::import($row, $old_destination_id_values); } else { - file_unmanaged_copy($source, $destination, $replace); + throw new MigrateException(t('File %source could not be copied to %destination.', + array('%source' => $source, '%destination' => $destination))); + } + } + + /** + * Urlencode all the components of a remote filename. + * + * @param $filename + * + * @return string + */ + protected function urlencode($filename) { + // Only apply to a full URL + if ($this->configuration['urlencode'] && strpos($filename, '://')) { + $components = explode('/', $filename); + foreach ($components as $key => $component) { + $components[$key] = rawurlencode($component); + } + $filename = implode('/', $components); + // Actually, we don't want certain characters encoded + $filename = str_replace('%3A', ':', $filename); + $filename = str_replace('%3F', '?', $filename); + $filename = str_replace('%26', '&', $filename); } - return parent::import($row, $old_destination_id_values); + return $filename; } }