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

attachmentStore.ts

109 lines3.4 KB

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