From cb26d52d3376f1587a4b48a0e7b3f5d1f4517f5b Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Fri, 7 Oct 2022 09:24:08 +1000 Subject: [PATCH] DEV: Remvoe IMAGES_EXTENSIONS_REGEX const in lib/uploads (#18497) This IMAGES_EXTENSIONS_REGEX const just creates redundancy when we already have the `isImage` function -- the two can easily get out of sync. Replace all usages of the former with the latter. --- .../javascripts/discourse/app/lib/uploads.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/uploads.js b/app/assets/javascripts/discourse/app/lib/uploads.js index 366e842c79e..02e0a2b6737 100644 --- a/app/assets/javascripts/discourse/app/lib/uploads.js +++ b/app/assets/javascripts/discourse/app/lib/uploads.js @@ -135,9 +135,6 @@ export function validateUploadedFile(file, opts) { return true; } -export const IMAGES_EXTENSIONS_REGEX = - /(png|jpe?g|gif|svg|ico|heic|heif|webp)/i; - function extensionsToArray(exts) { return exts .toLowerCase() @@ -155,12 +152,10 @@ function staffExtensions(siteSettings) { } function imagesExtensions(staff, siteSettings) { - let exts = extensions(siteSettings).filter((ext) => - IMAGES_EXTENSIONS_REGEX.test(ext) - ); + let exts = extensions(siteSettings).filter((ext) => isImage(`.${ext}`)); if (staff) { const staffExts = staffExtensions(siteSettings).filter((ext) => - IMAGES_EXTENSIONS_REGEX.test(ext) + isImage(`.${ext}`) ); exts = exts.concat(staffExts); } @@ -234,7 +229,7 @@ export function authorizesOneOrMoreImageExtensions(staff, siteSettings) { } export function isImage(path) { - return /\.(png|webp|jpe?g|gif|svg|ico)$/i.test(path); + return /\.(png|webp|jpe?g|gif|svg|ico|heic|heif)$/i.test(path); } export function isVideo(path) { @@ -252,9 +247,7 @@ function uploadTypeFromFileName(fileName) { export function allowsImages(staff, siteSettings) { return ( authorizesAllExtensions(staff, siteSettings) || - IMAGES_EXTENSIONS_REGEX.test( - authorizedExtensions(staff, siteSettings).join() - ) + authorizedExtensions(staff, siteSettings).some((ext) => isImage(`.${ext}`)) ); }