add check to make sure the image is valid

This commit is contained in:
erik-sn committed 2020-09-20 20:34:42 -04:00
1 parent 1d719a5b2e
commit 1e900baab9
3 files changed
+10 -9

No files matched your search

@@ -115,8 +115,6 @@ const TEMP_MEDIA = [
}, },
]; ];
const URL_REGEX = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;
export const AddTweetModal = ({ visible, handleClose }) => { export const AddTweetModal = ({ visible, handleClose }) => {
const [showEmoji, setShowEmoji] = useState(false); const [showEmoji, setShowEmoji] = useState(false);
@@ -170,14 +168,16 @@ export const AddTweetModal = ({ visible, handleClose }) => {
const addMedia = useCallback(() => { const addMedia = useCallback(() => {
setMediaType(null); setMediaType(null);
setMediaLink(null); setMediaLink(null);
// only add the media if it's a valid URL // only add the media if it's a valid URL
// TODO - can we test that it's a valid image in JS? const image = new Image();
if (mediaLink.match(URL_REGEX)) { image.onload = () => {
// 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.
setMedia(media.concat([{ id: uuidv4(), mediaType, mediaLink }])); setMedia(media.concat([{ id: uuidv4(), mediaType, mediaLink }]));
} };
image.onerror = () => {
console.warn("Invalid image: ", mediaLink);
};
image.src = mediaLink;
}, [mediaType, mediaLink]); }, [mediaType, mediaLink]);
const _removeMedia = (id) => setMedia(media.filter((m) => m.id !== id)); const _removeMedia = (id) => setMedia(media.filter((m) => m.id !== id));
const removeMedia = useCallback(_removeMedia, [media]); const removeMedia = useCallback(_removeMedia, [media]);
@@ -26,7 +26,7 @@ const useStyles = makeStyles((theme) => ({
}, },
img: { img: {
maxHeight: "125px", // constrain images so we don't overwhelm the UI maxHeight: "125px", // constrain images so we don't overwhelm the UI
maxWidth: "500px", maxWidth: "300px",
padding: "0px 3px 3px 0px", padding: "0px 3px 3px 0px",
}, },
})); }));
@@ -26,6 +26,7 @@ export const MediaPrompt = ({ visible, value, handleChange }) => {
className={classes.textField} className={classes.textField}
onChange={handleChange} onChange={handleChange}
multiline multiline
size="small"
placeholder={t("APPS_TWITTER_MEDIA_PLACEHOLDER")} placeholder={t("APPS_TWITTER_MEDIA_PLACEHOLDER")}
/> />
); );