From 1e900baab976763182b78f33e287849ba3d95a13 Mon Sep 17 00:00:00 2001 From: erik-sn Date: Sun, 20 Sep 2020 20:34:42 -0400 Subject: [PATCH] add check to make sure the image is valid --- .../src/apps/twitter/components/AddTweetModal.js | 16 ++++++++-------- phone/src/apps/twitter/components/MediaImage.js | 2 +- phone/src/apps/twitter/components/MediaPrompt.js | 1 + 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/phone/src/apps/twitter/components/AddTweetModal.js b/phone/src/apps/twitter/components/AddTweetModal.js index bf0bd1c..49a6b48 100644 --- a/phone/src/apps/twitter/components/AddTweetModal.js +++ b/phone/src/apps/twitter/components/AddTweetModal.js @@ -115,8 +115,6 @@ const TEMP_MEDIA = [ }, ]; -const URL_REGEX = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/; - export const AddTweetModal = ({ visible, handleClose }) => { const [showEmoji, setShowEmoji] = useState(false); @@ -170,14 +168,16 @@ export const AddTweetModal = ({ visible, handleClose }) => { const addMedia = useCallback(() => { setMediaType(null); setMediaLink(null); + // only add the media if it's a valid URL - // TODO - can we test that it's a valid image in JS? - if (mediaLink.match(URL_REGEX)) { - // we add an ID here so that we can have a distinct identifier on the - // client before it is added to the database. This ID is NOT what used - // or contained in the database. + const image = new Image(); + image.onload = () => { setMedia(media.concat([{ id: uuidv4(), mediaType, mediaLink }])); - } + }; + image.onerror = () => { + console.warn("Invalid image: ", mediaLink); + }; + image.src = mediaLink; }, [mediaType, mediaLink]); const _removeMedia = (id) => setMedia(media.filter((m) => m.id !== id)); const removeMedia = useCallback(_removeMedia, [media]); diff --git a/phone/src/apps/twitter/components/MediaImage.js b/phone/src/apps/twitter/components/MediaImage.js index 9c90065..c3d313e 100644 --- a/phone/src/apps/twitter/components/MediaImage.js +++ b/phone/src/apps/twitter/components/MediaImage.js @@ -26,7 +26,7 @@ const useStyles = makeStyles((theme) => ({ }, img: { maxHeight: "125px", // constrain images so we don't overwhelm the UI - maxWidth: "500px", + maxWidth: "300px", padding: "0px 3px 3px 0px", }, })); diff --git a/phone/src/apps/twitter/components/MediaPrompt.js b/phone/src/apps/twitter/components/MediaPrompt.js index a5b670a..2bec2b0 100644 --- a/phone/src/apps/twitter/components/MediaPrompt.js +++ b/phone/src/apps/twitter/components/MediaPrompt.js @@ -26,6 +26,7 @@ export const MediaPrompt = ({ visible, value, handleChange }) => { className={classes.textField} onChange={handleChange} multiline + size="small" placeholder={t("APPS_TWITTER_MEDIA_PLACEHOLDER")} /> );