fix: make sure cachedimage component only works with tmdb urls

This commit is contained in:
Ryan Cohen
2022-09-30 15:55:48 +09:00
committed by sct
parent 063beea2ca
commit ad88048a94

View File

@@ -7,21 +7,18 @@ const imageLoader: ImageLoader = ({ src }) => src;
/**
* The CachedImage component should be used wherever
* we want to offer the option to locally cache images.
*
* It uses the `next/image` Image component but overrides
* the `unoptimized` prop based on the application setting `cacheImages`.
**/
const CachedImage = ({ src, ...props }: ImageProps) => {
const { currentSettings } = useSettings();
let imageUrl = src;
if (
typeof imageUrl === 'string' &&
imageUrl.startsWith('https://image.tmdb.org') &&
currentSettings.cacheImages
) {
imageUrl = imageUrl.replace('https://image.tmdb.org', '/imageproxy');
if (typeof imageUrl === 'string') {
const parsedUrl = new URL(imageUrl);
if (parsedUrl.host === 'image.tmdb.org' && currentSettings.cacheImages) {
imageUrl = imageUrl.replace('https://image.tmdb.org', '/imageproxy');
}
}
return <Image unoptimized loader={imageLoader} src={imageUrl} {...props} />;