* refactor: proxy and cache user avatar images * fix: extract keys * fix: set avatar image URL * fix: show the correct avatar in the list of available users in advanced request * fix(s): set correct src URL for cached image * fix: remove unexpired unused image when a user changes their avatar * fix: requested changes * refactor: use 'mime' package to detmerine file extension * style: grammar * refactor: checks if the default avatar is cached to avoid creating duplicates for different users * fix: fix vulnerability * fix: fix incomplete URL substring sanitization * refactor: only cache avatar with http url protocol * fix: remove log and correctly set the if statement for the cached image component * fix: avatar images not showing on issues page * style: formatting --------- Co-authored-by: JoaquinOlivero <joaquin.olivero@hotmail.com>
31 lines
932 B
TypeScript
31 lines
932 B
TypeScript
import useSettings from '@app/hooks/useSettings';
|
|
import type { ImageLoader, ImageProps } from 'next/image';
|
|
import Image from 'next/image';
|
|
|
|
const imageLoader: ImageLoader = ({ src }) => src;
|
|
|
|
/**
|
|
* The CachedImage component should be used wherever
|
|
* we want to offer the option to locally cache images.
|
|
**/
|
|
const CachedImage = ({ src, ...props }: ImageProps) => {
|
|
const { currentSettings } = useSettings();
|
|
|
|
let imageUrl = src;
|
|
|
|
if (typeof imageUrl === 'string' && imageUrl.startsWith('http')) {
|
|
const parsedUrl = new URL(imageUrl);
|
|
|
|
if (parsedUrl.host === 'image.tmdb.org') {
|
|
if (currentSettings.cacheImages)
|
|
imageUrl = imageUrl.replace('https://image.tmdb.org', '/imageproxy');
|
|
} else if (parsedUrl.host !== 'gravatar.com') {
|
|
imageUrl = '/avatarproxy/' + imageUrl;
|
|
}
|
|
}
|
|
|
|
return <Image unoptimized loader={imageLoader} src={imageUrl} {...props} />;
|
|
};
|
|
|
|
export default CachedImage;
|