Skip to content

Commit

Permalink
Env based branding log, #69
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiancook committed Aug 28, 2023
1 parent 8071320 commit a0d3c80
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"human-readable-ids": "^1.0.4",
"ioredis": "^5.3.2",
"json-schema-to-ts": "^2.8.0",
"jsonwebtoken": "^9.0.0",
"jsonwebtoken": "^9.0.1",
"luxon": "^3.3.0",
"nanoid": "^4.0.2",
"nodemailer": "^6.9.3",
Expand Down
3 changes: 3 additions & 0 deletions public/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions src/authentication/token-signature.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// See https://github.com/upstash/sdk-qstash-ts/blob/main/pkg/receiver.ts
// This is a version that doesn't rely on deno
import {JwtPayload, verify} from "jsonwebtoken";
import type {JwtPayload} from "jsonwebtoken";
import {ok} from "../is";
import {subtle} from "crypto";
import JsonWebToken from "jsonwebtoken";

export interface VerifyWithKeyOptions {
token: string;
Expand All @@ -27,7 +28,7 @@ export async function verifyTokenWithKeys(req: VerifyWithKeyOptions) {

export async function verifyTokenWithKey(req: VerifyWithKeyOptions, key: string) {
try {
const verified = verify(req.token, key, {
const verified = JsonWebToken.verify(req.token, key, {
issuer: req.issuer,
subject: req.subject
});
Expand Down
86 changes: 86 additions & 0 deletions src/branding/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {DEFAULT_BRANDING_LOGO, getConfig, PUBLIC_PATH, BRANDING_LOGO} from "../config";
import {root} from "../package";
import {join} from "node:path";
import {stat} from "fs/promises";
import {ok} from "../is";
import {ROOT_PUBLIC_PATH} from "../view";
import {readFile} from "node:fs/promises";
import mime from "mime";


function getPublicRoots() {
const roots = [
ROOT_PUBLIC_PATH,
];
const config = getConfig();
if (root !== config.root && config.root) {
roots.unshift(
join(config.root, PUBLIC_PATH || "public")
);
}
return roots;
}

export async function getBrandingLogoBufferAndType() {
const logo = await getBrandingLogoFile();
const buffer = await readFile(logo.path);
// Could use blob here, but then we need to go from blob to buffer elsewhere
return {
buffer,
type: mime.getType(logo.path)
}
}

export async function getBrandingLogoFile() {

const roots = getPublicRoots();

if (BRANDING_LOGO) {
return getBrandingLogo(BRANDING_LOGO);
}
return getPublicLogo();

async function getBrandingLogo(logo: string) {
const found = await getFirstFile(roots, [
logo
]);
ok(found, "Expected BRANDING_LOGO at PUBLIC_PATH");
return found;
}

async function getPublicLogo() {
const found = await getFirstFile(roots, [
DEFAULT_BRANDING_LOGO,
"logo.svg",
"logo.png"
]);
ok(found, "Expected to find logo at root or BRANDING_LOGO");
return found;
}

async function getFirstFile(paths: string[], names: string[]) {
for (const root of paths) {
for (const name of names) {
const path = join(root, name);
if (await isFile(path)) {
return {
root,
path,
name,
};
}
}
}
return undefined;
}

}

async function isFile(path: string) {
try {
const fileStat = await stat(path);
return fileStat.isFile();
} catch {
return false;
}
}
4 changes: 3 additions & 1 deletion src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ export const {
ALLOW_ANONYMOUS_VIEWS,
ENABLE_CACHE,
DEFAULT_TIMEZONE = "Pacific/Auckland",
DEFAULT_BRANDING_LOGO,
BRANDING_LOGO,
DEFAULT_BRANDING_LOGO = "logo.svg",
DEFAULT_BRANDING_PRIMARY,
DEFAULT_BRANDING_SECONDARY,
TESTING,
PUBLIC_PATH,
MEDIA_PARTNER_PREFIX_TEMPLATE,
MEDIA_PARTNER_PREFIX,
MEDIA_USER_PREFIX_TEMPLATE,
Expand Down
5 changes: 3 additions & 2 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {name, version} from "../package";
import {name, root, version} from "../package";
import {createContext} from "../hooks/context";
import {Config} from "./types";

Expand All @@ -8,7 +8,8 @@ export { Config } from "./types";

const ConfigContext = createContext<Config>({
name,
version
version,
root
});

export function getConfig(overrides?: Partial<Config>): Config {
Expand Down
1 change: 1 addition & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export interface Config extends
VirtualEventConfig {
name: string;
version: string;
root: string;
}
15 changes: 15 additions & 0 deletions src/listen/file/get-branding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {FastifyInstance} from "fastify";
import {getBrandingLogoBufferAndType} from "../../branding";

export async function brandingRoutes(fastify: FastifyInstance) {
try {
fastify.get("/branding/logo", {
async handler(request, response) {
const { buffer, type } = await getBrandingLogoBufferAndType();
response.header("Content-Type", type);
response.send(buffer);
}
})
} catch {}

}
2 changes: 2 additions & 0 deletions src/listen/file/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { FastifyInstance } from "fastify";
import { getFileImageWatermarkRoutes } from "./get-file-image-watermark";
import {brandingRoutes} from "./get-branding";

export async function fileRoutes(fastify: FastifyInstance) {
async function routes(fastify: FastifyInstance) {
fastify.register(getFileImageWatermarkRoutes);
fastify.register(brandingRoutes);
}

fastify.register(routes, {
Expand Down
5 changes: 3 additions & 2 deletions src/view/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const { pathname } = new URL(import.meta.url);
const DIRECTORY = dirname(pathname);
export const REACT_CLIENT_DIRECTORY = join(DIRECTORY, "../react/client");

export const ROOT_PUBLIC_PATH = join(root, "./public")

export async function fileRoutes(fastify: FastifyInstance) {
await fastify.register(etag);
await fastify.addHook("onRequest", (request, response, done) => {
Expand All @@ -47,9 +49,8 @@ export async function fileRoutes(fastify: FastifyInstance) {
decorateReply: !fastify.hasReplyDecorator("sendFile"),
prefix: `/${name}/client`,
});
const publicPath = join(root, "./public");
await fastify.register(files, {
root: publicPath,
root: ROOT_PUBLIC_PATH,
decorateReply: false,
prefix: `/${name}/public`,
});
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6949,7 +6949,7 @@ jsonwebtoken@^8.5.1:
ms "^2.1.1"
semver "^5.6.0"

jsonwebtoken@^9.0.0:
jsonwebtoken@^9.0.1:
version "9.0.1"
resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-9.0.1.tgz#81d8c901c112c24e497a55daf6b2be1225b40145"
integrity sha512-K8wx7eJ5TPvEjuiVSkv167EVboBDv9PZdDoF7BgeQnBLVvZWW9clr2PsQHVJDTKaEIH5JBIwHujGcHp7GgI2eg==
Expand Down

0 comments on commit a0d3c80

Please sign in to comment.