Fix images from media domain not loaded if imgproxyOnly enabled (#1153)
This commit is contained in:
parent
f0403a2bbe
commit
902875bd55
|
@ -1,6 +1,6 @@
|
||||||
import styles from './text.module.css'
|
import styles from './text.module.css'
|
||||||
import { Fragment, useState, useEffect, useMemo, useCallback, forwardRef, useRef, memo } from 'react'
|
import { Fragment, useState, useEffect, useMemo, useCallback, forwardRef, useRef, memo } from 'react'
|
||||||
import { IMGPROXY_URL_REGEXP } from '@/lib/url'
|
import { IMGPROXY_URL_REGEXP, MEDIA_DOMAIN_REGEXP } from '@/lib/url'
|
||||||
import { useShowModal } from './modal'
|
import { useShowModal } from './modal'
|
||||||
import { useMe } from './me'
|
import { useMe } from './me'
|
||||||
import { Dropdown } from 'react-bootstrap'
|
import { Dropdown } from 'react-bootstrap'
|
||||||
|
@ -63,7 +63,7 @@ function ImageOriginal ({ src, topLevel, rel, tab, children, onClick, ...props }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function ImageProxy ({ src, srcSet: { dimensions, ...srcSetObj } = {}, onClick, topLevel, onError, ...props }) {
|
function TrustedImage ({ src, srcSet: { dimensions, ...srcSetObj } = {}, onClick, topLevel, onError, ...props }) {
|
||||||
const srcSet = useMemo(() => {
|
const srcSet = useMemo(() => {
|
||||||
if (Object.keys(srcSetObj).length === 0) return undefined
|
if (Object.keys(srcSetObj).length === 0) return undefined
|
||||||
// srcSetObj shape: { [widthDescriptor]: <imgproxyUrl>, ... }
|
// srcSetObj shape: { [widthDescriptor]: <imgproxyUrl>, ... }
|
||||||
|
@ -132,7 +132,7 @@ export default function ZoomableImage ({ src, srcSet, ...props }) {
|
||||||
const showModal = useShowModal()
|
const showModal = useShowModal()
|
||||||
|
|
||||||
// if `srcSet` is falsy, it means the image was not processed by worker yet
|
// if `srcSet` is falsy, it means the image was not processed by worker yet
|
||||||
const [imgproxy, setImgproxy] = useState(!!srcSet || IMGPROXY_URL_REGEXP.test(src))
|
const [trustedDomain, setTrustedDomain] = useState(!!srcSet || IMGPROXY_URL_REGEXP.test(src) || MEDIA_DOMAIN_REGEXP.test(src))
|
||||||
|
|
||||||
// backwards compatibility:
|
// backwards compatibility:
|
||||||
// src may already be imgproxy url since we used to replace image urls with imgproxy urls
|
// src may already be imgproxy url since we used to replace image urls with imgproxy urls
|
||||||
|
@ -158,13 +158,13 @@ export default function ZoomableImage ({ src, srcSet, ...props }) {
|
||||||
</Dropdown.Item>)
|
</Dropdown.Item>)
|
||||||
}), [showModal, originalUrl, styles])
|
}), [showModal, originalUrl, styles])
|
||||||
|
|
||||||
const handleError = useCallback(() => setImgproxy(false), [setImgproxy])
|
const handleError = useCallback(() => setTrustedDomain(false), [setTrustedDomain])
|
||||||
|
|
||||||
if (!src) return null
|
if (!src) return null
|
||||||
|
|
||||||
if (imgproxy) {
|
if (trustedDomain) {
|
||||||
return (
|
return (
|
||||||
<ImageProxy
|
<TrustedImage
|
||||||
src={src} srcSet={srcSet}
|
src={src} srcSet={srcSet}
|
||||||
onClick={handleClick} onError={handleError} {...props}
|
onClick={handleClick} onError={handleError} {...props}
|
||||||
/>
|
/>
|
||||||
|
|
|
@ -89,5 +89,7 @@ export const URL_REGEXP = /^((https?|ftp):\/\/)?(www.)?(((([a-z]|\d|-|\.|_|~|[\u
|
||||||
export const WS_REGEXP = /^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|(?=[^\/]{1,254}(?![^\/]))(?:(?=[a-zA-Z0-9-]{1,63}\.)(?:xn--+)?[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,63})(:([0-9]{1,5}))?(\/[^\s`@#$^&=.?"{}\\]+\/?)*([^\s`@#$^&=?"{}\/\\]+)?(\?[^\s`#$^"{}\\]+)*$/
|
export const WS_REGEXP = /^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|(?=[^\/]{1,254}(?![^\/]))(?:(?=[a-zA-Z0-9-]{1,63}\.)(?:xn--+)?[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,63})(:([0-9]{1,5}))?(\/[^\s`@#$^&=.?"{}\\]+\/?)*([^\s`@#$^&=?"{}\/\\]+)?(\?[^\s`#$^"{}\\]+)*$/
|
||||||
|
|
||||||
export const IMGPROXY_URL_REGEXP = new RegExp(`^${process.env.NEXT_PUBLIC_IMGPROXY_URL}.*$`)
|
export const IMGPROXY_URL_REGEXP = new RegExp(`^${process.env.NEXT_PUBLIC_IMGPROXY_URL}.*$`)
|
||||||
|
export const MEDIA_DOMAIN_REGEXP = new RegExp(`^https?://${process.env.NEXT_PUBLIC_MEDIA_DOMAIN}/.*$`)
|
||||||
|
|
||||||
// this regex is not a bullet proof way of checking if a url points to an image. to be sure, fetch the url and check the mimetype
|
// this regex is not a bullet proof way of checking if a url points to an image. to be sure, fetch the url and check the mimetype
|
||||||
export const IMG_URL_REGEXP = /^(https?:\/\/.*\.(?:png|jpg|jpeg|gif))$/
|
export const IMG_URL_REGEXP = /^(https?:\/\/.*\.(?:png|jpg|jpeg|gif))$/
|
||||||
|
|
Loading…
Reference in New Issue