From f1e23ac056b3d90a8b1a5075cfea6d47ae512ea7 Mon Sep 17 00:00:00 2001 From: Nicholas Pease Date: Fri, 19 Apr 2024 01:59:43 -0400 Subject: [PATCH] Better Picture Detection / Handling --- frontend-next/src/components/app/datatypes.js | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/frontend-next/src/components/app/datatypes.js b/frontend-next/src/components/app/datatypes.js index 48da176..d17892f 100644 --- a/frontend-next/src/components/app/datatypes.js +++ b/frontend-next/src/components/app/datatypes.js @@ -37,29 +37,42 @@ let dateOptions = { minute: "2-digit", }; +/** + * Photo URL Test + * @param {String} url - URL to Test + * @returns {Boolean} - Image Loaded (True) or Not (False) + */ +function imageProcessing(url) { + var img = new Image(); + img.src = url; + return img.complete; +} + + /** * Rich Message Formatting * @param {String} message - Message to Format * @returns {String} - Formatted Message (IN HTML) */ function RMF(message) { - var IMG_END = [".jpg", ".jpeg", ".png", ".gif", ".webp"] var URLREGEX = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g; var URLmatch = message.match(URLREGEX); var newMessage = URLmatch ? [] : message if (URLmatch) { for (var i = 0; i < URLmatch.length; i++) { - if (IMG_END.includes(URLmatch[i].slice(-4)) || IMG_END.includes(URLmatch[i].slice(-5))) { - // Its a photo - newMessage.push(()) - } else { + if (imageProcessing("https://"+URLmatch[i])) { newMessage.push(( - {URLmatch.length == 1 && message.split(URLmatch[i])[0]} - {URLmatch[i]} + {(URLmatch.length == 1) && message.split(URLmatch[i])[0].replace("https://","")} + {(i == URLmatch.length || URLmatch.length == 1) && message.split(URLmatch[i])[1]} )) + } else { + newMessage.push(( + {URLmatch.length == 1 && message.split(URLmatch[i])[0]} + {URLmatch[i]} + {(i == URLmatch.length || URLmatch.length == 1) && message.split(URLmatch[i])[1]} + )) } - } } return newMessage -- 2.52.0