Skip to content

Commit

Permalink
WIP on #39.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed Apr 27, 2021
1 parent d634ea3 commit ac5f239
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 80 deletions.
30 changes: 8 additions & 22 deletions src/Plugin/Form/IslandoraRiprapSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#title' => $this->t('Number of events to show in each Media\'s "Details" report. Leave empty to show all.'),
'#default_value' => $config->get('number_of_events'),
];
$form['gemini_rest_endpoint'] = [
'#type' => 'textfield',
'#title' => $this->t('Gemini microservice REST endpoint'),
'#description' => $this->t('Do not include the trailing /.'),
'#default_value' => $config->get('gemini_rest_endpoint'),
];
$form['use_drupal_urls'] = [
'#type' => 'checkbox',
'#title' => $this->t('Use Drupal URLs for media instead of Fedora URLs. Check this box only if you are not using Fedora.'),
Expand Down Expand Up @@ -215,7 +209,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set('riprap_local_settings_file', rtrim($form_state->getValue('riprap_local_settings_file'), '/'))
->set('execute_riprap_in_cron', $form_state->getValue('execute_riprap_in_cron'))
->set('number_of_events', $form_state->getValue('number_of_events'))
->set('gemini_rest_endpoint', rtrim($form_state->getValue('gemini_rest_endpoint'), '/'))
->set('use_drupal_urls', $form_state->getValue('use_drupal_urls'))
->set('log_riprap_warnings', $form_state->getValue('log_riprap_warnings'))
->set('use_sample_failed_fixity_events', $form_state->getValue('use_sample_failed_fixity_events'))
Expand Down Expand Up @@ -248,22 +241,14 @@ public function generateRiprapConfig(array $values) {
# Plugins #
###########
plugins.fetchresourcelist: ['PluginFetchResourceListFromDrupal']
# Use this plugin if you want to use the 'Riprap resource (media) list' View provided by Islandora Riprap.
plugins.fetchresourcelist: ['PluginFetchResourceListFromDrupalView']
drupal_baseurl: '$base_url'
jsonapi_authorization_headers: ['Authorization: Basic YWRtaW46aXNsYW5kb3Jh']
drupal_media_auth: ['{$values['user_name']}', '{$values['user_pass']}']
drupal_content_types: ['{$values['fixity_content_type']}']
drupal_media_tags: ['/taxonomy/term/{$values['fixity_terms']}']
use_fedora_urls: true
gemini_endpoint: '{$values['gemini_rest_endpoint']}'
gemini_auth_header: 'Bearer islandora'
# Can be a maximum of 50.
jsonapi_page_size: 50
# The number of resources to check in one Riprap run; if absent, will use
# value defined in jsonapi_page_size. Must be a multiple of number specified
# in jsonapi_page_size.
max_resources: 1000
jsonapi_pager_data_file_path: '/var/www/html/riprap/var/fetchresourcelist.from.drupal.pager.txt'
drupal_user: {$values['user_name']}
drupal_password: {$values['user_pass']}
fedora_baseurl: 'http://127.0.0.1:8080/fcrepo/rest'
# Absolute or relative to the Riprap application directory.
views_pager_data_file_path: 'var/fetchresourcelist.from.drupal.pager.txt'
plugins.fetchdigest: PluginFetchDigestFromFedoraAPI
fedoraapi_method: HEAD
Expand All @@ -274,6 +259,7 @@ public function generateRiprapConfig(array $values) {
plugins.postcheck: ['PluginPostCheckCopyFailures']
# Absolute or relative to the Riprap application directory.
failures_log_path: '/tmp/riprap_failed_events.log'
EOF;
return $riprap_config;
}
Expand Down
9 changes: 8 additions & 1 deletion src/Plugin/views/field/RiprapResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ public function render(ResultRow $value) {

$media = $value->_entity;
$mid = $media->id();
$binary_resource_uuid = $utils->getFileUuid($mid);

if ($this->use_drupal_urls) {
$binary_resource_url = $utils->getLocalUrl($mid);
}
else {
$binary_resource_url = $utils->getFedoraUrl($mid);
if (!$binary_resource_url) {
return [
'#theme' => 'islandora_riprap_summary',
'#content' => 'Not in Fedora',
'#outcome' => NULL,
'#mid' => NULL,
];
}
}

$num_events = $config->get('number_of_events') ?: 10;
Expand Down
82 changes: 25 additions & 57 deletions src/Riprap/IslandoraRiprapUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,72 +63,33 @@ public function getFileUuid($mid) {
/**
* Get a Fedora URL for a File entity from Gemini.
*
* @param string $uuid
* The File entity's UUID.
* @param string $mid
* The Meida entity's ID.
*
* @return string
* The Fedora URL corresponding to the UUID, or a message.
* The Fedora URL to the file corresponding to the Media's ID, or False.
*/
public function getFedoraUrl($mid) {
$media = Media::load($mid);
$media_source_service = \Drupal::service('islandora.media_source_service');
$source_file = $media_source_service->getSourceFile($media);
$uri = $source_file->getFileUri();
$scheme = \Drupal::service('stream_wrapper_manager')->getScheme($uri);
$flysystem_config = Settings::get('flysystem');

$mapper = \Drupal::service('islandora.entity_mapper');

$flysystem_config = Settings::get('flysystem');
if (isset($flysystem_config[$scheme]) && $flysystem_config[$scheme]['driver'] == 'fedora') {
$fedora_root = $flysystem_config['fedora']['config']['root'];
$fedora_root = rtrim($fedora_root, '/');
$parts = parse_url($uri);
$path = $parts['host'] . $parts['path'];
}
else {
$path = $mapper->getFedoraPath($source_file->uuid());
}
$path = trim($path, '/');
$fedora_uri = "$fedora_root/$path";
return($fedora_uri);
}

/**
* Get a Fedora URL for a File entity from Gemini.
*
* @param string $uuid
* The File entity's UUID.
*
* @return string
* The Fedora URL corresponding to the UUID, or a message.
*/
public function _getFedoraUrl($uuid) {
try {
$container = \Drupal::getContainer();
$jwt = $container->get('jwt.authentication.jwt');
$auth = 'Bearer ' . $jwt->generateToken();
$client = \Drupal::httpClient();
$options = [
'http_errors' => FALSE,
'headers' => ['Authorization' => $auth],
];
$url = $this->gemini_endpoint . '/' . $uuid;
$response = $client->request('GET', $url, $options);
$code = $response->getStatusCode();
if ($code == 200) {
$body = $response->getBody()->getContents();
$body_array = json_decode($body, TRUE);
return $body_array['fedora'];
}
elseif ($code == 404) {
$body = $response->getBody()->getContents();
return 'Not in Fedora';
}
else {
\Drupal::logger('islandora_riprap')->error('HTTP response code: @code', ['@code' => $code]);
}
}
catch (RequestException $e) {
\Drupal::logger('islandora_riprap')->error($e->getMessage());
return "Sorry, there has been an error, please refer to the system log";
else {
return false;
}
$path = ltrim($path, '/');
$fedora_uri = "$fedora_root/$path";
return($fedora_uri);
}

/**
Expand All @@ -144,12 +105,19 @@ public function _getFedoraUrl($uuid) {
* incoming Media entity.
*/
public function getLocalUrl($mid) {
$media_fields = [
'field_media_file',
'field_media_image',
'field_media_audio_file',
'field_media_video_file',
];
if ($this->config->get('media_fields')) {
$media_fields_config = $this->config->get('media_fields');
$media_fields = preg_split('/\n/', $media_fields_config);
}
else {
$media_fields = [
'field_media_file',
'field_media_document',
'field_media_image',
'field_media_audio_file',
'field_media_video_file',
];
}
$media = Media::load($mid);
// Loop through each of the media fields and get the URL of the File
// in the first one encountered. Assumes each Media entity has only
Expand Down

0 comments on commit ac5f239

Please sign in to comment.