From 5446f2914126dac7cacf74cdcfa8cc8eea7ba5b5 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Tue, 2 Jun 2020 18:02:04 -0500 Subject: [PATCH] improved filetype error handling --- core/extension.php | 12 ++++++++++-- ext/upload/main.php | 6 +++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/extension.php b/core/extension.php index 37d6cad9..18aa9703 100644 --- a/core/extension.php +++ b/core/extension.php @@ -317,6 +317,9 @@ abstract class DataHandlerExtension extends Extension if (is_null($image)) { throw new UploadException("Data handler failed to create image object from data"); } + if (empty($image->ext)) { + throw new UploadException("Unable to determine extension for ". $event->tmpname); + } try { send_event(new MediaCheckPropertiesEvent($image)); } catch (MediaException $e) { @@ -330,6 +333,9 @@ abstract class DataHandlerExtension extends Extension if (is_null($image)) { throw new UploadException("Data handler failed to create image object from data"); } + if (empty($image->ext)) { + throw new UploadException("Unable to determine extension for ". $event->tmpname); + } try { send_event(new MediaCheckPropertiesEvent($image)); } catch (MediaException $e) { @@ -404,9 +410,11 @@ abstract class DataHandlerExtension extends Extension $image->filename = (($pos = strpos($metadata['filename'], '?')) !== false) ? substr($metadata['filename'], 0, $pos) : $metadata['filename']; if ($config->get_bool("upload_use_mime")) { $image->ext = get_extension_for_file($filename); - } else { + } + if (empty($image->ext)) { $image->ext = (($pos = strpos($metadata['extension'], '?')) !== false) ? substr($metadata['extension'], 0, $pos) : $metadata['extension']; } + $image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']); $image->source = $metadata['source']; @@ -428,7 +436,7 @@ abstract class DataHandlerExtension extends Extension foreach (getSubclassesOf("DataHandlerExtension") as $handler) { $handler = (new $handler()); - foreach($handler->SUPPORTED_MIME as $mime) { + foreach ($handler->SUPPORTED_MIME as $mime) { $arr = array_merge($arr, get_all_extension_for_mime($mime)); } } diff --git a/ext/upload/main.php b/ext/upload/main.php index 8581d225..96eb7af3 100644 --- a/ext/upload/main.php +++ b/ext/upload/main.php @@ -48,7 +48,7 @@ class DataUploadEvent extends Event $filetype = get_extension_for_file($tmpname); } - if(empty($filetype)) { + if (empty($filetype)) { if (array_key_exists('extension', $metadata) && !empty($metadata['extension'])) { $filetype = strtolower($metadata['extension']); } else { @@ -56,6 +56,10 @@ class DataUploadEvent extends Event } } + if (empty($filetype)) { + throw new UploadException("Could not determine extension for file " . $metadata["filename"]); + } + $this->set_type($filetype); }