Skip to content

Commit

Permalink
SafeImg -> ProxyImg
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalmi committed Aug 22, 2023
1 parent 593f5c7 commit 1022c54
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const safeOrigins = [
'https://imgproxy.iris.to/',
];

export const isSafeOrigin = (url: string) => {
export const shouldSkipProxy = (url: string) => {
return safeOrigins.some((origin) => url.startsWith(origin));
};

const SafeImg = (props: Props) => {
const ProxyImg = (props: Props) => {
const [proxyFailed, setProxyFailed] = useState(false);
const [src, setSrc] = useState(props.src);

Expand All @@ -31,7 +31,7 @@ const SafeImg = (props: Props) => {
if (
props.src &&
!props.src.startsWith('data:image') &&
(!isSafeOrigin(props.src) || props.width)
(!shouldSkipProxy(props.src) || props.width)
) {
const originalSrc = props.src;
if (props.width) {
Expand Down Expand Up @@ -70,4 +70,4 @@ const SafeImg = (props: Props) => {
);
};

export default SafeImg;
export default ProxyImg;
6 changes: 3 additions & 3 deletions src/js/components/embed/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState } from 'react';

import Show from '../helpers/Show';
import Modal from '../modal/Modal';
import SafeImg from '../SafeImg';
import ProxyImg from '../ProxyImg.tsx';

import Embed from './index';

Expand All @@ -26,15 +26,15 @@ const Image: Embed = {
key={match + index}
className="flex justify-center items-center md:justify-start min-h-96 my-2"
>
<SafeImg
<ProxyImg
onError={() => setHasError(true)}
onClick={onClick}
className="my-2 rounded md:max-h-96 max-w-full cursor-pointer"
src={match}
/>
<Show when={showModal}>
<Modal centerVertically={true} onClose={() => setShowModal(false)}>
<SafeImg className="rounded max-h-[90vh] max-w-[90vw]" src={match} />
<ProxyImg className="rounded max-h-[90vh] max-w-[90vw]" src={match} />
</Modal>
</Show>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/js/components/feed/ImageGridItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { route } from 'preact-router';

import { ImageOrVideo } from '@/components/feed/types';
import SafeImg from '@/components/SafeImg';
import ProxyImg from '@/components/ProxyImg.tsx';
import Key from '@/nostr/Key';
import Icons from '@/utils/Icons';

Expand Down Expand Up @@ -34,7 +34,7 @@ export const ImageGridItem = ({
}}
ref={lastElementRef}
>
<SafeImg square={true} width={319} src={url} alt="" className="w-full h-full object-cover" />
<ProxyImg square={true} width={319} src={url} alt="" className="w-full h-full object-cover" />
{item.type === 'video' && (
<div className="absolute top-0 right-0 m-2 shadow-md shadow-gray-500 ">{Icons.video}</div>
)}
Expand Down
4 changes: 2 additions & 2 deletions src/js/components/feed/ImageModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/20/solid';
import EventComponent from '@/components/events/EventComponent';
import { ImageOrVideo } from '@/components/feed/types';
import Modal from '@/components/modal/Modal';
import SafeImg from '@/components/SafeImg';
import ProxyImg from '@/components/ProxyImg.tsx';

type ImageModalProps = {
mediaItems: Array<ImageOrVideo>;
Expand Down Expand Up @@ -63,7 +63,7 @@ const ImageModal = ({ mediaItems, activeItemIndex, setActiveItemIndex }: ImageMo
poster={`https://imgproxy.iris.to/thumbnail/638/${activeItem.url}`}
/>
) : (
<SafeImg
<ProxyImg
key={activeItem.url}
className="max-h-full max-w-full object-contain"
src={activeItem.url}
Expand Down
4 changes: 2 additions & 2 deletions src/js/components/modal/Image.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Modal from '@/components/modal/Modal';
import SafeImg from '@/components/SafeImg';
import ProxyImg from '@/components/ProxyImg.tsx';

type SimpleImageModalProps = {
imageUrl: string;
Expand All @@ -10,7 +10,7 @@ const SimpleImageModal = ({ imageUrl, onClose }: SimpleImageModalProps) => {
return (
<Modal width="100%" height="100%" onClose={onClose}>
<div className="flex h-full justify-center items-center">
<SafeImg className="max-h-full max-w-full object-contain" src={imageUrl} />
<ProxyImg className="max-h-full max-w-full object-contain" src={imageUrl} />
</div>
</Modal>
);
Expand Down
4 changes: 2 additions & 2 deletions src/js/components/searchbox/SearchResult.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import SafeImg from '@/components/SafeImg.tsx';
import ProxyImg from '@/components/ProxyImg.tsx';
import Avatar from '@/components/user/Avatar.tsx';
import Name from '@/components/user/Name.tsx';
import Key from '@/nostr/Key.ts';
Expand Down Expand Up @@ -28,7 +28,7 @@ export default function SearchResult({ item, onClick, onFocus, selected }) {
onClick={(e) => onClick(e, item)}
>
{item.picture ? (
<SafeImg src={item.picture} className="rounded-full" width={40} />
<ProxyImg src={item.picture} className="rounded-full" width={40} />
) : (
<Avatar key={`${npub}ic`} str={npub} width={40} />
)}
Expand Down
4 changes: 2 additions & 2 deletions src/js/components/user/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useProfile } from '@/nostr/hooks/useProfile.ts';
import Key from '../../nostr/Key';
import SocialNetwork from '../../nostr/SocialNetwork';
import Show from '../helpers/Show';
import SafeImg from '../SafeImg';
import ProxyImg from '../ProxyImg.tsx';

type Props = {
str: unknown;
Expand Down Expand Up @@ -62,7 +62,7 @@ const MyAvatar: React.FC<Props> = (props) => {
>
<div>
<Show when={hasPic}>
<SafeImg
<ProxyImg
className="object-cover rounded-full"
src={picture || ''}
width={width}
Expand Down
6 changes: 3 additions & 3 deletions src/js/components/user/ProfilePicture.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'preact/hooks';

import Modal from '../modal/Modal';
import SafeImg from '../SafeImg';
import ProxyImg from '../ProxyImg.tsx';

type Props = { picture?: string; onError?: () => void };

Expand All @@ -22,7 +22,7 @@ const ProfilePicture = ({ picture, onError }: Props) => {

return (
<div className="rounded-full border-4 border-black bg-black">
<SafeImg
<ProxyImg
width={128}
square={true}
className="rounded-full cursor-pointer"
Expand All @@ -32,7 +32,7 @@ const ProfilePicture = ({ picture, onError }: Props) => {
/>
{showModal && (
<Modal centerVertically={true} onClose={handleClose}>
<SafeImg
<ProxyImg
className="max-w-full max-h-[90vh]"
square={true}
src={picture}
Expand Down
4 changes: 2 additions & 2 deletions src/js/views/profile/EditProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RouteProps } from '@/views/types.ts';

import Upload from '../../components/buttons/Upload.tsx';
import Header from '../../components/header/Header.tsx';
import SafeImg from '../../components/SafeImg.tsx';
import ProxyImg from '../../components/ProxyImg.tsx';
import Key from '../../nostr/Key.ts';
import SocialNetwork from '../../nostr/SocialNetwork.ts';
import { translate as t } from '../../translations/Translation.mjs';
Expand Down Expand Up @@ -115,7 +115,7 @@ const EditProfile: React.FC<RouteProps> = () => {
</p>
{val && (
<p>
<SafeImg key={val} src={val} />
<ProxyImg key={val} src={val} />
</p>
)}
</>
Expand Down
4 changes: 2 additions & 2 deletions src/js/views/profile/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ProfileHelmet from '@/views/profile/Helmet.tsx';

import Feed from '../../components/feed/Feed.tsx';
import Show from '../../components/helpers/Show.tsx';
import { isSafeOrigin } from '../../components/SafeImg.tsx';
import { shouldSkipProxy } from '../../components/ProxyImg.tsx';
import ProfileCard from '../../components/user/ProfileCard.tsx';
import Key from '../../nostr/Key.ts';
import SocialNetwork from '../../nostr/SocialNetwork.ts';
Expand Down Expand Up @@ -50,7 +50,7 @@ function Profile(props) {
return;
}

bannerURL = isSafeOrigin(bannerURL)
bannerURL = shouldSkipProxy(bannerURL)
? bannerURL
: `https://imgproxy.iris.to/insecure/rs:fit:948:948/plain/${bannerURL}`;

Expand Down

0 comments on commit 1022c54

Please sign in to comment.