Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

attachmentStore.ts

110 lines3.4 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeCrypto from "node:crypto";
3import * as NodeFS from "node:fs";
4​
5import type { ChatAttachment } from "@v12/contracts";
6​
7import {
8 normalizeAttachmentRelativePath,
9 resolveAttachmentRelativePath,
10} from "./attachmentPaths.ts";
11import { inferImageExtension, SAFE_IMAGE_FILE_EXTENSIONS } from "./imageMime.ts";
12​
13const ATTACHMENT_FILENAME_EXTENSIONS = [...SAFE_IMAGE_FILE_EXTENSIONS, ".bin"];
14const ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS = 80;
15const ATTACHMENT_ID_THREAD_SEGMENT_PATTERN = "[a-z0-9_]+(?:-[a-z0-9_]+)*";
16const ATTACHMENT_ID_UUID_PATTERN = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
17const ATTACHMENT_ID_PATTERN = new RegExp(
18 `^(${ATTACHMENT_ID_THREAD_SEGMENT_PATTERN})-(${ATTACHMENT_ID_UUID_PATTERN})$`,
19 "i",
20);
21​
22export function toSafeThreadAttachmentSegment(threadId: string): string | null {
23 const segment = threadId
24 .trim()
25 .toLowerCase()
26 .replace(/[^a-z0-9_-]+/gi, "-")
27 .replace(/-+/g, "-")
28 .replace(/^[-_]+|[-_]+$/g, "")
29 .slice(0, ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS)
30 .replace(/[-_]+$/g, "");
31 if (segment.length === 0) {
32 return null;
33 }
34 return segment;
36​
37export function createAttachmentId(threadId: string): string | null {
38 const threadSegment = toSafeThreadAttachmentSegment(threadId);
39 if (!threadSegment) {
40 return null;
41 }
42 return `${threadSegment}-${NodeCrypto.randomUUID()}`;
44​
45export function parseThreadSegmentFromAttachmentId(attachmentId: string): string | null {
46 const normalizedId = normalizeAttachmentRelativePath(attachmentId);
47 if (!normalizedId || normalizedId.includes("/") || normalizedId.includes(".")) {
48 return null;
49 }
50 const match = normalizedId.match(ATTACHMENT_ID_PATTERN);
51 if (!match) {
52 return null;
53 }
54 return match[1]?.toLowerCase() ?? null;
56​
57export function attachmentRelativePath(attachment: ChatAttachment): string {
58 switch (attachment.type) {
59 case "image": {
60 const extension = inferImageExtension({
61 mimeType: attachment.mimeType,
62 fileName: attachment.name,
63 });
64 return `${attachment.id}${extension}`;
65 }
66 }
68​
69export function resolveAttachmentPath(input: {
70 readonly attachmentsDir: string;
71 readonly attachment: ChatAttachment;
72}): string | null {
73 return resolveAttachmentRelativePath({
74 attachmentsDir: input.attachmentsDir,
75 relativePath: attachmentRelativePath(input.attachment),
76 });
78​
79export function resolveAttachmentPathById(input: {
80 readonly attachmentsDir: string;
81 readonly attachmentId: string;
82}): string | null {
83 const normalizedId = normalizeAttachmentRelativePath(input.attachmentId);
84 if (!normalizedId || normalizedId.includes("/") || normalizedId.includes(".")) {
85 return null;
86 }
87 for (const extension of ATTACHMENT_FILENAME_EXTENSIONS) {
88 const maybePath = resolveAttachmentRelativePath({
89 attachmentsDir: input.attachmentsDir,
90 relativePath: `${normalizedId}${extension}`,
91 });
92 if (maybePath && NodeFS.existsSync(maybePath)) {
93 return maybePath;
94 }
95 }
96 return null;
98​
99export function parseAttachmentIdFromRelativePath(relativePath: string): string | null {
100 const normalized = normalizeAttachmentRelativePath(relativePath);
101 if (!normalized || normalized.includes("/")) {
102 return null;
103 }
104 const extensionIndex = normalized.lastIndexOf(".");
105 if (extensionIndex <= 0) {
106 return null;
107 }
108 const id = normalized.slice(0, extensionIndex);
109 return id.length > 0 && !id.includes(".") ? id : null;