-
-
Notifications
You must be signed in to change notification settings - Fork 230
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
fix: rewrite avatarproxy and CachedImage #1016
Changes from 3 commits
27d7371
7ff242d
7429297
a17be16
c3bf6be
386f563
7a7330f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,24 +4,34 @@ import Image from 'next/image'; | |
|
||
const imageLoader: ImageLoader = ({ src }) => src; | ||
|
||
export type CachedImageProps = ImageProps & { | ||
src: string; | ||
type: 'tmdb' | 'avatar'; | ||
}; | ||
|
||
/** | ||
* The CachedImage component should be used wherever | ||
* we want to offer the option to locally cache images. | ||
**/ | ||
const CachedImage = ({ src, ...props }: ImageProps) => { | ||
const CachedImage = ({ src, type, ...props }: CachedImageProps) => { | ||
const { currentSettings } = useSettings(); | ||
|
||
let imageUrl = src; | ||
|
||
if (typeof imageUrl === 'string' && imageUrl.startsWith('http')) { | ||
const parsedUrl = new URL(imageUrl); | ||
let imageUrl: string; | ||
|
||
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; | ||
} | ||
if (type === 'tmdb') { | ||
// tmdb stuff | ||
imageUrl = | ||
currentSettings.cacheImages && !src.startsWith('/') | ||
? src.replace('https://image.tmdb.org', '/imageproxy') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. src.replace doesn't check if this string is present at the beginning so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
: src; | ||
} else if (type === 'avatar') { | ||
// jellyfin avatar (in any) | ||
const jellyfinAvatar = src.match( | ||
/(\/Users\/\w+\/Images\/Primary\/?\?tag=\w+&quality=90)$/ | ||
gauthier-th marked this conversation as resolved.
Show resolved
Hide resolved
|
||
)?.[1]; | ||
imageUrl = jellyfinAvatar ? `/avatarproxy` + jellyfinAvatar : src; | ||
} else { | ||
return null; | ||
} | ||
|
||
return <Image unoptimized loader={imageLoader} src={imageUrl} {...props} />; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regex should be set on router part and not inside the route no ?
And if you want to keep it here the regex look like wrong as it's missing
^
, would be catched : https://test.fake/Users/efefe/Images/Primary/?tag=efefe&quality=90 (even if it's useless)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be set in the router path yes. But I did it this way to be consistent with what is made on front-end. Both ways seems fine to me.
And having it in the router path would require to reconstruct the URL with the URL param and the URL query