From d8faf15e5e1710e5aa06851b84c2b6bf27775643 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 17 Sep 2024 17:43:02 -0300 Subject: [PATCH] Reworked. --- .../migrations/dgis_orig_file.yml | 22 ++----- .../migrations/dgis_tn_file.yml | 23 ++----- src/Plugin/migrate/process/FoxmlFile.php | 62 ++++++++++--------- 3 files changed, 41 insertions(+), 66 deletions(-) diff --git a/modules/dgi_migrate_foxml_standard_mods/migrations/dgis_orig_file.yml b/modules/dgi_migrate_foxml_standard_mods/migrations/dgis_orig_file.yml index f410b34..15f66b3 100644 --- a/modules/dgi_migrate_foxml_standard_mods/migrations/dgis_orig_file.yml +++ b/modules/dgi_migrate_foxml_standard_mods/migrations/dgis_orig_file.yml @@ -70,11 +70,6 @@ process: - plugin: dgi_migrate.method source: '@_latest' method: getUri - _path: - - plugin: format_date - source: '@created' - from_format: U - to_format: 'Y-m' filemime: - plugin: dgi_migrate.subproperty property: MIMETYPE @@ -93,19 +88,12 @@ process: - '@_safe_pid' - '@_ext' delimiter: '.' - _dest_uri: - - plugin: concat - source: - - constants/file_dest - - '@_path' - - '@filename' - delimiter: '/' uri: - - plugin: dgi_migrate.naive_file_copy - file_exists: rename - source: - - '@_source_uri' - - '@_dest_uri' + - plugin: dgi_migrate.foxml_file + source: '@_source_uri' + destination: constants/file_dest + date: '@created' + filename: '@filename' filesize: - plugin: callback source: '@uri' diff --git a/modules/dgi_migrate_foxml_standard_mods/migrations/dgis_tn_file.yml b/modules/dgi_migrate_foxml_standard_mods/migrations/dgis_tn_file.yml index 25a5286..b2a5ef6 100644 --- a/modules/dgi_migrate_foxml_standard_mods/migrations/dgis_tn_file.yml +++ b/modules/dgi_migrate_foxml_standard_mods/migrations/dgis_tn_file.yml @@ -66,11 +66,6 @@ process: - plugin: dgi_migrate.method source: '@_latest' method: getUri -# _path: -# - plugin: format_date -# source: '@created' -# from_format: U -# to_format: 'Y-m' filemime: - plugin: dgi_migrate.subproperty property: MIMETYPE @@ -89,22 +84,12 @@ process: - '@_safe_pid' - '@_ext' delimiter: '.' -# _dest_uri: -# - plugin: concat -# source: -# - constants/file_dest -# - '@_path' -# - '@filename' -# delimiter: '/' -# uri: -# - plugin: dgi_migrate.naive_file_copy -# file_exists: rename -# source: -# - '@_source_uri' -# - '@_dest_uri' uri: - plugin: dgi_migrate.foxml_file - pid: + source: '@_source_uri' + destination: constants/file_dest + date: '@created' + filename: '@filename' filesize: - plugin: callback source: '@uri' diff --git a/src/Plugin/migrate/process/FoxmlFile.php b/src/Plugin/migrate/process/FoxmlFile.php index 2908612..fc0119d 100644 --- a/src/Plugin/migrate/process/FoxmlFile.php +++ b/src/Plugin/migrate/process/FoxmlFile.php @@ -2,9 +2,8 @@ namespace Drupal\dgi_migrate\Plugin\migrate\process; -use Drupal\Component\Transliteration\TransliterationInterface; -use Drupal\Core\Language\LanguageInterface; use Drupal\migrate\MigrateExecutableInterface; +use Drupal\migrate\MigrateSkipRowException; use Drupal\migrate\Plugin\MigrateProcessInterface; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\ProcessPluginBase; @@ -18,14 +17,11 @@ * - method: One of "copy" (to copy the file) or "direct" (to directly use the * file). Defaults to "copy". Can be set with the DGI_MIGRATE_FILE_METHOD * environment variable. - * - pid: Property in the row containing the PID, to build out a destination if - * copying. * - destination: Property in the row containing the destination, to build out * a destination path if copying. * - date: Property in the row containing a date with which to build a path in * the destination, when copying. - * - mimetype; Property in the row containing the MIME type of the file, to - * determine an extension. + * - filename: Property in the row containing a filename. * * @MigrateProcessPlugin( * id = "dgi_migrate.foxml_file" @@ -41,9 +37,7 @@ public function __construct( $plugin_id, $plugin_definition, protected MigrationInterface $migration, - protected TransliterationInterface $transliteration, protected MigrateProcessInterface $naiveCopyPlugin, - protected MigrateProcessInterface $extensionPlugin, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); } @@ -61,9 +55,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $migration, - $container->get('transliteration'), - $process_plugin_manager->createInstance('dgi_migrate.naive_file_copy'), - $process_plugin_manager->createInstance('dgi_migrate.process.extension_from_mimetype'), + $process_plugin_manager->createInstance('dgi_migrate.naive_file_copy', [], $migration), ); } @@ -74,35 +66,45 @@ public function transform($value, MigrateExecutableInterface $migrate_executable return match ($this->configuration['method']) { 'copy' => $this->naiveCopyPlugin->transform([ $value, - $this->getDestinationPath($migrate_executable, $row, $destination_property), + $this->getDestinationPath($row), ], $migrate_executable, $row, $destination_property), - 'direct' => $value, + 'direct' => $this->ensureNonWritable($value), }; } /** * Build out path. */ - protected function getDestinationPath(MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) : string { - $pid = $row->get($this->configuration['pid']); - // Adapted from https://api.drupal.org/api/drupal/core%21modules%21migrate%21src%21Plugin%21migrate%21process%21MachineName.php/class/MachineName/10 - $safe_pid = preg_replace( - '/_+/', - '_', - preg_replace( - '/[^a-z0-9_]+/', - '_', - strtolower( - $this->transliteration->transliterate($pid, LanguageInterface::LANGCODE_DEFAULT, '_'), - ), - ) - ); - + protected function getDestinationPath(Row $row) : string { $dest_dir = $row->get($this->configuration['destination']); $path = date('Y-m', $row->get($this->configuration['date'])); - $ext = $this->extensionPlugin->transform($row->get($this->configuration['mimetype']), $migrate_executable, $row, $destination_property); + $filename = $row->get($this->configuration['filename']); + + return "{$dest_dir}/{$path}/{$filename}"; + } + + /** + * Helper; ensure the given path does not appear to be writable. + * + * @param string $path + * The path to check. + * + * @return string + * The path unchanged if non-writable; otherwise, we throw the skip + * exception. + * + * @throws \Drupal\migrate\MigrateSkipRowException + * If the file appears to be writable/deletable. + */ + protected function ensureNonWritable(string $path) : string { + $file = new \SplFileInfo($path); + + if ($file->isFile() && !$file->isDir() && $file->isReadable() && !$file->isWritable() && + !$file->getPathInfo()->isWritable()) { + throw new MigrateSkipRowException('Source appears writable (or not readable); skipping row.'); + } - return "{$dest_dir}/{$path}/{$safe_pid}.{$ext}"; + return $path; } }