Skip to content

Commit

Permalink
Reworked.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey committed Sep 17, 2024
1 parent d6d2ee0 commit d8faf15
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down
62 changes: 32 additions & 30 deletions src/Plugin/migrate/process/FoxmlFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"
Expand All @@ -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);
}
Expand All @@ -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),
);
}

Expand All @@ -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;
}

}

0 comments on commit d8faf15

Please sign in to comment.