diff --git a/src/Controller/InstructionsController.php b/src/Controller/InstructionsController.php index 8b75480..e22ae5a 100644 --- a/src/Controller/InstructionsController.php +++ b/src/Controller/InstructionsController.php @@ -14,23 +14,24 @@ class InstructionsController extends ControllerBase { */ public function modal() { $html = " -
Upload your files to the server. If the files use the naming convention of {nid}_filename the created media will be - associated with the node identified by that nid. If not, the media will still be created, but will have to be associated - manually to a node through the UI. +
Upload your files to the server. If your files follow the naming convention {nid}_filename (where {nid} is the Node ID + of the target node), the created media will automatically associate with that node. e.g. 35_myfile.tif + will create a media associated with node 35. If the files do not use this naming convention, the media will still be created, + but will have to be associated manually to a node through the UI.
-If Already in place is selected, files in the destination folder will become Drupal managed files, - and those files will be used to build the media. This is the fastest and most efficient way to ingest very large files.
+If you choose Already in place, unmanaged files in the destination folder will be now be managed + by Drupal, and those files will be used to create media. This is the fastest way to ingest very large files.
The source directory holds files to be ingested. The source directory must be identified by the full - server path e.g. /var/www/upload_folder. + server path e.g. /var/www/upload_folder.
-The destination directory is within the Drupal file system. The path is relative, and the folder will be created if - it does not already exist.
-The file storage system is defined within the Media Types definitions. The uploaded files will be stored - in the default directory unless another file system is selected. +
The destination directory will exist within the Drupal file system. The path is relative, and the folder will be created within the file system + if it does not already exist. The user must have permissions to create folders and write files in the selected file system.
+The filesystem is defined within the Media Types definitions. The uploaded files will be stored + in that system unless another file system is selected.
File ownership is normally www-data:www-data on Apache systems and nginx:nginx for - a Docker based Islandora installation. The default value is normally fine, but check with your systems administrator - if you are unsure. + a Docker based Islandora installation. The default value is normally fine, but confirm with your systems administrator + if you are unsure. n.b. If your installation only uses one file system this choice is irrelevant.
"; return [ '#type' => 'markup', diff --git a/src/Form/CreateMediaFromFileForm.php b/src/Form/CreateMediaFromFileForm.php index 54932bf..e6c8013 100644 --- a/src/Form/CreateMediaFromFileForm.php +++ b/src/Form/CreateMediaFromFileForm.php @@ -41,6 +41,19 @@ final class CreateMediaFromFileForm extends FormBase { protected $streamWrapperManager; + /** + * Media field mapping. + * + * @var array + */ + protected $fileTypes = [ + 'audio' => 'field_media_audio_file', + 'document' => 'field_media_document', + 'file' => 'field_media_file', + 'image' => 'field_media_image', + 'video' => 'field_media_video_file', + ]; + /** * {@inheritdoc} */ @@ -74,8 +87,8 @@ final class CreateMediaFromFileForm extends FormBase { public function buildForm(array $form, FormStateInterface $form_state): array { $server_software = $_SERVER['SERVER_SOFTWARE'] ?? ''; $default_ownership = match (TRUE) { - stripos($server_software, 'apache') !== FALSE => 'www-data:www-data', - stripos($server_software, 'nginx') !== FALSE => 'nginx:nginx', + str_contains($server_software, 'apache') => 'www-data:www-data', + str_contains($server_software, 'nginx') => 'nginx:nginx', default => 'unknown:unknown', }; @@ -89,8 +102,7 @@ final class CreateMediaFromFileForm extends FormBase { $term_default = $term->tid; } } - $wrappers = $this->streamWrapperManager->getWrappers(); - $file_system_options = []; + $wrappers = $this->streamWrapperManager->getWrappers(); $file_system_options = []; $file_system_options['default'] = $this->t("File system defined in media type"); @@ -100,11 +112,9 @@ final class CreateMediaFromFileForm extends FormBase { if (in_array($scheme, $unwanted)) { continue; } + $class = $wrapper_info['class']; - if ( - is_a($class, StreamWrapperInterface::class, TRUE) && - class_exists($class) - ) { + if (is_a($class, StreamWrapperInterface::class, TRUE) && class_exists($class)) { $instance = new $class(); $instance->setUri($scheme . '://'); $file_system_options[$scheme] = $instance->getName() ?: $scheme; @@ -161,6 +171,9 @@ final class CreateMediaFromFileForm extends FormBase { '#required' => TRUE, '#default_value' => 'default', ]; + if (count($file_system_options) <= 1) { + $form['file_system']['#attributes']['style'] = 'display: none;'; + } $form['field_wrapper'] = [ '#type' => 'container', '#attributes' => ['class' => ['media-fields-wrapper']], @@ -212,17 +225,21 @@ final class CreateMediaFromFileForm extends FormBase { * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state): void { - $file_system = $form_state->getValue('file_system') . "://"; - $source_directory = $form_state->getValue('in_place') ? $file_system . $form_state->getValue('destination') : $form_state->getValue('source'); + $file_system = $this->getFilesystem($form_state); + $source_directory = rtrim($form_state->getValue('in_place') ? $file_system . $form_state->getValue('destination') : $form_state->getValue('source'), '/'); + if (!is_dir($source_directory)) { - $form_state->setErrorByName('source', $this->t('The specified directory does not exist.')); - } - if (is_dir($source_directory)) { - $files = scandir($source_directory); - $files = array_diff($files, ['.', '..']); - if (!$files) { - $form_state->setErrorByName('source', $this->t('The specified directory is empty.')); + if (str_starts_with($file_system, 'fedora')) { + $form_state->setErrorByName('source', $this->t('The fedora file system is not suitable for preloading files.')); + } + else { + $form_state->setErrorByName('source', $this->t('The specified directory does not exist.')); } + return; + } + + if (empty(array_diff(scandir($source_directory), ['.', '..']))) { + $form_state->setErrorByName('source', $this->t('The specified directory is empty.')); } } @@ -230,35 +247,15 @@ final class CreateMediaFromFileForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state): void { - $file_types = [ - 'audio' => 'field_media_audio_file', - 'document' => 'field_media_document', - 'file' => 'field_media_file', - 'image' => 'field_media_image', - 'video' => 'field_media_video_file', - ]; + $file_system = $this->getFilesystem($form_state); $media_type = $form_state->getValue('media_type'); - $file_type = $file_types[$media_type]; - - if ($form_state->getValue('file_system') == 'default') { - $field_config = FieldConfig::loadByName('media', $media_type, $file_type); - if ($field_config) { - $settings = $field_config->getSettings(); - $scheme = $settings['uri_scheme'] ?? 'public'; - $file_system = $scheme . "://"; - } - } - else { - $file_system = $form_state->getValue('file_system') . "://"; - } - $destination = trim($form_state->getValue('destination'), '/'); $build_data = [ 'source_dir' => $form_state->getValue('in_place') ? $file_system . $destination : $form_state->getValue('source'), 'destination_path' => "{$file_system}{$destination}", - 'media_type' => $media_type, - 'file_type' => $file_type, + 'media_type' => $form_state->getValue('media_type'), + 'file_type' => $this->fileTypes[$media_type], 'media_use' => $form_state->getValue('media_use'), 'ownership' => $form_state->getValue('ownership'), ]; @@ -278,4 +275,26 @@ final class CreateMediaFromFileForm extends FormBase { $form_state->setRedirect('