Browse Source

UI updates

main
astanley 7 months ago
parent
commit
eca3066930
  1. 25
      src/Controller/InstructionsController.php
  2. 95
      src/Form/CreateMediaFromFileForm.php
  3. 4
      src/Utils.php

25
src/Controller/InstructionsController.php

@ -14,23 +14,24 @@ class InstructionsController extends ControllerBase {
*/ */
public function modal() { public function modal() {
$html = " $html = "
<p>Upload your files to the server. If the files use the naming convention of {nid}_filename the created media will be <p>Upload your files to the server. If your files follow the naming convention <strong>{nid}_filename</strong> (where {nid} is the Node ID
associated with the node identified by that nid. If not, the media will still be created, but will have to be associated of the target node), the created media will automatically associate with that node. <em>e.g.</em> <strong>35_myfile.tif</strong>
manually to a node through the UI. 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.
</p> </p>
<p> If <strong><em>Already in place</em></strong> is selected, files in the destination folder will become Drupal managed files, <p> If you choose <strong><em>Already in place</em></strong>, unmanaged files in the destination folder will be now be managed
and those files will be used to build the media. This is the fastest and most efficient way to ingest very large files. </p> by Drupal, and those files will be used to create media. This is the fastest way to ingest very large files. </p>
<p>The source directory holds files to be ingested. The source directory must be identified by the full <p>The source directory holds files to be ingested. The source directory must be identified by the full
server path e.g. <strong><em>/var/www/upload_folder</em></strong>. server path <em>e.g.</em> <strong><em>/var/www/upload_folder</em></strong>.
</p> </p>
<p>The destination directory is within the Drupal file system. The path is relative, and the folder will be created if <p>The destination directory will exist within the Drupal file system. The path is relative, and the folder will be created within the file system
it does not already exist.</p> if it does not already exist. The user must have permissions to create folders and write files in the selected file system.</p>
<p> The file storage system is defined within the Media Types definitions. The uploaded files will be stored <p> The filesystem is defined within the Media Types definitions. The uploaded files will be stored
in the default directory unless another file system is selected. in that system unless another file system is selected.
<p> <p>
<p>File ownership is normally <strong>www-data:www-data</strong> on Apache systems and <strong>nginx:nginx</strong> for <p>File ownership is normally <strong>www-data:www-data</strong> on Apache systems and <strong>nginx:nginx</strong> for
a Docker based Islandora installation. The default value is normally fine, but check with your systems administrator a Docker based Islandora installation. The default value is normally fine, but confirm with your systems administrator
if you are unsure. if you are unsure. <em>n.b</em>. If your installation only uses one file system this choice is irrelevant.
</p>"; </p>";
return [ return [
'#type' => 'markup', '#type' => 'markup',

95
src/Form/CreateMediaFromFileForm.php

@ -41,6 +41,19 @@ final class CreateMediaFromFileForm extends FormBase {
protected $streamWrapperManager; 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} * {@inheritdoc}
*/ */
@ -74,8 +87,8 @@ final class CreateMediaFromFileForm extends FormBase {
public function buildForm(array $form, FormStateInterface $form_state): array { public function buildForm(array $form, FormStateInterface $form_state): array {
$server_software = $_SERVER['SERVER_SOFTWARE'] ?? ''; $server_software = $_SERVER['SERVER_SOFTWARE'] ?? '';
$default_ownership = match (TRUE) { $default_ownership = match (TRUE) {
stripos($server_software, 'apache') !== FALSE => 'www-data:www-data', str_contains($server_software, 'apache') => 'www-data:www-data',
stripos($server_software, 'nginx') !== FALSE => 'nginx:nginx', str_contains($server_software, 'nginx') => 'nginx:nginx',
default => 'unknown:unknown', default => 'unknown:unknown',
}; };
@ -89,8 +102,7 @@ final class CreateMediaFromFileForm extends FormBase {
$term_default = $term->tid; $term_default = $term->tid;
} }
} }
$wrappers = $this->streamWrapperManager->getWrappers();
$file_system_options = [];
$wrappers = $this->streamWrapperManager->getWrappers(); $wrappers = $this->streamWrapperManager->getWrappers();
$file_system_options = []; $file_system_options = [];
$file_system_options['default'] = $this->t("File system defined in media type"); $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)) { if (in_array($scheme, $unwanted)) {
continue; continue;
} }
$class = $wrapper_info['class']; $class = $wrapper_info['class'];
if ( if (is_a($class, StreamWrapperInterface::class, TRUE) && class_exists($class)) {
is_a($class, StreamWrapperInterface::class, TRUE) &&
class_exists($class)
) {
$instance = new $class(); $instance = new $class();
$instance->setUri($scheme . '://'); $instance->setUri($scheme . '://');
$file_system_options[$scheme] = $instance->getName() ?: $scheme; $file_system_options[$scheme] = $instance->getName() ?: $scheme;
@ -161,6 +171,9 @@ final class CreateMediaFromFileForm extends FormBase {
'#required' => TRUE, '#required' => TRUE,
'#default_value' => 'default', '#default_value' => 'default',
]; ];
if (count($file_system_options) <= 1) {
$form['file_system']['#attributes']['style'] = 'display: none;';
}
$form['field_wrapper'] = [ $form['field_wrapper'] = [
'#type' => 'container', '#type' => 'container',
'#attributes' => ['class' => ['media-fields-wrapper']], '#attributes' => ['class' => ['media-fields-wrapper']],
@ -212,17 +225,21 @@ final class CreateMediaFromFileForm extends FormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validateForm(array &$form, FormStateInterface $form_state): void { public function validateForm(array &$form, FormStateInterface $form_state): void {
$file_system = $form_state->getValue('file_system') . "://"; $file_system = $this->getFilesystem($form_state);
$source_directory = $form_state->getValue('in_place') ? $file_system . $form_state->getValue('destination') : $form_state->getValue('source'); $source_directory = rtrim($form_state->getValue('in_place') ? $file_system . $form_state->getValue('destination') : $form_state->getValue('source'), '/');
if (!is_dir($source_directory)) { if (!is_dir($source_directory)) {
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.')); $form_state->setErrorByName('source', $this->t('The specified directory does not exist.'));
} }
if (is_dir($source_directory)) { return;
$files = scandir($source_directory);
$files = array_diff($files, ['.', '..']);
if (!$files) {
$form_state->setErrorByName('source', $this->t('The specified directory is empty.'));
} }
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} * {@inheritdoc}
*/ */
public function submitForm(array &$form, FormStateInterface $form_state): void { public function submitForm(array &$form, FormStateInterface $form_state): void {
$file_types = [ $file_system = $this->getFilesystem($form_state);
'audio' => 'field_media_audio_file',
'document' => 'field_media_document',
'file' => 'field_media_file',
'image' => 'field_media_image',
'video' => 'field_media_video_file',
];
$media_type = $form_state->getValue('media_type'); $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'), '/'); $destination = trim($form_state->getValue('destination'), '/');
$build_data = [ $build_data = [
'source_dir' => $form_state->getValue('in_place') ? $file_system . $destination : $form_state->getValue('source'), 'source_dir' => $form_state->getValue('in_place') ? $file_system . $destination : $form_state->getValue('source'),
'destination_path' => "{$file_system}{$destination}", 'destination_path' => "{$file_system}{$destination}",
'media_type' => $media_type, 'media_type' => $form_state->getValue('media_type'),
'file_type' => $file_type, 'file_type' => $this->fileTypes[$media_type],
'media_use' => $form_state->getValue('media_use'), 'media_use' => $form_state->getValue('media_use'),
'ownership' => $form_state->getValue('ownership'), 'ownership' => $form_state->getValue('ownership'),
]; ];
@ -278,4 +275,26 @@ final class CreateMediaFromFileForm extends FormBase {
$form_state->setRedirect('<front>'); $form_state->setRedirect('<front>');
} }
/**
* Gets default filesystem for media type.
*/
protected function getFilesystem($form_state) {
$media_type = $form_state->getValue('media_type');
$file_type = $this->fileTypes[$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') . "://";
}
return $file_system;
}
} }

4
src/Utils.php

@ -7,6 +7,7 @@ namespace Drupal\islandora_inplace_media;
use Drupal\Core\File\FileSystemInterface; use Drupal\Core\File\FileSystemInterface;
use Drupal\file\Entity\File; use Drupal\file\Entity\File;
use Drupal\media\Entity\Media; use Drupal\media\Entity\Media;
use Drupal\Core\File\FileExists;
/** /**
* Utility class for managing media file operations in Islandora. * Utility class for managing media file operations in Islandora.
@ -35,7 +36,7 @@ class Utils {
$path = $destination_path; $path = $destination_path;
} }
else { else {
$path = $fileSystem->copy($source_path, $destination_path, FileSystemInterface::EXISTS_RENAME); $path = $fileSystem->copy($source_path, $destination_path, FileExists::Rename);
} }
$file = \Drupal::service('file.repository')->loadByUri($path); $file = \Drupal::service('file.repository')->loadByUri($path);
if (!$file) { if (!$file) {
@ -84,6 +85,7 @@ class Utils {
$destination = $build_data['destination_path']; $destination = $build_data['destination_path'];
$fileSystem = \Drupal::service('file_system'); $fileSystem = \Drupal::service('file_system');
$fileSystem->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY); $fileSystem->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY);
chown($destination, $build_data['ownership']);
$files = scandir($source_dir); $files = scandir($source_dir);
$files = array_diff($files, ['.', '..']); $files = array_diff($files, ['.', '..']);

Loading…
Cancel
Save