From 8efd0c23fc0a060e31cd608ef9eb13ff70260a78 Mon Sep 17 00:00:00 2001 From: MorganDawe Date: Thu, 28 Aug 2014 15:34:22 -0300 Subject: [PATCH] Updated file extension logic on ds download. --- includes/mime_detect.inc | 1 - includes/utilities.inc | 29 ++++++++++++++++++++++------- islandora.module | 1 + 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/includes/mime_detect.inc b/includes/mime_detect.inc index 29fcf1e5..da2c69f3 100644 --- a/includes/mime_detect.inc +++ b/includes/mime_detect.inc @@ -134,7 +134,6 @@ class MimeDetect { 'xhtml' => 'application/xhtml+xml', 'xsl' => 'text/xsl', 'xslt' => 'text/xsl', - 'xml' => 'application/xml', 'csv' => 'text/csv', 'tsv' => 'text/tab-separated-values', 'txt' => 'text/plain', diff --git a/includes/utilities.inc b/includes/utilities.inc index 4892a4eb..0612f895 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -46,15 +46,30 @@ function islandora_convert_bytes_to_human_readable($bytes, $precision = 2) { } } +/** + * Retrieve the correct file extension for a give mimetype. + * + * @param string $mimetype + * The mimetype whose extension is required. + * + * @return string + * The extension mapped to the given mimetype. + */ function islandora_get_extension_for_mimetype($mimetype) { - module_load_include('inc', 'file', 'file.mimetypes.inc'); - dsm($mimetype, "mimetype"); - $mimetype_mapping = file_default_mimetype_mapping(); - $extenstion_index = array_search($mimetype, $mimetype_mapping['mimetypes']); - $extenstion = array_search($extenstion_index, $mimetype_mapping['extensions']); - dsm($extenstion, "extenstion"); - return $extenstion; + require_once(DRUPAL_ROOT . '/includes/file.mimetypes.inc'); + + $mimetype_mapping = file_mimetype_mapping(); + $extension_index = array_search($mimetype, $mimetype_mapping['mimetypes']); + $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'. + if ($mimetype == "text/xml") { + return "xml"; + } + return $extension; } + /** * Add a file as managed if is not already. * diff --git a/islandora.module b/islandora.module index b2bb99b8..c930ba5f 100644 --- a/islandora.module +++ b/islandora.module @@ -1590,6 +1590,7 @@ function islandora_download_clip(AbstractObject $object) { function islandora_file_mimetype_mapping_alter(&$mapping) { $mime_detect = new MimeDetect(); $types = $mime_detect->getMimeTypes(); + $diff = array_diff_key($types, $mapping['extensions']); foreach ($diff as $ext => $mime) { $mapping['mimetypes'][] = $mime;