From e6e2523d46074a0d1dcf3a7eebb8022e9c092ae4 Mon Sep 17 00:00:00 2001 From: JoaquinOlivero Date: Wed, 4 Sep 2024 17:58:40 +0000 Subject: [PATCH] refactor: only cache avatar with http url protocol --- server/lib/imageproxy.ts | 2 +- server/routes/auth.ts | 29 +------------------ server/routes/avatarproxy.ts | 9 +----- server/routes/user/index.ts | 16 +--------- src/components/Common/CachedImage/index.tsx | 11 +++++-- .../IssueDetails/IssueComment/index.tsx | 2 +- src/components/IssueDetails/index.tsx | 2 +- src/components/Layout/UserDropdown/index.tsx | 4 +-- src/components/ManageSlideOver/index.tsx | 4 +-- src/components/RequestCard/index.tsx | 4 +-- .../RequestList/RequestItem/index.tsx | 8 ++--- .../RequestModal/AdvancedRequester/index.tsx | 4 +-- .../UserList/JellyfinImportModal.tsx | 2 +- src/components/UserList/index.tsx | 2 +- .../UserProfile/ProfileHeader/index.tsx | 2 +- 15 files changed, 30 insertions(+), 71 deletions(-) diff --git a/server/lib/imageproxy.ts b/server/lib/imageproxy.ts index 3b61e8424..badfe94f2 100644 --- a/server/lib/imageproxy.ts +++ b/server/lib/imageproxy.ts @@ -190,7 +190,7 @@ class ImageProxy { await promises.rm(directory, { recursive: true }); - logger.info(`Cleared ${files[0]} image from cache 'avatar'`, { + logger.info(`Cleared ${files[0]} from cache 'avatar'`, { label: 'Image Cache', }); } catch (e) { diff --git a/server/routes/auth.ts b/server/routes/auth.ts index 2778f1a36..4e7f77278 100644 --- a/server/routes/auth.ts +++ b/server/routes/auth.ts @@ -344,13 +344,6 @@ authRoutes.post('/jellyfin', async (req, res, next) => { userType: UserType.EMBY, }); - if ( - user.avatar.startsWith('https://gravatar.com/') && - user.avatar.includes('default=mm&size=200') - ) { - user.avatar = 'https://gravatar.com/avatar/?default=mm&size=200'; - } - break; case MediaServerType.JELLYFIN: settings.main.mediaServerType = MediaServerType.JELLYFIN; @@ -370,13 +363,6 @@ authRoutes.post('/jellyfin', async (req, res, next) => { userType: UserType.JELLYFIN, }); - if ( - user.avatar.startsWith('https://gravatar.com/') && - user.avatar.includes('default=mm&size=200') - ) { - user.avatar = 'https://gravatar.com/avatar/?default=mm&size=200'; - } - break; default: throw new Error('select_server_type'); @@ -431,18 +417,11 @@ authRoutes.post('/jellyfin', async (req, res, next) => { } user.avatar = avatar; } else { - let avatar = gravatarUrl(user.email || account.User.Name, { + const avatar = gravatarUrl(user.email || account.User.Name, { default: 'mm', size: 200, }); - if ( - avatar.startsWith('https://gravatar.com/') && - avatar.includes('default=mm&size=200') - ) { - avatar = 'https://gravatar.com/avatar/?default=mm&size=200'; - } - if (avatar !== user.avatar) { const avatarProxy = new ImageProxy('avatar', ''); avatarProxy.clearCachedImage(user.avatar); @@ -499,12 +478,6 @@ authRoutes.post('/jellyfin', async (req, res, next) => { : UserType.EMBY, }); - if ( - user.avatar.startsWith('https://gravatar.com/') && - user.avatar.includes('default=mm&size=200') - ) { - user.avatar = 'https://gravatar.com/avatar/?default=mm&size=200'; - } //initialize Jellyfin/Emby users with local login const passedExplicitPassword = body.password && body.password.length > 0; if (passedExplicitPassword) { diff --git a/server/routes/avatarproxy.ts b/server/routes/avatarproxy.ts index a9ed802a9..65638df2b 100644 --- a/server/routes/avatarproxy.ts +++ b/server/routes/avatarproxy.ts @@ -7,16 +7,9 @@ const router = Router(); const avatarImageProxy = new ImageProxy('avatar', ''); // Proxy avatar images router.get('/*', async (req, res) => { - let imagePath = req.url.startsWith('/') ? req.url.slice(1) : req.url; + const imagePath = req.url.startsWith('/') ? req.url.slice(1) : req.url; try { - if ( - imagePath.startsWith('https://gravatar.com/') && - imagePath.includes('default=mm&size=200') - ) { - imagePath = 'https://gravatar.com/avatar/?default=mm&size=200'; - } - const imageData = await avatarImageProxy.getImage(imagePath); res.writeHead(200, { diff --git a/server/routes/user/index.ts b/server/routes/user/index.ts index 304560631..f8a0d41a2 100644 --- a/server/routes/user/index.ts +++ b/server/routes/user/index.ts @@ -122,14 +122,7 @@ router.post( } const passedExplicitPassword = body.password && body.password.length > 0; - let avatar = gravatarUrl(email, { default: 'mm', size: 200 }); - - if ( - avatar.startsWith('https://gravatar.com/') && - avatar.includes('default=mm&size=200') - ) { - avatar = 'https://gravatar.com/avatar/?default=mm&size=200'; - } + const avatar = gravatarUrl(email, { default: 'mm', size: 200 }); if ( !passedExplicitPassword && @@ -564,13 +557,6 @@ router.post( : UserType.EMBY, }); - if ( - newUser.avatar.startsWith('https://gravatar.com/') && - newUser.avatar.includes('default=mm&size=200') - ) { - newUser.avatar = 'https://gravatar.com/avatar/?default=mm&size=200'; - } - await userRepository.save(newUser); createdUsers.push(newUser); } diff --git a/src/components/Common/CachedImage/index.tsx b/src/components/Common/CachedImage/index.tsx index 6dfb8ee75..e5c659f83 100644 --- a/src/components/Common/CachedImage/index.tsx +++ b/src/components/Common/CachedImage/index.tsx @@ -16,8 +16,15 @@ const CachedImage = ({ src, ...props }: ImageProps) => { if (typeof imageUrl === 'string' && imageUrl.startsWith('http')) { const parsedUrl = new URL(imageUrl); - if (parsedUrl.host === 'image.tmdb.org' && currentSettings.cacheImages) { - imageUrl = imageUrl.replace('https://image.tmdb.org', '/imageproxy'); + console.log(parsedUrl); + + if (parsedUrl.host === 'image.tmdb.org') { + if (currentSettings.cacheImages) + imageUrl = imageUrl.replace('https://image.tmdb.org', '/imageproxy'); + } + + if (parsedUrl.protocol === 'http:') { + imageUrl = '/avatarproxy/' + imageUrl; } } diff --git a/src/components/IssueDetails/IssueComment/index.tsx b/src/components/IssueDetails/IssueComment/index.tsx index a4d6181b5..ab3f59ad8 100644 --- a/src/components/IssueDetails/IssueComment/index.tsx +++ b/src/components/IssueDetails/IssueComment/index.tsx @@ -89,7 +89,7 @@ const IssueComment = ({ { className="group ml-1 inline-flex h-full items-center xl:ml-1.5" > { > {
{ > { > { > { > = ({
{ > {