From ac6275b9067c70997995b7105b7220770391171e Mon Sep 17 00:00:00 2001 From: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com> Date: Wed, 12 Jan 2022 18:36:39 +0530 Subject: [PATCH] hotfix for images hosted elsewhere and link stored in DB (#1480) * hotfix for images hosted elsewhere and link stored in DB * improved if else Co-authored-by: Peer Richelsen --- pages/api/user/avatar.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pages/api/user/avatar.ts b/pages/api/user/avatar.ts index 550723fc..1c248413 100644 --- a/pages/api/user/avatar.ts +++ b/pages/api/user/avatar.ts @@ -22,7 +22,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) .update(user?.email as string) .digest("hex"); const img = user?.avatar; - if (img) { + if (!img) { + res.writeHead(302, { + Location: defaultAvatarSrc({ md5: emailMd5 }), + }); + res.end(); + } else if (!img.includes("data:image")) { + res.writeHead(302, { + Location: img, + }); + res.end(); + } else { const decoded = img .toString() .replace("data:image/png;base64,", "") @@ -33,10 +43,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) "Content-Length": imageResp.length, }); res.end(imageResp); - } else { - res.writeHead(302, { - Location: defaultAvatarSrc({ md5: emailMd5 }), - }); - res.end(); } }