Skip to content

Commit

Permalink
feat: proxy avatars for Accounts_AvatarExternalProviderUrl (#32824)
Browse files Browse the repository at this point in the history
Co-authored-by: Tasso <[email protected]>
Co-authored-by: Guilherme Gazzo <[email protected]>
  • Loading branch information
3 people authored Aug 21, 2024
1 parent a134176 commit 1041e8c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 1 addition & 5 deletions apps/meteor/client/providers/AvatarUrlProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ type AvatarUrlProviderProps = {

const AvatarUrlProvider = ({ children }: AvatarUrlProviderProps) => {
const cdnAvatarUrl = String(useSetting('CDN_PREFIX') || '');
const externalProviderUrl = String(useSetting('Accounts_AvatarExternalProviderUrl') || '');
const contextValue = useMemo(
() => ({
getUserPathAvatar: ((): ((uid: string, etag?: string) => string) => {
if (externalProviderUrl) {
return (uid: string): string => externalProviderUrl.trim().replace(/\/+$/, '').replace('{username}', uid);
}
if (cdnAvatarUrl) {
return (uid: string, etag?: string): string => `${cdnAvatarUrl}/avatar/${uid}${etag ? `?etag=${etag}` : ''}`;
}
Expand All @@ -26,7 +22,7 @@ const AvatarUrlProvider = ({ children }: AvatarUrlProviderProps) => {
getRoomPathAvatar: ({ type, ...room }: any): string =>
roomCoordinator.getRoomDirectives(type || room.t).getAvatarPath({ username: room._id, ...room }) || '',
}),
[externalProviderUrl, cdnAvatarUrl],
[cdnAvatarUrl],
);

return <AvatarUrlContext.Provider children={children} value={contextValue} />;
Expand Down
8 changes: 8 additions & 0 deletions apps/meteor/server/routes/avatar/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Avatars, Users } from '@rocket.chat/models';
import { serverFetch as fetch } from '@rocket.chat/server-fetch';

import { FileUpload } from '../../../app/file-upload/server';
import { settings } from '../../../app/settings/server';
Expand Down Expand Up @@ -51,6 +52,13 @@ export const userAvatar = async function (req, res) {
return FileUpload.get(file, req, res);
}

if (settings.get('Accounts_AvatarExternalProviderUrl')) {
const response = await fetch(settings.get('Accounts_AvatarExternalProviderUrl').replace('{username}', requestUsername));
response.headers.forEach((value, key) => res.setHeader(key, value));
response.body.pipe(res);
return;
}

// if still using "letters fallback"
if (!wasFallbackModified(reqModifiedHeader, res)) {
res.writeHead(304);
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/src/locales/de.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -5530,4 +5530,4 @@
"Enterprise": "Unternehmen",
"UpgradeToGetMore_engagement-dashboard_Title": "Analytics",
"UpgradeToGetMore_auditing_Title": "Nachrichtenüberprüfung"
}
}

0 comments on commit 1041e8c

Please sign in to comment.