Initial implementation, getAppLogos() (#8405)
* Initial implementation, getAppLogos() * Handle localhost:3000 correctly * New URL wasn't working with all hostnames * Only get the first of the subdomainParts, join not needed * rename url -> hostname * getAppLogos -> getTeamLogos
This commit is contained in:
1 parent
c38dd2d072
commit
e1403bfbeb
1 file changed
+53
-79
+53
-79
@@ -1,106 +1,80 @@
|
|||||||
import fs from "fs";
|
|
||||||
import mime from "mime-types";
|
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
import path from "path";
|
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { LOGO, LOGO_ICON } from "@calcom/lib/constants";
|
import { IS_SELF_HOSTED, LOGO, LOGO_ICON, WEBAPP_URL } from "@calcom/lib/constants";
|
||||||
import logger from "@calcom/lib/logger";
|
|
||||||
|
|
||||||
const log = logger.getChildLogger({ prefix: [`[api/logo]`] });
|
|
||||||
|
|
||||||
function removePort(url: string) {
|
function removePort(url: string) {
|
||||||
return url.replace(/:\d+$/, "");
|
return url.replace(/:\d+$/, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractSubdomainAndDomain(url: string): [string, string] | null {
|
function extractSubdomainAndDomain(hostname: string) {
|
||||||
try {
|
const hostParts = removePort(hostname).split(".");
|
||||||
const parsedUrl = new URL(url);
|
|
||||||
const hostParts = parsedUrl.href.split(".");
|
const subdomainParts = hostParts.slice(0, hostParts.length - 2);
|
||||||
if (hostParts.length > 2) {
|
const domain = hostParts.slice(hostParts.length - 2).join(".");
|
||||||
const subdomain = hostParts.slice(0, hostParts.length - 2).join(".");
|
|
||||||
const domain = hostParts.slice(hostParts.length - 2).join(".");
|
return [subdomainParts[0], domain];
|
||||||
return [subdomain, removePort(domain)];
|
|
||||||
} else if (hostParts.length === 2) {
|
|
||||||
const subdomain = "";
|
|
||||||
const domain = hostParts.join(".");
|
|
||||||
return [subdomain, removePort(domain)];
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const logoApiSchema = z.object({
|
const logoApiSchema = z.object({
|
||||||
icon: z.coerce.boolean().optional(),
|
icon: z.coerce.boolean().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
function handleDefaultLogo(
|
const SYSTEM_SUBDOMAINS = ["console", "app", "www"];
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse,
|
|
||||||
parsedQuery?: z.infer<typeof logoApiSchema>
|
|
||||||
) {
|
|
||||||
let fileNameParts = LOGO.split(".");
|
|
||||||
|
|
||||||
if (parsedQuery?.icon) {
|
async function getTeamLogos(subdomain: string) {
|
||||||
fileNameParts = LOGO_ICON.split(".");
|
if (
|
||||||
|
// if not cal.com
|
||||||
|
IS_SELF_HOSTED ||
|
||||||
|
// missing subdomain (empty string)
|
||||||
|
!subdomain ||
|
||||||
|
// in SYSTEM_SUBDOMAINS list
|
||||||
|
SYSTEM_SUBDOMAINS.includes(subdomain)
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
appLogo: `${WEBAPP_URL}${LOGO}`,
|
||||||
|
appIconLogo: `${WEBAPP_URL}${LOGO_ICON}`,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
// load from DB
|
||||||
const { [fileNameParts.length - 1]: fileExtension } = fileNameParts;
|
const { default: prisma } = await import("@calcom/prisma");
|
||||||
const STATIC_PATH = path.join(process.cwd(), "public" + LOGO);
|
const team = await prisma.team.findUniqueOrThrow({
|
||||||
const imageBuffer = fs.readFileSync(STATIC_PATH);
|
where: {
|
||||||
const mimeType = mime.lookup(fileExtension);
|
slug: subdomain,
|
||||||
if (mimeType) res.setHeader("Content-Type", mimeType);
|
},
|
||||||
res.setHeader("Cache-Control", "s-maxage=86400");
|
select: {
|
||||||
res.send(imageBuffer);
|
appLogo: true,
|
||||||
|
appIconLogo: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// try to use team logos, otherwise default to LOGO/LOGO_ICON regardless
|
||||||
|
return {
|
||||||
|
appLogo: team.appLogo || `${WEBAPP_URL}${LOGO}`,
|
||||||
|
appIconLogo: team.appIconLogo || `${WEBAPP_URL}${LOGO_ICON}`,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This API endpoint is used to serve the logo associated with a team if no logo is found we serve our default logo
|
* This API endpoint is used to serve the logo associated with a team if no logo is found we serve our default logo
|
||||||
*/
|
*/
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
try {
|
const { query } = req;
|
||||||
const { query } = req;
|
const parsedQuery = logoApiSchema.parse(query);
|
||||||
const parsedQuery = logoApiSchema.parse(query);
|
|
||||||
|
|
||||||
const hostname = req?.headers["host"];
|
const hostname = req?.headers["host"];
|
||||||
if (!hostname) throw new Error("No hostname");
|
if (!hostname) throw new Error("No hostname");
|
||||||
const domains = extractSubdomainAndDomain(hostname);
|
const domains = extractSubdomainAndDomain(hostname);
|
||||||
if (!domains) throw new Error("No domains");
|
if (!domains) throw new Error("No domains");
|
||||||
const [subdomain, domain] = domains;
|
|
||||||
// Only supported on cal.com and cal.dev
|
|
||||||
if (["cal.com", "cal.dev"].includes(domain)) return handleDefaultLogo(req, res, parsedQuery);
|
|
||||||
// Skip if no subdomain
|
|
||||||
if (!subdomain) throw new Error("No subdomain");
|
|
||||||
// Omit system subdomains
|
|
||||||
if (["console", "app", "www"].includes(subdomain)) return handleDefaultLogo(req, res, parsedQuery);
|
|
||||||
|
|
||||||
const { default: prisma } = await import("@calcom/prisma");
|
const [subdomain] = domains;
|
||||||
|
const { appLogo, appIconLogo } = await getTeamLogos(subdomain);
|
||||||
|
|
||||||
const team = await prisma.team.findUnique({
|
const filteredLogo = parsedQuery?.icon ? appIconLogo : appLogo;
|
||||||
where: {
|
|
||||||
slug: subdomain,
|
|
||||||
},
|
|
||||||
select: {
|
|
||||||
appLogo: true,
|
|
||||||
appIconLogo: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const filteredLogo = parsedQuery?.icon ? team?.appIconLogo : team?.appLogo;
|
const response = await fetch(filteredLogo);
|
||||||
|
const arrayBuffer = await response.arrayBuffer();
|
||||||
if (!filteredLogo) throw new Error("No team appLogo");
|
const buffer = Buffer.from(arrayBuffer);
|
||||||
|
res.setHeader("Content-Type", response.headers.get("content-type") as string);
|
||||||
const response = await fetch(filteredLogo);
|
res.setHeader("Cache-Control", "s-maxage=86400");
|
||||||
const arrayBuffer = await response.arrayBuffer();
|
res.send(buffer);
|
||||||
const buffer = Buffer.from(arrayBuffer);
|
|
||||||
res.setHeader("Content-Type", response.headers.get("content-type") as string);
|
|
||||||
res.setHeader("Cache-Control", "s-maxage=86400");
|
|
||||||
res.send(buffer);
|
|
||||||
} catch (e) {
|
|
||||||
if (e instanceof Error) log.debug(e.message);
|
|
||||||
handleDefaultLogo(req, res);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user