feat: upload composer images on attach
Attachments used to ride the turn-start command as base64 data URLs: send carried the bytes, the stash re-encoded them into localStorage, and a 10MB image meant a 14M-char string in a single ws frame. Images now upload the moment they are attached. A ws RPC mints a pending-<uuid> id plus a signed, expiring upload URL (mirroring signed asset GETs, so it works against any environment); the browser POSTs the compressed bytes to it with progress and abort; the turn-start command carries id references only. The Normalizer renames pending files to their thread segment at send, resolving by uuid so retries after a partial send are idempotent. Never-sent uploads are deleted on chip removal and swept after 30 days. Breaking: the dataUrl upload variant is deleted with no compatibility path. RN mobile compiles with image attach gated off behind IMAGE_ATTACH_ENABLED and tells the user an update is needed; drafts and stash persist id references (v9/v3 storage, old payloads purged). Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
5304f3e9d4
commit
f695b6f5bf
38 files changed
+2266
-1281
No files matched your search
@@ -7,10 +7,19 @@ import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import {
|
||||
createAttachmentId,
|
||||
createPendingAttachmentId,
|
||||
parseThreadSegmentFromAttachmentId,
|
||||
planAttachmentClaim,
|
||||
resolveAttachmentPathById,
|
||||
sweepStalePendingAttachments,
|
||||
toSafeThreadAttachmentSegment,
|
||||
PENDING_ATTACHMENT_MAX_AGE_MS,
|
||||
} from "./attachmentStore.ts";
|
||||
|
||||
function makeTempAttachmentsDir(): string {
|
||||
return NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"));
|
||||
}
|
||||
|
||||
describe("attachmentStore", () => {
|
||||
it("sanitizes thread ids when creating attachment ids", () => {
|
||||
const attachmentId = createAttachmentId("thread.folder/unsafe space");
|
||||
@@ -45,11 +54,9 @@ describe("attachmentStore", () => {
|
||||
});
|
||||
|
||||
it("resolves attachment path by id using the extension that exists on disk", () => {
|
||||
const attachmentsDir = NodeFS.mkdtempSync(
|
||||
NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"),
|
||||
);
|
||||
const attachmentsDir = makeTempAttachmentsDir();
|
||||
try {
|
||||
const attachmentId = "thread-1-attachment";
|
||||
const attachmentId = "thread-1-00000000-0000-4000-8000-00000000000a";
|
||||
const pngPath = NodePath.join(attachmentsDir, `${attachmentId}.png`);
|
||||
NodeFS.writeFileSync(pngPath, Buffer.from("hello"));
|
||||
|
||||
@@ -63,6 +70,150 @@ describe("attachmentStore", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("resolves a pending id to the renamed thread-scoped file after claim", () => {
|
||||
const attachmentsDir = makeTempAttachmentsDir();
|
||||
try {
|
||||
const uuid = "00000000-0000-4000-8000-00000000000b";
|
||||
const scopedPath = NodePath.join(attachmentsDir, `thread-9-${uuid}.png`);
|
||||
NodeFS.writeFileSync(scopedPath, Buffer.from("pixels"));
|
||||
|
||||
// Old signed asset URLs carry the pending id; they must keep resolving.
|
||||
const resolved = resolveAttachmentPathById({
|
||||
attachmentsDir,
|
||||
attachmentId: `pending-${uuid}`,
|
||||
});
|
||||
expect(resolved).toBe(scopedPath);
|
||||
} finally {
|
||||
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("reserves the pending segment for uploads", () => {
|
||||
expect(toSafeThreadAttachmentSegment("pending")).toBe("pending_thread");
|
||||
const pendingId = createPendingAttachmentId();
|
||||
expect(parseThreadSegmentFromAttachmentId(pendingId)).toBe("pending");
|
||||
});
|
||||
|
||||
describe("planAttachmentClaim", () => {
|
||||
const uuid = "00000000-0000-4000-8000-00000000000c";
|
||||
|
||||
it("plans a rename for a pending attachment", () => {
|
||||
const attachmentsDir = makeTempAttachmentsDir();
|
||||
try {
|
||||
const pendingPath = NodePath.join(attachmentsDir, `pending-${uuid}.webp`);
|
||||
NodeFS.writeFileSync(pendingPath, Buffer.from("pixels"));
|
||||
|
||||
const claimPlan = planAttachmentClaim({
|
||||
attachmentsDir,
|
||||
threadId: "Thread.Foo",
|
||||
attachmentId: `pending-${uuid}`,
|
||||
});
|
||||
expect(claimPlan).toEqual({
|
||||
ok: true,
|
||||
finalId: `thread-foo-${uuid}`,
|
||||
currentPath: pendingPath,
|
||||
finalPath: NodePath.join(attachmentsDir, `thread-foo-${uuid}.webp`),
|
||||
alreadyScoped: false,
|
||||
});
|
||||
} finally {
|
||||
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("is idempotent when a retry references an already-renamed file", () => {
|
||||
const attachmentsDir = makeTempAttachmentsDir();
|
||||
try {
|
||||
const scopedPath = NodePath.join(attachmentsDir, `thread-foo-${uuid}.webp`);
|
||||
NodeFS.writeFileSync(scopedPath, Buffer.from("pixels"));
|
||||
|
||||
const claimPlan = planAttachmentClaim({
|
||||
attachmentsDir,
|
||||
threadId: "Thread.Foo",
|
||||
attachmentId: `pending-${uuid}`,
|
||||
});
|
||||
expect(claimPlan).toMatchObject({ ok: true, alreadyScoped: true });
|
||||
} finally {
|
||||
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("refuses an attachment claimed by another thread", () => {
|
||||
const attachmentsDir = makeTempAttachmentsDir();
|
||||
try {
|
||||
NodeFS.writeFileSync(
|
||||
NodePath.join(attachmentsDir, `other-thread-${uuid}.png`),
|
||||
Buffer.from("pixels"),
|
||||
);
|
||||
|
||||
const claimPlan = planAttachmentClaim({
|
||||
attachmentsDir,
|
||||
threadId: "Thread.Foo",
|
||||
attachmentId: `pending-${uuid}`,
|
||||
});
|
||||
expect(claimPlan).toMatchObject({ ok: false });
|
||||
} finally {
|
||||
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("reports a missing attachment", () => {
|
||||
const attachmentsDir = makeTempAttachmentsDir();
|
||||
try {
|
||||
const claimPlan = planAttachmentClaim({
|
||||
attachmentsDir,
|
||||
threadId: "Thread.Foo",
|
||||
attachmentId: `pending-${uuid}`,
|
||||
});
|
||||
expect(claimPlan).toMatchObject({ ok: false });
|
||||
} finally {
|
||||
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("sweepStalePendingAttachments", () => {
|
||||
it("deletes stale pending and partial files, keeps fresh and scoped ones", () => {
|
||||
const attachmentsDir = makeTempAttachmentsDir();
|
||||
try {
|
||||
const stalePending = NodePath.join(
|
||||
attachmentsDir,
|
||||
"pending-00000000-0000-4000-8000-000000000001.png",
|
||||
);
|
||||
const freshPending = NodePath.join(
|
||||
attachmentsDir,
|
||||
"pending-00000000-0000-4000-8000-000000000002.png",
|
||||
);
|
||||
const scoped = NodePath.join(
|
||||
attachmentsDir,
|
||||
"thread-1-00000000-0000-4000-8000-000000000003.png",
|
||||
);
|
||||
const stalePart = NodePath.join(
|
||||
attachmentsDir,
|
||||
"pending-00000000-0000-4000-8000-000000000004.png.part",
|
||||
);
|
||||
for (const filePath of [stalePending, freshPending, scoped, stalePart]) {
|
||||
NodeFS.writeFileSync(filePath, Buffer.from("x"));
|
||||
}
|
||||
const staleMs = Date.now() - PENDING_ATTACHMENT_MAX_AGE_MS - 60_000;
|
||||
NodeFS.utimesSync(stalePending, staleMs / 1000, staleMs / 1000);
|
||||
NodeFS.utimesSync(scoped, staleMs / 1000, staleMs / 1000);
|
||||
NodeFS.utimesSync(stalePart, staleMs / 1000, staleMs / 1000);
|
||||
|
||||
const swept = sweepStalePendingAttachments({
|
||||
attachmentsDir,
|
||||
nowMs: Date.now(),
|
||||
});
|
||||
expect(swept.deleted).toBe(2);
|
||||
expect(NodeFS.existsSync(stalePending)).toBe(false);
|
||||
expect(NodeFS.existsSync(stalePart)).toBe(false);
|
||||
expect(NodeFS.existsSync(freshPending)).toBe(true);
|
||||
expect(NodeFS.existsSync(scoped)).toBe(true);
|
||||
} finally {
|
||||
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("returns null when no attachment file exists for the id", () => {
|
||||
const attachmentsDir = NodeFS.mkdtempSync(
|
||||
NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"),
|
||||
|
||||
Reference in new issue
Block a user