Browse Source

Merge pull request #565 from willtp87/7.x.add_datastream_fix

Some fixes and improovements for datastream uploads.
pull/569/head
Adam 10 years ago
parent
commit
4a420e6c31
  1. 61
      includes/add_datastream.form.inc
  2. 20
      includes/datastream.version.inc
  3. 28
      includes/mimetype.utils.inc

61
includes/add_datastream.form.inc

@ -27,8 +27,8 @@ function islandora_add_datastream_form(array $form, array &$form_state, Abstract
// @deprecated Storing objects in $form_state is asking for a bad time... // @deprecated Storing objects in $form_state is asking for a bad time...
// Causes issues with derivative generation when we try to use it. // Causes issues with derivative generation when we try to use it.
$form_state['object'] = $object; $form_state['object'] = $object;
$form_state['datastream_requirements'] = islandora_get_missing_datastreams_requirements($object); $datastream_requirements = islandora_get_missing_datastreams_requirements($object);
$unused_datastreams = array_keys($form_state['datastream_requirements']); $unused_datastreams = array_keys($datastream_requirements);
$unused_datastreams = "'" . implode("', '", $unused_datastreams) . "'"; $unused_datastreams = "'" . implode("', '", $unused_datastreams) . "'";
$upload_size = min((int) ini_get('post_max_size'), (int) ini_get('upload_max_filesize')); $upload_size = min((int) ini_get('post_max_size'), (int) ini_get('upload_max_filesize'));
return array( return array(
@ -38,7 +38,7 @@ function islandora_add_datastream_form(array $form, array &$form_state, Abstract
), ),
'dsid_fieldset' => array( 'dsid_fieldset' => array(
'#type' => 'fieldset', '#type' => 'fieldset',
'#title' => 'Add a datastream', '#title' => t('Add a Datastream'),
'#collapsible' => FALSE, '#collapsible' => FALSE,
'#collapsed' => FALSE, '#collapsed' => FALSE,
'dsid' => array( 'dsid' => array(
@ -60,7 +60,7 @@ function islandora_add_datastream_form(array $form, array &$form_state, Abstract
'#required' => TRUE, '#required' => TRUE,
'#size' => 64, '#size' => 64,
'#maxlength' => 64, '#maxlength' => 64,
'#description' => t('A human-readable label'), '#description' => t('A human-readable label.'),
'#type' => 'textfield', '#type' => 'textfield',
'#element_validate' => array('islandora_add_datastream_form_field_does_not_contain_a_forward_slash'), '#element_validate' => array('islandora_add_datastream_form_field_does_not_contain_a_forward_slash'),
), ),
@ -69,10 +69,11 @@ function islandora_add_datastream_form(array $form, array &$form_state, Abstract
'#required' => TRUE, '#required' => TRUE,
'#title' => t('Upload Document'), '#title' => t('Upload Document'),
'#size' => 48, '#size' => 48,
'#description' => t('Select a file to upload.<br/>Files must be less than <b>@size MB.</b>', array('@size' => $upload_size)), '#description' => t('Select a file to upload.<br/>Files must be less than <strong>@size MB.</strong>', array('@size' => $upload_size)),
'#default_value' => isset($form_state['values']['files']) ? $form_state['values']['files'] : NULL, '#default_value' => isset($form_state['values']['files']) ? $form_state['values']['files'] : NULL,
'#upload_location' => 'temporary://', '#upload_location' => 'public://',
'#upload_validators' => array( '#upload_validators' => array(
// Disable default file_validate_extensions; we need direct control.
'file_validate_extensions' => array(NULL), 'file_validate_extensions' => array(NULL),
// Assume its specified in MB. // Assume its specified in MB.
'file_validate_size' => array($upload_size * 1024 * 1024), 'file_validate_size' => array($upload_size * 1024 * 1024),
@ -136,6 +137,29 @@ function islandora_add_datastream_form_field_is_valid_dsid(array $element, array
} }
} }
/**
* Validation callback for islandora_add_datastream_form.
*
* Checks if the given datastream can accept the given MIME type.
*/
function islandora_add_datastream_form_validate(array $form, array &$form_state) {
module_load_include('inc', 'islandora', 'includes/mimetype.utils');
$extensions = islandora_get_extensions_for_datastream(
$form_state['object'],
$form_state['values']['dsid']
);
$file = file_load($form_state['values']['file']);
if ($file) {
$errors = file_validate_extensions($file, implode(' ', $extensions));
if (count($errors) > 0) {
form_set_error(
'file',
t("!error (for the set DSID)", array('!error' => $errors[0]))
);
}
}
}
/** /**
* Checks if the given form field contains a "/" character. * Checks if the given form field contains a "/" character.
* *
@ -152,31 +176,6 @@ function islandora_add_datastream_form_field_does_not_contain_a_forward_slash(ar
} }
} }
/**
* Checks if the given datastream requires the upload to be a certain MIME type.
*
* @param array $form
* The Drupal form.
* @param array $form_state
* The Drupal form state.
*/
function islandora_add_datastream_form_validate(array $form, array &$form_state) {
$file = file_load($form_state['values']['file']);
$dsid = $form_state['values']['dsid'];
if (isset($form_state['datastream_requirements'][$dsid]) && $file) {
$allowed_types = $form_state['datastream_requirements'][$dsid]['mime'];
$mime_detect = new MimeDetect();
$allowed_extensions = array();
foreach ($allowed_types as $mime) {
$allowed_extensions = array_merge($allowed_extensions, $mime_detect->getValidExtensions($mime));
}
$errors = file_validate_extensions($file, implode(' ', $allowed_extensions));
if (count($errors) > 0) {
form_set_error('file', $errors[0]);
}
}
}
/** /**
* Adds the new datastream based on the submitted values. * Adds the new datastream based on the submitted values.
* *

20
includes/datastream.version.inc

@ -281,31 +281,23 @@ function islandora_datastream_version_replace_form($form, &$form_state, Abstract
$form_state['object_id'] = $object->id; $form_state['object_id'] = $object->id;
$form_state['dsid'] = $datastream->id; $form_state['dsid'] = $datastream->id;
$form_state['object'] = $object; $form_state['object'] = $object;
$datastream_mime_map = islandora_get_object_extensions($object);
$current_mime = $datastream->mimetype; $extensions = islandora_get_extensions_for_datastream($object, $datastream->id);
$mimes = $datastream_mime_map[$datastream->id]['mime'];
if (!in_array($current_mime, $mimes)) {
$mimes[] = $current_mime;
}
$extensions = array();
foreach ($mimes as $mime) {
$extensions = array_merge($extensions, islandora_get_extensions_for_mimetype($mime));
}
$valid_extensions = implode(' ', $extensions); $valid_extensions = implode(' ', $extensions);
$upload_size = min((int) ini_get('post_max_size'), (int) ini_get('upload_max_filesize')); $upload_size = min((int) ini_get('post_max_size'), (int) ini_get('upload_max_filesize'));
return array( return array(
'dsid_fieldset' => array( 'dsid_fieldset' => array(
'#type' => 'fieldset', '#type' => 'fieldset',
'#title' => t("Update datastream with latest version"), '#title' => t("Update Datastream"),
'#collapsible' => FALSE, '#collapsible' => FALSE,
'#collapsed' => FALSE, '#collapsed' => FALSE,
'dsid' => array( 'dsid' => array(
'#type' => 'markup', '#type' => 'markup',
'#markup' => "<div>DSID: <b>$datastream->id</b></div>", '#markup' => t("<div>DSID: <strong>@dsid</strong></div>", array('@dsid' => $datastream->id)),
), ),
'label' => array( 'label' => array(
'#type' => 'markup', '#type' => 'markup',
'#markup' => "<div>Label: <b>$datastream->label</b></div>", '#markup' => t("<div>Label: <strong>@label</strong></div>", array('@label' => $datastream->label)),
), ),
'file' => array( 'file' => array(
'#type' => 'managed_file', '#type' => 'managed_file',
@ -313,7 +305,7 @@ function islandora_datastream_version_replace_form($form, &$form_state, Abstract
'#title' => t('Upload Document'), '#title' => t('Upload Document'),
'#size' => 64, '#size' => 64,
'#description' => t('Select a file to upload.<br/>Files must be less than <strong>@size MB.</strong>', array('@size' => $upload_size)), '#description' => t('Select a file to upload.<br/>Files must be less than <strong>@size MB.</strong>', array('@size' => $upload_size)),
'#upload_location' => 'temporary://', '#upload_location' => 'public://',
'#upload_validators' => array( '#upload_validators' => array(
'file_validate_extensions' => array($valid_extensions), 'file_validate_extensions' => array($valid_extensions),
// Assume its specified in MB. // Assume its specified in MB.

28
includes/mimetype.utils.inc

@ -58,3 +58,31 @@ function islandora_get_extensions_for_mimetype($mimetype) {
} }
return $extensions; return $extensions;
} }
/**
* Mimes accepted by a datastream.
*
* @param AbstractObject $object
* Object to check for extensions.
* @param string $dsid
* Datastream ID to check for extensions.
*
* @return array
* Mimes accepted by the datastream ID on the object.
*/
function islandora_get_extensions_for_datastream(AbstractObject $object, $dsid) {
module_load_include('inc', 'islandora', 'includes/utilities');
$datastream_mime_map = islandora_get_datastreams_requirements_from_models($object->models);
$mimes = $datastream_mime_map[$dsid]['mime'];
if (isset($object[$dsid])) {
$current_mime = $object[$dsid]->mimetype;
if (!in_array($current_mime, $mimes)) {
$mimes[] = $current_mime;
}
}
$extensions = array();
foreach ($mimes as $mime) {
$extensions = array_merge($extensions, islandora_get_extensions_for_mimetype($mime));
}
return $extensions;
}

Loading…
Cancel
Save