Skip to content

Commit efea686

Browse files
authored
feat(web): attach svg files to messages (pingdotgg#21355)
1 parent 140de68 commit efea686

12 files changed

Lines changed: 268 additions & 7 deletions

‎apps/server/src/assets/AssetAccess.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,44 @@ describe("AssetAccess", () => {
979979
}).pipe(Effect.provide(layerTest)),
980980
);
981981

982+
it.effect("serves document attachments inline when a viewer requests it", () =>
983+
Effect.gen(function* () {
984+
const config = yield* ServerConfig.ServerConfig;
985+
const fileSystem = yield* FileSystem.FileSystem;
986+
const path = yield* Path.Path;
987+
yield* fileSystem.makeDirectory(config.attachmentsDir, { recursive: true });
988+
for (const [extension, mimeType] of [
989+
["pdf", "application/pdf"],
990+
["svg", "image/svg+xml"],
991+
] as const) {
992+
const attachmentId = `thread-1-00000000-0000-4000-8000-000000000001-${extension}`;
993+
const attachmentPath = path.join(config.attachmentsDir, `${attachmentId}.${extension}`);
994+
yield* fileSystem.writeFile(attachmentPath, new Uint8Array([1, 2, 3]));
995+
996+
const result = yield* issueAssetUrl({
997+
resource: {
998+
_tag: "attachment",
999+
attachmentId,
1000+
fileName: `report.${extension}`,
1001+
mimeType,
1002+
disposition: "inline",
1003+
},
1004+
});
1005+
const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
1006+
const separatorIndex = suffix.indexOf("/");
1007+
1008+
expect(
1009+
yield* resolveAsset(suffix.slice(0, separatorIndex), suffix.slice(separatorIndex + 1)),
1010+
).toEqual({
1011+
kind: "file",
1012+
path: attachmentPath,
1013+
fileName: `report.${extension}`,
1014+
mimeType,
1015+
});
1016+
}
1017+
}).pipe(Effect.provide(layerTest)),
1018+
);
1019+
9821020
it.effect("keeps inline requests for other attachment types as downloads", () =>
9831021
Effect.gen(function* () {
9841022
const config = yield* ServerConfig.ServerConfig;

‎apps/server/src/assets/AssetAccess.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const PROJECT_FAVICON_ICNS_MIN_SIZE = 96;
8282
const INLINE_VIDEO_MIME_TYPE_PATTERN = /^video\/[\w!#$&^.+-]+$/i;
8383
// Use server-assigned attachment extensions, never client MIME types.
8484
const INLINE_PREVIEW_MIME_TYPES: Record<string, string> = {
85+
svg: "image/svg+xml",
8586
pdf: "application/pdf",
8687
html: "text/html",
8788
htm: "text/html",

‎apps/server/src/assets/AttachmentUpload.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as NodePath from "node:path";
44

55
import * as NodeServices from "@effect/platform-node/NodeServices";
66
import { describe, expect, it } from "@effect/vitest";
7+
import { ChatAttachmentId } from "@t3tools/contracts";
78
import * as Deferred from "effect/Deferred";
89
import * as Effect from "effect/Effect";
910
import * as Fiber from "effect/Fiber";
@@ -14,6 +15,10 @@ import * as Stream from "effect/Stream";
1415
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
1516
import { base64UrlEncode, signPayload } from "../auth/utils.ts";
1617
import * as ServerConfig from "../config.ts";
18+
import {
19+
claimPendingAttachments,
20+
releaseClaimedAttachments,
21+
} from "../orchestration-v2/AttachmentClaims.ts";
1722
import {
1823
ATTACHMENT_UPLOAD_ROUTE_PREFIX,
1924
deletePendingAttachment,
@@ -139,6 +144,47 @@ describe("AttachmentUpload", () => {
139144
}).pipe(Effect.provide(layerTest)),
140145
);
141146

147+
it.effect("stores MIME-identified SVG files with a previewable signed extension", () =>
148+
Effect.gen(function* () {
149+
const config = yield* ServerConfig.ServerConfig;
150+
const bytes = new TextEncoder().encode('<svg xmlns="http://www.w3.org/2000/svg"/>');
151+
for (const name of ["diagram", "diagram.bin"]) {
152+
const issued = yield* issueAttachmentUploadUrl({
153+
type: "file",
154+
name,
155+
mimeType: "image/svg+xml",
156+
sizeBytes: bytes.length,
157+
});
158+
const token = issued.relativeUrl.slice(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/`.length);
159+
const claims = yield* validateAttachmentUploadToken(token);
160+
if (!claims) throw new Error("Expected valid SVG upload claims.");
161+
expect(claims.name).toBe(`${name}.svg`);
162+
expect(issued.attachmentId).toMatch(/-svg$/);
163+
expect(yield* storeAttachmentUpload(claims, bytes)).toEqual({ ok: true });
164+
expect(
165+
NodeFS.readFileSync(NodePath.join(config.attachmentsDir, `${issued.attachmentId}.svg`)),
166+
).toEqual(Buffer.from(bytes));
167+
const claimed = yield* claimPendingAttachments({
168+
threadId: "svg-upload-thread",
169+
attachments: [
170+
{
171+
type: "file",
172+
id: ChatAttachmentId.make(issued.attachmentId),
173+
name,
174+
mimeType: "image/svg+xml",
175+
sizeBytes: bytes.length,
176+
},
177+
],
178+
});
179+
expect(claimed.attachments[0]?.name).toBe(`${name}.svg`);
180+
expect(NodeFS.readFileSync(claimed.claimedPaths[0]!)).toEqual(Buffer.from(bytes));
181+
yield* releaseClaimedAttachments(claimed.claimedPaths);
182+
yield* deletePendingAttachment(issued.attachmentId);
183+
}
184+
expect(NodeFS.readdirSync(config.attachmentsDir)).toEqual([]);
185+
}).pipe(Effect.provide(layerTest)),
186+
);
187+
142188
it.effect("removes partial streamed uploads that exceed their signed size", () =>
143189
Effect.gen(function* () {
144190
const config = yield* ServerConfig.ServerConfig;

‎apps/server/src/assets/AttachmentUpload.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
type AttachmentCreateUploadUrlInput,
44
AttachmentUploadSigningKeyError,
55
} from "@t3tools/contracts";
6+
import { isSvgAttachment } from "@t3tools/shared/image";
67
import * as Clock from "effect/Clock";
78
import * as Crypto from "effect/Crypto";
89
import * as Effect from "effect/Effect";
@@ -93,8 +94,12 @@ export const issueAttachmentUploadUrl = Effect.fn("AttachmentUpload.issueUrl")(f
9394
}
9495

9596
const attachmentType = input.type ?? "image";
97+
const name =
98+
attachmentType === "file" && isSvgAttachment(input) && !/\.svg$/i.test(input.name)
99+
? `${input.name}.svg`
100+
: input.name;
96101
const attachmentId = createPendingAttachmentId(
97-
attachmentType === "file" ? attachmentFileExtension(input.name) : undefined,
102+
attachmentType === "file" ? attachmentFileExtension(name) : undefined,
98103
);
99104
const expiresAt = nowMs + ATTACHMENT_UPLOAD_URL_TTL_MS;
100105
const encodedPayload = base64UrlEncode(
@@ -103,7 +108,7 @@ export const issueAttachmentUploadUrl = Effect.fn("AttachmentUpload.issueUrl")(f
103108
kind: "attachment-upload",
104109
type: attachmentType,
105110
attachmentId,
106-
name: input.name,
111+
name,
107112
mimeType: input.mimeType,
108113
sizeBytes: input.sizeBytes,
109114
expiresAt,

‎apps/server/src/orchestration-v2/AttachmentClaims.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "@t3tools/contracts";
77
import * as Effect from "effect/Effect";
88
import * as Schema from "effect/Schema";
9+
import { isSvgAttachment } from "@t3tools/shared/image";
910

1011
import {
1112
parseThreadSegmentFromAttachmentId,
@@ -106,6 +107,12 @@ export const claimPendingAttachments = Effect.fn("AttachmentClaims.claimPendingA
106107
const normalized: ChatAttachment = {
107108
...attachment,
108109
id: ChatAttachmentId.make(claim.finalId),
110+
name:
111+
attachment.type === "file" &&
112+
isSvgAttachment(attachment) &&
113+
!/\.svg$/i.test(attachment.name)
114+
? `${attachment.name}.svg`
115+
: attachment.name,
109116
mimeType: attachment.mimeType.toLowerCase(),
110117
};
111118
const expectedPath = resolveAttachmentPath({

‎apps/web/src/components/chat/UserMessageContextChips.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isSvgAttachment } from "@t3tools/shared/image";
12
import { useRenderedAppearance } from "../../hooks/useTheme";
23
import { PullRequestGlyph } from "../pullRequest/pullRequestIcons";
34
import { ReadOnlySourcePreview } from "../files/AttachmentFilePreview";
@@ -268,6 +269,7 @@ function UserMessageElementDetails({
268269
}
269270

270271
interface UserMessageContextRenderContext {
272+
environmentId: import("@t3tools/contracts").EnvironmentId;
271273
reference: ChatMarkdownContextReference;
272274
annotationImage: ChatImageAttachment | null;
273275
attachment: ChatImageAttachment | ChatFileAttachment | null;
@@ -378,6 +380,11 @@ const userMessageContextPresentationRegistry = createContextPresentationRegistry
378380
return (
379381
<FileChip
380382
name={record.name}
383+
svgPreview={
384+
isSvgAttachment(attachment)
385+
? { environmentId: context.environmentId, attachmentId: attachment.id }
386+
: undefined
387+
}
381388
size={size}
382389
isVideo={isVideo}
383390
theme={context.resolvedTheme}
@@ -509,13 +516,14 @@ export function UserMessageContextReferenceChip(props: {
509516
onExpandVideo: (file: ChatFileAttachment) => void;
510517
onOpenFile: (file: ChatFileAttachment) => void;
511518
}) {
512-
const { resolvedTheme } = use(TimelineRowCtx);
519+
const { resolvedTheme, activeThreadEnvironmentId } = use(TimelineRowCtx);
513520
const copyMarkdown = formatComposerContextReference({
514521
kind: props.reference.kind,
515522
contextId: props.reference.contextId as ComposerContextId,
516523
label: props.reference.label,
517524
});
518525
return userMessageContextPresentationRegistry.render(props.reference.kind, props.record, {
526+
environmentId: activeThreadEnvironmentId,
519527
reference: props.reference,
520528
annotationImage: props.annotationImage,
521529
attachment: props.attachment,

‎apps/web/src/components/chat/composerAttachmentFiles.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ import {
1818
} from "./composerAttachmentFiles";
1919

2020
describe("composer attachment files", () => {
21+
it("attaches SVG as a file and rejects other unsupported images", () => {
22+
expect(classifyComposerAttachmentFile({ name: "diagram.svg", type: "image/svg+xml" })).toBe(
23+
"file",
24+
);
25+
expect(classifyComposerAttachmentFile({ name: "diagram.SVG", type: "" })).toBe("file");
26+
expect(
27+
classifyComposerAttachmentFile({ name: "diagram.svg", type: "application/octet-stream" }),
28+
).toBe("file");
29+
expect(classifyComposerAttachmentFile({ name: "photo.tiff", type: "image/tiff" })).toBe(
30+
"unsupported-image",
31+
);
32+
expect(classifyComposerAttachmentFile({ name: "report.pdf", type: "application/pdf" })).toBe(
33+
"file",
34+
);
35+
});
36+
2137
it("keeps DNG originals on the file path without changing their bytes", async () => {
2238
const bytes = new Uint8Array([0x49, 0x49, 0x2a, 0x00, 0x08, 0, 0, 0, 0xfe, 0xff, 0x00, 0x80]);
2339
const files = [
@@ -96,6 +112,22 @@ describe("composer attachment files", () => {
96112
).toBe(false);
97113
});
98114

115+
it("claims SVG and unsupported image pastes even when text is present", () => {
116+
const images = [
117+
new File(["svg"], "diagram.svg", { type: "image/svg+xml" }),
118+
new File(["tiff"], "photo.tiff", { type: "image/tiff" }),
119+
];
120+
121+
for (const image of images) {
122+
expect(
123+
shouldHandleComposerAttachmentPaste({
124+
files: [image],
125+
plainText: "Image caption",
126+
}),
127+
).toBe(true);
128+
}
129+
});
130+
99131
it("uses the hard local limit while server config is unknown", () => {
100132
expect(
101133
fileAttachmentStagingLimit({

‎apps/web/src/components/chat/composerAttachmentFiles.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";
1212
import { isHeicImageFile } from "../../lib/imageCompression";
1313
import { isVideoAttachment, videoMimeType } from "../../types";
14+
import { isSvgAttachment } from "@t3tools/shared/image";
1415

1516
type ComposerAttachmentFileKind = "image" | "file" | "unsupported-image";
1617

@@ -72,6 +73,7 @@ function isDngFile(file: Pick<File, "name" | "type">): boolean {
7273
export function composerFileAttachmentMimeType(file: Pick<File, "name" | "type">): string {
7374
// Downstream image detection uses MIME, even for attachments staged as files.
7475
if (isDngFile(file)) return "image/x-adobe-dng";
76+
if (isSvgAttachment({ name: file.name, mimeType: file.type })) return "image/svg+xml";
7577
return (
7678
videoMimeType({ name: file.name, mimeType: file.type }) ??
7779
(file.type || "application/octet-stream")
@@ -81,6 +83,7 @@ export function composerFileAttachmentMimeType(file: Pick<File, "name" | "type">
8183
export function classifyComposerAttachmentFile(
8284
file: Pick<File, "name" | "type">,
8385
): ComposerAttachmentFileKind {
86+
if (isSvgAttachment({ name: file.name, mimeType: file.type })) return "file";
8487
if (isDngFile(file)) return "file";
8588
if (isHeicImageFile(file)) {
8689
return "image";
@@ -164,7 +167,11 @@ export function shouldHandleComposerAttachmentPaste(input: {
164167
input.files.some((file) => {
165168
if (isDngFile(file)) return true;
166169
const classification = classifyComposerAttachmentFile(file);
167-
return classification === "image" || classification === "unsupported-image";
170+
return (
171+
classification === "image" ||
172+
classification === "unsupported-image" ||
173+
isSvgAttachment({ name: file.name, mimeType: file.type })
174+
);
168175
})
169176
) {
170177
return true;

‎apps/web/src/components/composerContextPresentation.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ChatMarkdown from "./ChatMarkdown";
2+
import { isSvgAttachment } from "@t3tools/shared/image";
23
import { ReadOnlySourcePreview } from "./files/AttachmentFilePreview";
34
import type { PreviewAnnotationPayload, ThreadContextRecord } from "@t3tools/contracts";
45
import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
@@ -222,6 +223,15 @@ function FileContextChip(props: {
222223
return (
223224
<FileChip
224225
name={props.record.name}
226+
svgPreview={
227+
isSvgAttachment(props.record)
228+
? {
229+
file: props.record.file,
230+
environmentId: props.record.uploadEnvironmentId,
231+
attachmentId: props.record.uploadedAttachmentId,
232+
}
233+
: undefined
234+
}
225235
size={size}
226236
isVideo={isVideo}
227237
theme={resolvedTheme}

0 commit comments

Comments
 (0)