Mercurial > hg > cmmr2012-drupal-site
comparison modules/contrib/migrate_plus/src/Plugin/migrate/process/FileBlob.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | a9cd425dd02b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
10 use Drupal\migrate\Row; | 10 use Drupal\migrate\Row; |
11 use Symfony\Component\DependencyInjection\ContainerInterface; | 11 use Symfony\Component\DependencyInjection\ContainerInterface; |
12 | 12 |
13 /** | 13 /** |
14 * Copy a file from a blob into a file. | 14 * Copy a file from a blob into a file. |
15 * | |
16 * The source value is an indexed array of two values: | |
17 * - The destination URI, e.g. 'public://example.txt'. | |
18 * - The binary blob data. | |
19 * | |
20 * Available configuration keys: | |
21 * - reuse: true | |
22 * | |
23 * @codingStandardsIgnoreStart | |
24 * | |
25 * Examples: | |
26 * @code | |
27 * uri: | |
28 * plugin: file_blob | |
29 * source: | |
30 * - 'public://example.txt' | |
31 * - blob | |
32 * @endcode | |
33 * Above, a basic configuration. | |
34 * | |
35 * @code | |
36 * source: | |
37 * constants: | |
38 * destination: public://images | |
39 * process: | |
40 * destination_blob: | |
41 * plugin: callback | |
42 * callable: base64_decode | |
43 * source: | |
44 * - blob | |
45 * destination_basename: | |
46 * plugin: callback | |
47 * callable: basename | |
48 * source: file_name | |
49 * destination_path: | |
50 * plugin: concat | |
51 * source: | |
52 * - constants/destination | |
53 * - @destination_basename | |
54 * uri: | |
55 * plugin: file_blob | |
56 * source: | |
57 * - @destination_path | |
58 * - @destination_blob | |
59 * @endcode | |
60 In the example above, it is necessary to manipulate the values before they | |
61 * are processed by this plugin. This is because this plugin takes a binary blob | |
62 * and saves it as a file. In many cases, as in this example, the data is base64 | |
63 * encoded and should be decoded first. In destination_blob, the incoming data | |
64 * is decoded from base64 to binary. The destination_path element is | |
65 * concatenating the base filename with the destination directory set in the | |
66 * constants to create the final path. The resulting values are then referenced | |
67 * as the source of the file_blob plugin. | |
68 * | |
69 * @codingStandardsIgnoreEnd | |
15 * | 70 * |
16 * @MigrateProcessPlugin( | 71 * @MigrateProcessPlugin( |
17 * id = "file_blob" | 72 * id = "file_blob" |
18 * ) | 73 * ) |
19 */ | 74 */ |
76 // more than absolutely necessary. | 131 // more than absolutely necessary. |
77 if ($this->putFile($destination, $blob, $replace)) { | 132 if ($this->putFile($destination, $blob, $replace)) { |
78 return $destination; | 133 return $destination; |
79 } | 134 } |
80 $dir = $this->getDirectory($destination); | 135 $dir = $this->getDirectory($destination); |
81 if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) { | 136 // TODO: remove after 8.6 is no longer supported in |
137 // https://www.drupal.org/project/migrate_plus/issues/3035587 | |
138 if (version_compare(\Drupal::VERSION, '8.7', '>=')) { | |
139 $success = $this->fileSystem->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS); | |
140 if (!$success) { | |
141 throw new MigrateSkipProcessException("Could not create directory '$dir'"); | |
142 } | |
143 } | |
144 elseif (file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { | |
82 throw new MigrateSkipProcessException("Could not create directory '$dir'"); | 145 throw new MigrateSkipProcessException("Could not create directory '$dir'"); |
83 } | 146 } |
84 if ($this->putFile($destination, $blob, $replace)) { | 147 if ($this->putFile($destination, $blob, $replace)) { |
85 return $destination; | 148 return $destination; |
86 } | 149 } |
93 * @param string $destination | 156 * @param string $destination |
94 * The destination path or URI. | 157 * The destination path or URI. |
95 * @param string $blob | 158 * @param string $blob |
96 * The base64 encoded file contents. | 159 * The base64 encoded file contents. |
97 * @param int $replace | 160 * @param int $replace |
98 * (optional) FILE_EXISTS_REPLACE (default) or FILE_EXISTS_ERROR, depending | 161 * (optional) either FileSystemInterface::EXISTS_REPLACE; (default) or |
99 * on the configuration. | 162 * FileSystemInterface::EXISTS_ERROR, depending on the configuration. |
100 * | 163 * |
101 * @return bool|string | 164 * @return bool|string |
102 * File path on success, FALSE on failure. | 165 * File path on success, FALSE on failure. |
103 */ | 166 */ |
104 protected function putFile($destination, $blob, $replace = FILE_EXISTS_REPLACE) { | 167 protected function putFile($destination, $blob, $replace = FileSystemInterface::EXISTS_REPLACE) { |
105 if ($path = file_destination($destination, $replace)) { | 168 // TODO: remove after 8.6 is no longer supported in |
169 // https://www.drupal.org/project/migrate_plus/issues/3035587 | |
170 if (!isset($replace)) { | |
171 $replace = FILE_EXISTS_REPLACE; | |
172 } | |
173 if (version_compare(\Drupal::VERSION, '8.7', '>=')) { | |
174 $path = $this->fileSystem->getDestinationFilename($destination, $replace); | |
175 } | |
176 else { | |
177 $path = file_destination($destination, $replace); | |
178 } | |
179 | |
180 if ($path) { | |
106 if (file_put_contents($path, $blob)) { | 181 if (file_put_contents($path, $blob)) { |
107 return $path; | 182 return $path; |
108 } | 183 } |
109 else { | 184 else { |
110 return FALSE; | 185 return FALSE; |
117 | 192 |
118 /** | 193 /** |
119 * Determines how to handle file conflicts. | 194 * Determines how to handle file conflicts. |
120 * | 195 * |
121 * @return int | 196 * @return int |
122 * Either FILE_EXISTS_REPLACE (default) or FILE_EXISTS_ERROR, depending on | 197 * Either FileSystemInterface::EXISTS_REPLACE; (default) or |
123 * the configuration. | 198 * FileSystemInterface::EXISTS_ERROR, depending on the configuration. |
124 */ | 199 */ |
125 protected function getOverwriteMode() { | 200 protected function getOverwriteMode() { |
126 if (!empty($this->configuration['reuse'])) { | 201 // TODO: remove after 8.6 is no longer supported in |
127 return FILE_EXISTS_ERROR; | 202 // https://www.drupal.org/project/migrate_plus/issues/3035587 |
128 } | 203 if (isset($this->configuration['reuse']) && !empty($this->configuration['reuse'])) { |
129 | 204 if (version_compare(\Drupal::VERSION, '8.7', '>=')) { |
130 return FILE_EXISTS_REPLACE; | 205 return FileSystemInterface::EXISTS_ERROR; |
206 } | |
207 else { | |
208 return FILE_EXISTS_ERROR; | |
209 } | |
210 | |
211 } | |
212 | |
213 if (version_compare(\Drupal::VERSION, '8.7', '>=')) { | |
214 return FileSystemInterface::EXISTS_REPLACE; | |
215 } | |
216 else { | |
217 return FILE_EXISTS_REPLACE; | |
218 } | |
131 } | 219 } |
132 | 220 |
133 /** | 221 /** |
134 * Returns the directory component of a URI or path. | 222 * Returns the directory component of a URI or path. |
135 * | 223 * |