Skip to content

fix(view-data): Add image content type check for URLs in view data page #255

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
88 changes: 46 additions & 42 deletions src/components/Points/PointImage.jsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { Box, CardMedia, Modal, Typography } from '@mui/material';

function PointImage({ data, sx }) {
const [fullScreenImg, setFullScreenImg] = useState(null);
const renderImages = () => {
const images = [];

function isImgUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
if (url) {
return /\.(jpg|jpeg|png|webp|gif|svg)$/.test(url.pathname);
}
return false;
}
const [imageUrls, setImageUrls] = useState([]);

// Loop through the object's properties
for (const key in data) {
if (typeof data[key] == 'string') {
// Check if the value is an image URL
if (isImgUrl(data[key])) {
images.push(
<CardMedia
component="img"
sx={{
width: 150,
margin: 'auto',
padding: 1,
wordWrap: 'break-word',
p: 1,
border: '1px solid #ccc',
borderRadius: '5px',
...sx,
}}
key={key}
image={data[key]}
alt={data[key]}
onClick={() => setFullScreenImg(data[key])}
/>
);
useEffect(() => {
const fetchImageUrls = async () => {
const urls = [];
for (const key in data) {
if (typeof data[key] === 'string') {
Copy link
Member

@Anush008 Anush008 Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought.
Maybe a Promise.all to check them all at once?

try {
const url = new URL(data[key]);
if (/\.(jpg|jpeg|png|webp|gif|svg)$/.test(url.pathname)) {
urls.push({ key, url: data[key] });
continue;
}
const response = await fetch(url, { method: 'HEAD' });
const contentType = response.headers.get('content-type');
if (contentType && contentType.startsWith('image/')) {
urls.push({ key, url: data[key] });
}
} catch (_) {
// Ignore invalid URLs
}
}
}
}
setImageUrls(urls);
};

return images;
fetchImageUrls();
}, [data]);

const renderImages = () => {
return imageUrls.map(({ key, url }) => (
<CardMedia
component="img"
sx={{
width: 150,
margin: 'auto',
padding: 1,
wordWrap: 'break-word',
p: 1,
border: '1px solid #ccc',
borderRadius: '5px',
...sx,
}}
key={key}
image={url}
alt={url}
onClick={() => setFullScreenImg(url)}
/>
));
};

const images = renderImages();
Expand Down
Loading