2022-01-11 08:54:02 +00:00
|
|
|
import crypto from "crypto";
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2022-04-27 11:08:13 +00:00
|
|
|
import { getPlaceholderAvatar } from "@lib/getPlaceholderAvatar";
|
2022-01-11 08:54:02 +00:00
|
|
|
import prisma from "@lib/prisma";
|
|
|
|
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;
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
} else if (teamname) {
|
|
|
|
const team = await prisma.team.findUnique({
|
|
|
|
where: {
|
|
|
|
slug: teamname,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
logo: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
identity = {
|
|
|
|
name: teamname,
|
|
|
|
shouldDefaultBeNameBased: true,
|
|
|
|
avatar: team?.logo,
|
|
|
|
};
|
|
|
|
}
|
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-01-12 13:06:39 +00:00
|
|
|
if (!img) {
|
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);
|
|
|
|
}
|
|
|
|
}
|