Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: look for image on url head before rendering preview #6127

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions app/containers/message/Urls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ const UrlImage = ({ image, hasContent }: { image: string; hasContent: boolean })
overflow: 'hidden',
alignItems: 'center',
justifyContent: 'center',
...imageDimensions.width <= 64 && { width: 64 },
...imageDimensions.height <= 64 && { height: 64 }
...(imageDimensions.width <= 64 && { width: 64 }),
...(imageDimensions.height <= 64 && { height: 64 })
};
if (!hasContent) {
containerStyle = {
Expand Down Expand Up @@ -130,14 +130,33 @@ const Url = ({ url }: { url: IUrl }) => {
const { colors, theme } = useTheme();
const { baseUrl, user } = useContext(MessageContext);
const API_Embed = useAppSelector(state => state.settings.API_Embed);
const [imageUrl, setImageUrl] = useState('');

useEffect(() => {
const verifyUrlIsImage = async () => {
try {
const imageUrl = getImageUrl();
if (!imageUrl) return;

const response = await fetch(imageUrl, { method: 'HEAD' });
const contentType = response.headers.get('content-type');
if (contentType?.startsWith?.('image/')) {
setImageUrl(imageUrl);
}
} catch {
// do nothing
}
};
verifyUrlIsImage();
}, [url.image, url.url]);

const getImageUrl = () => {
const imageUrl = url.image || url.url;
const _imageUrl = url.image || url.url;

if (!imageUrl) return null;
if (imageUrl.includes('http')) return imageUrl;
return `${baseUrl}/${imageUrl}?rc_uid=${user.id}&rc_token=${user.token}`;
if (!_imageUrl) return null;
if (_imageUrl.includes('http')) return _imageUrl;
return `${baseUrl}/${_imageUrl}?rc_uid=${user.id}&rc_token=${user.token}`;
};
const image = getImageUrl();

const onPress = () => openLink(url.url, theme);

Expand Down Expand Up @@ -168,9 +187,9 @@ const Url = ({ url }: { url: IUrl }) => {
]}
background={Touchable.Ripple(colors.surfaceNeutral)}>
<>
{image ? (
{imageUrl ? (
<WidthAwareView>
<UrlImage image={image} hasContent={hasContent} />
<UrlImage image={imageUrl} hasContent={hasContent} />
</WidthAwareView>
) : null}
{hasContent ? <UrlContent title={url.title} description={url.description} /> : null}
Expand Down