From 38d2da6ce72a7c809c8a69a7cd25379b94cfc594 Mon Sep 17 00:00:00 2001 From: indigoxela Date: Fri, 3 May 2024 09:31:05 +0200 Subject: [PATCH] Issue #94: Add SVG support for drag-and-drop upload (#95) --- tinymce.pages.inc | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/tinymce.pages.inc b/tinymce.pages.inc index e7d9eb2..e8a0dc0 100644 --- a/tinymce.pages.inc +++ b/tinymce.pages.inc @@ -28,8 +28,10 @@ function tinymce_image_upload($format) { } $destination = $upload_settings['scheme'] . '://' . $upload_settings['directory']; + $extensions = image_get_supported_extensions(); $validators = array( - 'file_validate_is_image' => array(), + 'file_validate_extensions' => array(implode(' ', $extensions)), + 'tinymce_validate_is_image' => array(), ); if ($upload_settings['max_size']) { $validators['file_validate_size'] = array(parse_size($upload_settings['max_size'])); @@ -52,8 +54,8 @@ function tinymce_image_upload($format) { 'uploaded' => 1, 'location' => $url, 'fileId' => $file->fid, - 'width' => $image_info['width'], - 'height' => $image_info['height'], + 'width' => !empty($image_info['width']) ? $image_info['width'] : '', + 'height' => !empty($image_info['height']) ? $image_info['height'] : '', ); } else { @@ -73,3 +75,28 @@ function tinymce_image_upload($format) { return $response; } + +/** + * Custom validation callback for image upload. + * + * Currently file_validate_is_image() fails for newly uploaded files, as the uri + * is something like "/tmp/phpAbCde" - without file extension. + * @see https://github.com/backdrop/backdrop-issues/issues/6497 + */ +function tinymce_validate_is_image(File $file) { + $errors = array(); + + // SVG have their own validator on upload (file_validate_svg). + // If SVG aren't supported, yet, TinyMCE would refuse to upload in the first + // place and even if upload was attempted, the server-side extension based + // validation would fail. + if ($file->filemime == 'image/svg+xml') { + return $errors; + } + // Validate that the image toolkit can handle this raster image. + if (!$info = image_get_info($file->uri)) { + $errors[] = t('The image appears to be broken.'); + } + + return $errors; +}