mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
add check to make sure the image is valid
This commit is contained in:
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 }) => {
|
||||
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]);
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -26,6 +26,7 @@ export const MediaPrompt = ({ visible, value, handleChange }) => {
|
||||
className={classes.textField}
|
||||
onChange={handleChange}
|
||||
multiline
|
||||
size="small"
|
||||
placeholder={t("APPS_TWITTER_MEDIA_PLACEHOLDER")}
|
||||
/>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user