From 2b2c5c8601aa62b13d8d926e9d58d24c9d051c7b Mon Sep 17 00:00:00 2001 From: MorganDawe Date: Wed, 27 Aug 2014 16:43:53 -0300 Subject: [PATCH 1/5] Updating extestion and mime detection on download to use drupals file_g=default_mimetype_mapping() funcitons. --- includes/datastream.inc | 4 ++-- includes/utilities.inc | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/includes/datastream.inc b/includes/datastream.inc index edc8ceb1..e31ed254 100644 --- a/includes/datastream.inc +++ b/includes/datastream.inc @@ -30,6 +30,7 @@ function islandora_download_datastream(AbstractDatastream $datastream) { * The version of the datastream to display */ function islandora_view_datastream(AbstractDatastream $datastream, $download = FALSE, $version = NULL) { + module_load_include('inc', 'islandora', 'includes/utilities'); // XXX: Certain features of the Devel module rely on the use of "shutdown // handlers", such as query logging... The problem is that they might blindly // add additional output which will break things if what is actually being @@ -45,7 +46,6 @@ function islandora_view_datastream(AbstractDatastream $datastream, $download = F return drupal_not_found(); } } - header('Content-type: ' . $datastream->mimetype); if ($datastream->controlGroup == 'M' || $datastream->controlGroup == 'X') { header('Content-length: ' . $datastream->size); @@ -53,7 +53,7 @@ function islandora_view_datastream(AbstractDatastream $datastream, $download = F if ($download) { // Browsers will not append all extensions. $mime_detect = new MimeDetect(); - $extension = '.' . $mime_detect->getExtension($datastream->mimetype); + $extension = '.' . islandora_get_extension_for_mimetype($datastream->mimetype);//$mime_detect->getExtension($datastream->mimetype); // Prevent adding on a duplicate extension. $label = $datastream->label; $extension_length = strlen($extension); diff --git a/includes/utilities.inc b/includes/utilities.inc index 26633df5..4892a4eb 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -46,6 +46,15 @@ function islandora_convert_bytes_to_human_readable($bytes, $precision = 2) { } } +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; +} /** * Add a file as managed if is not already. * From 8efd0c23fc0a060e31cd608ef9eb13ff70260a78 Mon Sep 17 00:00:00 2001 From: MorganDawe Date: Thu, 28 Aug 2014 15:34:22 -0300 Subject: [PATCH 2/5] 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; From 4311be0f0dbde303e402a1e6b6165030734cf86e Mon Sep 17 00:00:00 2001 From: MorganDawe Date: Thu, 28 Aug 2014 15:36:49 -0300 Subject: [PATCH 3/5] removed unused code. --- includes/datastream.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/datastream.inc b/includes/datastream.inc index e31ed254..5f48211c 100644 --- a/includes/datastream.inc +++ b/includes/datastream.inc @@ -53,7 +53,7 @@ function islandora_view_datastream(AbstractDatastream $datastream, $download = F if ($download) { // Browsers will not append all extensions. $mime_detect = new MimeDetect(); - $extension = '.' . islandora_get_extension_for_mimetype($datastream->mimetype);//$mime_detect->getExtension($datastream->mimetype); + $extension = '.' . islandora_get_extension_for_mimetype($datastream->mimetype); // Prevent adding on a duplicate extension. $label = $datastream->label; $extension_length = strlen($extension); From 212276f6d01b87be1d9a799d835b64d8e0517c7f Mon Sep 17 00:00:00 2001 From: MorganDawe Date: Fri, 29 Aug 2014 08:55:21 -0300 Subject: [PATCH 4/5] Corrected small travis issue. --- includes/utilities.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index 0612f895..d3d2bbc3 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -56,7 +56,7 @@ function islandora_convert_bytes_to_human_readable($bytes, $precision = 2) { * The extension mapped to the given mimetype. */ function islandora_get_extension_for_mimetype($mimetype) { - require_once(DRUPAL_ROOT . '/includes/file.mimetypes.inc'); + require_once DRUPAL_ROOT . "/includes/file.mimetypes.inc"; $mimetype_mapping = file_mimetype_mapping(); $extension_index = array_search($mimetype, $mimetype_mapping['mimetypes']); From d2ecd81ba8ac8999d705184b26399fb992e77159 Mon Sep 17 00:00:00 2001 From: MorganDawe Date: Tue, 2 Sep 2014 16:19:42 -0300 Subject: [PATCH 5/5] Code review feedback. --- includes/datastream.inc | 2 +- includes/mimetype.utils.inc | 31 +++++++++++++++++++++++++++++++ includes/utilities.inc | 24 ------------------------ 3 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 includes/mimetype.utils.inc diff --git a/includes/datastream.inc b/includes/datastream.inc index 5f48211c..592870b7 100644 --- a/includes/datastream.inc +++ b/includes/datastream.inc @@ -30,7 +30,7 @@ function islandora_download_datastream(AbstractDatastream $datastream) { * The version of the datastream to display */ function islandora_view_datastream(AbstractDatastream $datastream, $download = FALSE, $version = NULL) { - module_load_include('inc', 'islandora', 'includes/utilities'); + module_load_include('inc', 'islandora', 'includes/mimetype.utils'); // XXX: Certain features of the Devel module rely on the use of "shutdown // handlers", such as query logging... The problem is that they might blindly // add additional output which will break things if what is actually being diff --git a/includes/mimetype.utils.inc b/includes/mimetype.utils.inc new file mode 100644 index 00000000..989c55cd --- /dev/null +++ b/includes/mimetype.utils.inc @@ -0,0 +1,31 @@ +