Browse Source

Merge pull request #547 from ajstanley/EMiC-210

Emic 210
pull/550/head 7.x-1.4-RC1
William Panting 10 years ago
parent
commit
a7c5a35166
  1. 1
      includes/datastream.inc
  2. 18
      includes/datastream.version.inc
  3. 37
      includes/mimetype.utils.inc

1
includes/datastream.inc

@ -52,7 +52,6 @@ function islandora_view_datastream(AbstractDatastream $datastream, $download = F
} }
if ($download) { if ($download) {
// Browsers will not append all extensions. // Browsers will not append all extensions.
$mime_detect = new MimeDetect();
$extension = '.' . islandora_get_extension_for_mimetype($datastream->mimetype); $extension = '.' . islandora_get_extension_for_mimetype($datastream->mimetype);
// Prevent adding on a duplicate extension. // Prevent adding on a duplicate extension.
$label = $datastream->label; $label = $datastream->label;

18
includes/datastream.version.inc

@ -275,21 +275,23 @@ function islandora_get_object_extensions(AbstractObject $object) {
function islandora_datastream_version_replace_form($form, &$form_state, AbstractDatastream $datastream) { function islandora_datastream_version_replace_form($form, &$form_state, AbstractDatastream $datastream) {
module_load_include('inc', 'islandora', 'includes/content_model'); module_load_include('inc', 'islandora', 'includes/content_model');
module_load_include('inc', 'islandora', 'includes/utilities'); module_load_include('inc', 'islandora', 'includes/utilities');
module_load_include('inc', 'islandora', 'includes/mimetype.utils');
$object = islandora_object_load($datastream->parent->id); $object = islandora_object_load($datastream->parent->id);
$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); $datastream_mime_map = islandora_get_object_extensions($object);
$mime_detect = new MimeDetect(); $current_mime = $datastream->mimetype;
$ext = array(); $mimes = $datastream_mime_map[$datastream->id]['mime'];
if (isset($datastream_mime_map[$datastream->id])) { if (!in_array($current_mime, $mimes)) {
foreach ($datastream_mime_map[$datastream->id]['mime'] as $key => $value) { $mimes[] = $current_mime;
$extensions = $mime_detect->getValidExtensions($value); }
$ext = array_merge($ext, $extensions); $extensions = array();
} foreach ($mimes as $mime) {
$extensions = array_merge($extensions, islandora_get_extensions_for_mimetype($mime));
} }
$valid_extensions = implode(' ', $ext); $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(

37
includes/mimetype.utils.inc

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @file * @file
* Mimetype specific utility functions. * Mimetype specific utility functions.
@ -11,17 +12,19 @@
* The mimetype whose extension is required. * The mimetype whose extension is required.
* *
* @return string * @return string
* The extension mapped to the given mimetype. * The extension mapped to the given mimetype. Defaults to 'bin'.
*/ */
function islandora_get_extension_for_mimetype($mimetype) { function islandora_get_extension_for_mimetype($mimetype) {
// file.mimetypes.inc is a part of Drupal core, however is not // file.mimetypes.inc is a part of Drupal core, however is not
// automatically loaded. Manually require it. // automatically loaded. Manually require it.
require_once DRUPAL_ROOT . "/includes/file.mimetypes.inc"; require_once DRUPAL_ROOT . "/includes/file.mimetypes.inc";
$extension = 'bin';
$mimetype_mapping = file_mimetype_mapping(); $mimetype_mapping = file_mimetype_mapping();
$extension_index = array_search($mimetype, $mimetype_mapping['mimetypes']); $extension_index = array_search($mimetype, $mimetype_mapping['mimetypes']);
$mime_array_flipped = array_reverse($mimetype_mapping['extensions']); if ($extension_index !== FALSE) {
$extension = array_search($extension_index, $mime_array_flipped); $mime_array_flipped = array_reverse($mimetype_mapping['extensions']);
$extension = array_search($extension_index, $mime_array_flipped);
}
// We can only have one mapping in drupal for 'xml'. // We can only have one mapping in drupal for 'xml'.
if ($mimetype == "text/xml") { if ($mimetype == "text/xml") {
@ -29,3 +32,29 @@ function islandora_get_extension_for_mimetype($mimetype) {
} }
return $extension; return $extension;
} }
/**
* Retrieve all file extensions for a give mimetype.
*
* @param string $mimetype
* The mimetype whose extensions are required.
*
* @return array
* All known legal extensions.
*/
function islandora_get_extensions_for_mimetype($mimetype) {
// file.mimetypes.inc is a part of Drupal core, however is not
// automatically loaded. Manually require it.
require_once DRUPAL_ROOT . "/includes/file.mimetypes.inc";
$mimetype_mapping = file_mimetype_mapping();
$index = array_search($mimetype, $mimetype_mapping['mimetypes']);
$extensions = array();
if ($index !== FALSE) {
$extensions = array_keys($mimetype_mapping['extensions'], $index);
}
// We can only have one mapping in drupal for 'xml'.
if ($mimetype == "text/xml") {
$extensions[] = 'xml';
}
return $extensions;
}

Loading…
Cancel
Save