3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\Core\File\FileSystemInterface;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\Core\StreamWrapper\LocalStream;
8 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
9 use Drupal\migrate\MigrateException;
10 use Drupal\migrate\MigrateExecutableInterface;
11 use Drupal\migrate\Plugin\MigrateProcessInterface;
12 use Drupal\migrate\ProcessPluginBase;
13 use Drupal\migrate\Row;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Copies or moves a local file from one place into another.
19 * The file can be moved, reused, or set to be automatically renamed if a
22 * The source value is an indexed array of two values:
23 * - The source path or URI, e.g. '/path/to/foo.txt' or 'public://bar.txt'.
24 * - The destination URI, e.g. 'public://foo.txt'.
26 * Available configuration keys:
27 * - move: (optional) Boolean, if TRUE, move the file, otherwise copy the file.
29 * - rename: (optional) Boolean, if TRUE, rename the file by appending a number
30 * until the name is unique. Defaults to FALSE.
31 * - reuse: (optional) Boolean, if TRUE, reuse the current file in its existing
32 * location rather than move/copy/rename the file. Defaults to FALSE.
42 * - public://new/path/to/file.png
45 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
47 * @MigrateProcessPlugin(
51 class FileCopy extends ProcessPluginBase implements ContainerFactoryPluginInterface {
54 * The stream wrapper manager service.
56 * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
58 protected $streamWrapperManager;
61 * The file system service.
63 * @var \Drupal\Core\File\FileSystemInterface
65 protected $fileSystem;
68 * An instance of the download process plugin.
70 * @var \Drupal\migrate\Plugin\MigrateProcessInterface
72 protected $downloadPlugin;
75 * Constructs a file_copy process plugin.
77 * @param array $configuration
78 * The plugin configuration.
79 * @param string $plugin_id
81 * @param mixed $plugin_definition
82 * The plugin definition.
83 * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrappers
84 * The stream wrapper manager service.
85 * @param \Drupal\Core\File\FileSystemInterface $file_system
86 * The file system service.
87 * @param \Drupal\migrate\Plugin\MigrateProcessInterface $download_plugin
88 * An instance of the download plugin for handling remote URIs.
90 public function __construct(array $configuration, $plugin_id, array $plugin_definition, StreamWrapperManagerInterface $stream_wrappers, FileSystemInterface $file_system, MigrateProcessInterface $download_plugin) {
96 parent::__construct($configuration, $plugin_id, $plugin_definition);
97 $this->streamWrapperManager = $stream_wrappers;
98 $this->fileSystem = $file_system;
99 $this->downloadPlugin = $download_plugin;
105 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
110 $container->get('stream_wrapper_manager'),
111 $container->get('file_system'),
112 $container->get('plugin.manager.migrate.process')->createInstance('download')
119 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
120 // If we're stubbing a file entity, return a URI of NULL so it will get
121 // stubbed by the general process.
122 if ($row->isStub()) {
125 list($source, $destination) = $value;
127 // If the source path or URI represents a remote resource, delegate to the
129 if (!$this->isLocalUri($source)) {
130 return $this->downloadPlugin->transform($value, $migrate_executable, $row, $destination_property);
133 // Ensure the source file exists, if it's a local URI or path.
134 if (!file_exists($source)) {
135 throw new MigrateException("File '$source' does not exist");
138 // If the start and end file is exactly the same, there is nothing to do.
139 if ($this->isLocationUnchanged($source, $destination)) {
143 // Check if a writable directory exists, and if not try to create it.
144 $dir = $this->getDirectory($destination);
145 // If the directory exists and is writable, avoid file_prepare_directory()
146 // call and write the file to destination.
147 if (!is_dir($dir) || !is_writable($dir)) {
148 if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
149 throw new MigrateException("Could not create or write to directory '$dir'");
153 $final_destination = $this->writeFile($source, $destination, $this->getOverwriteMode());
154 if ($final_destination) {
155 return $final_destination;
157 throw new MigrateException("File $source could not be copied to $destination");
161 * Tries to move or copy a file.
163 * @param string $source
164 * The source path or URI.
165 * @param string $destination
166 * The destination path or URI.
167 * @param int $replace
168 * (optional) FILE_EXISTS_REPLACE (default) or FILE_EXISTS_RENAME.
170 * @return string|bool
171 * File destination on success, FALSE on failure.
173 protected function writeFile($source, $destination, $replace = FILE_EXISTS_REPLACE) {
174 // Check if there is a destination available for copying. If there isn't,
175 // it already exists at the destination and the replace flag tells us to not
176 // replace it. In that case, return the original destination.
177 if (!($final_destination = file_destination($destination, $replace))) {
180 $function = 'file_unmanaged_' . ($this->configuration['move'] ? 'move' : 'copy');
181 return $function($source, $destination, $replace);
185 * Determines how to handle file conflicts.
188 * FILE_EXISTS_REPLACE (default), FILE_EXISTS_RENAME, or FILE_EXISTS_ERROR
189 * depending on the current configuration.
191 protected function getOverwriteMode() {
192 if (!empty($this->configuration['rename'])) {
193 return FILE_EXISTS_RENAME;
195 if (!empty($this->configuration['reuse'])) {
196 return FILE_EXISTS_ERROR;
199 return FILE_EXISTS_REPLACE;
203 * Returns the directory component of a URI or path.
205 * For URIs like public://foo.txt, the full physical path of public://
206 * will be returned, since a scheme by itself will trip up certain file
207 * API functions (such as file_prepare_directory()).
212 * @return string|false
213 * The directory component of the path or URI, or FALSE if it could not
216 protected function getDirectory($uri) {
217 $dir = $this->fileSystem->dirname($uri);
218 if (substr($dir, -3) == '://') {
219 return $this->fileSystem->realpath($dir);
225 * Determines if the source and destination URIs represent identical paths.
227 * @param string $source
229 * @param string $destination
230 * The destination URI.
233 * TRUE if the source and destination URIs refer to the same physical path,
236 protected function isLocationUnchanged($source, $destination) {
237 return $this->fileSystem->realpath($source) === $this->fileSystem->realpath($destination);
241 * Determines if the given URI or path is considered local.
243 * A URI or path is considered local if it either has no scheme component,
244 * or the scheme is implemented by a stream wrapper which extends
245 * \Drupal\Core\StreamWrapper\LocalStream.
248 * The URI or path to test.
252 protected function isLocalUri($uri) {
253 $scheme = $this->fileSystem->uriScheme($uri);
255 // The vfs scheme is vfsStream, which is used in testing. vfsStream is a
256 // simulated file system that exists only in memory, but should be treated
257 // as a local resource.
258 if ($scheme == 'vfs') {
261 return $scheme === FALSE || $this->streamWrapperManager->getViaScheme($scheme) instanceof LocalStream;