2022-01-11 08:54:02 +00:00
|
|
|
import crypto from "crypto";
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2022-11-01 06:30:08 +00:00
|
|
|
import { CAL_URL, WEBAPP_URL } from "@calcom/lib/constants";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { getPlaceholderAvatar } from "@calcom/lib/getPlaceholderAvatar";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
2022-01-11 08:54:02 +00:00
|
|
|
import { defaultAvatarSrc } from "@lib/profile";
|
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
// const username = req.url?.substring(1, req.url.lastIndexOf("/"));
|
|
|
|
const username = req.query.username as string;
|
2022-04-27 11:08:13 +00:00
|
|
|
const teamname = req.query.teamname as string;
|
|
|
|
let identity;
|
2022-11-01 06:30:08 +00:00
|
|
|
let linksToThisRoute = false;
|
2022-04-27 11:08:13 +00:00
|
|
|
if (username) {
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: {
|
|
|
|
username: username,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
avatar: true,
|
|
|
|
email: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
identity = {
|
|
|
|
name: username,
|
|
|
|
email: user?.email,
|
|
|
|
avatar: user?.avatar,
|
|
|
|
};
|
2022-11-01 06:30:08 +00:00
|
|
|
linksToThisRoute =
|
|
|
|
identity.avatar === `${CAL_URL}/${username}/avatar.png` ||
|
|
|
|
identity.avatar === `${WEBAPP_URL}/${username}/avatar.png`;
|
2022-04-27 11:08:13 +00:00
|
|
|
} else if (teamname) {
|
|
|
|
const team = await prisma.team.findUnique({
|
|
|
|
where: {
|
|
|
|
slug: teamname,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
logo: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
identity = {
|
|
|
|
name: teamname,
|
|
|
|
shouldDefaultBeNameBased: true,
|
|
|
|
avatar: team?.logo,
|
|
|
|
};
|
2022-11-01 06:30:08 +00:00
|
|
|
linksToThisRoute =
|
|
|
|
identity.avatar === `${CAL_URL}/team/${teamname}/avatar.png` ||
|
|
|
|
identity.avatar === `${WEBAPP_URL}/team/${teamname}/avatar.png`;
|
2022-04-27 11:08:13 +00:00
|
|
|
}
|
2022-01-11 08:54:02 +00:00
|
|
|
|
|
|
|
const emailMd5 = crypto
|
|
|
|
.createHash("md5")
|
2022-04-27 11:08:13 +00:00
|
|
|
.update((identity?.email as string) || "guest@example.com")
|
2022-01-11 08:54:02 +00:00
|
|
|
.digest("hex");
|
2022-04-27 11:08:13 +00:00
|
|
|
const img = identity?.avatar;
|
2022-11-01 06:30:08 +00:00
|
|
|
// If image isn't set or links to this route itself, use default avatar
|
|
|
|
if (!img || linksToThisRoute) {
|
2022-04-27 11:08:13 +00:00
|
|
|
let defaultSrc = defaultAvatarSrc({ md5: emailMd5 });
|
|
|
|
if (identity?.shouldDefaultBeNameBased) {
|
|
|
|
defaultSrc = getPlaceholderAvatar(null, identity.name);
|
|
|
|
}
|
2022-01-12 13:06:39 +00:00
|
|
|
res.writeHead(302, {
|
2022-04-27 11:08:13 +00:00
|
|
|
Location: defaultSrc,
|
2022-01-12 13:06:39 +00:00
|
|
|
});
|
2022-04-27 11:08:13 +00:00
|
|
|
|
2022-01-12 13:06:39 +00:00
|
|
|
res.end();
|
|
|
|
} else if (!img.includes("data:image")) {
|
|
|
|
res.writeHead(302, {
|
|
|
|
Location: img,
|
|
|
|
});
|
|
|
|
res.end();
|
|
|
|
} else {
|
2022-01-11 08:54:02 +00:00
|
|
|
const decoded = img
|
|
|
|
.toString()
|
|
|
|
.replace("data:image/png;base64,", "")
|
|
|
|
.replace("data:image/jpeg;base64,", "");
|
|
|
|
const imageResp = Buffer.from(decoded, "base64");
|
|
|
|
res.writeHead(200, {
|
|
|
|
"Content-Type": "image/png",
|
|
|
|
"Content-Length": imageResp.length,
|
|
|
|
});
|
|
|
|
res.end(imageResp);
|
|
|
|
}
|
|
|
|
}
|