Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import NodePath from "node:path";
3
4export const ATTACHMENTS_ROUTE_PREFIX = "/attachments";
5
6export function normalizeAttachmentRelativePath(rawRelativePath: string): string | null {
7 const normalized = NodePath.normalize(rawRelativePath).replace(/^[/\\]+/, "");
8 if (normalized.length === 0 || normalized.startsWith("..") || normalized.includes("\0")) {
9 return null;
10 }
11 return normalized.replace(/\\/g, "/");
12}
13
14export function resolveAttachmentRelativePath(input: {
15 readonly attachmentsDir: string;
16 readonly relativePath: string;
17}): string | null {
18 const normalizedRelativePath = normalizeAttachmentRelativePath(input.relativePath);
19 if (!normalizedRelativePath) {
20 return null;
21 }
22
23 const attachmentsRoot = NodePath.resolve(input.attachmentsDir);
24 const filePath = NodePath.resolve(NodePath.join(attachmentsRoot, normalizedRelativePath));
25 if (!filePath.startsWith(`${attachmentsRoot}${NodePath.sep}`)) {
26 return null;
27 }
28 return filePath;
29}