forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix avatar infinite redirect (calcom#5299)
* If due to some reason avatar URL is same as route, avoid infinite redirection by serving default * Fix avatar reverting issue Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: alannnc <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
12 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import crypto from "crypto"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { CAL_URL, WEBAPP_URL } from "@calcom/lib/constants"; | ||
import { getPlaceholderAvatar } from "@calcom/lib/getPlaceholderAvatar"; | ||
import prisma from "@calcom/prisma"; | ||
|
||
|
@@ -11,6 +12,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) | |
const username = req.query.username as string; | ||
const teamname = req.query.teamname as string; | ||
let identity; | ||
let linksToThisRoute = false; | ||
if (username) { | ||
const user = await prisma.user.findUnique({ | ||
where: { | ||
|
@@ -26,6 +28,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) | |
email: user?.email, | ||
avatar: user?.avatar, | ||
}; | ||
linksToThisRoute = | ||
identity.avatar === `${CAL_URL}/${username}/avatar.png` || | ||
identity.avatar === `${WEBAPP_URL}/${username}/avatar.png`; | ||
} else if (teamname) { | ||
const team = await prisma.team.findUnique({ | ||
where: { | ||
|
@@ -40,14 +45,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) | |
shouldDefaultBeNameBased: true, | ||
avatar: team?.logo, | ||
}; | ||
linksToThisRoute = | ||
identity.avatar === `${CAL_URL}/team/${teamname}/avatar.png` || | ||
identity.avatar === `${WEBAPP_URL}/team/${teamname}/avatar.png`; | ||
} | ||
|
||
const emailMd5 = crypto | ||
.createHash("md5") | ||
.update((identity?.email as string) || "[email protected]") | ||
.digest("hex"); | ||
const img = identity?.avatar; | ||
if (!img) { | ||
// If image isn't set or links to this route itself, use default avatar | ||
if (!img || linksToThisRoute) { | ||
let defaultSrc = defaultAvatarSrc({ md5: emailMd5 }); | ||
if (identity?.shouldDefaultBeNameBased) { | ||
defaultSrc = getPlaceholderAvatar(null, identity.name); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters