Skip to content

Commit 0e655ff

Browse files
lfscqk67claudeusrnk1
authored
fix(app): keep pasted image bytes in memory so a deleted temp file can't break the send (#62057)
Co-authored-by: lfscqk67 <lfscqk67@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com>
1 parent 811bfc8 commit 0e655ff

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎packages/app/src/runtime/persistence/drafts.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,50 @@ describe("draft store text externalization", () => {
138138
expect(new Uint8Array(await memory.blobs.get(image.id)!.arrayBuffer())).toEqual(new Uint8Array([1, 2, 3]))
139139
})
140140

141+
test("a pasted file stays readable after its file on disk is deleted", async () => {
142+
// Clipboard tools hand over temp files and delete them on the next copy; reads then fail.
143+
class DiskFile extends File {
144+
gone = false
145+
146+
private check() {
147+
if (this.gone) throw new DOMException("file is gone", "NotReadableError")
148+
}
149+
150+
override arrayBuffer() {
151+
this.check()
152+
153+
return super.arrayBuffer()
154+
}
155+
156+
override text() {
157+
this.check()
158+
159+
return super.text()
160+
}
161+
162+
override stream() {
163+
this.check()
164+
165+
return super.stream()
166+
}
167+
168+
override slice(...args: Parameters<File["slice"]>) {
169+
this.check()
170+
171+
return super.slice(...args)
172+
}
173+
}
174+
175+
const memory = memoryDriver()
176+
const store = createDraftStore(memory.driver)
177+
const file = new DiskFile([new Uint8Array([1, 2, 3])], "1793877187508_d.png", { type: "image/png" })
178+
const image = await store.putBlob(file)
179+
file.gone = true
180+
// happy-dom's FileReader reads a blob's internal buffer, so only a public read can notice the deleted file.
181+
expect(new Uint8Array(await memory.blobs.get(image.id)!.arrayBuffer())).toEqual(new Uint8Array([1, 2, 3]))
182+
expect(await blobDataUrl(image, "image/png")).toBe("data:image/png;base64,AQID")
183+
})
184+
141185
test("the previous document stays visible until missing blobs are restored", async () => {
142186
const memory = memoryDriver()
143187
const store = createDraftStore(memory.driver)

‎packages/app/src/runtime/persistence/drafts.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,15 @@ async function blobID(blob: Blob) {
188188
return id
189189
}
190190

191-
export async function createBlobReference(blob: Blob): Promise<BlobReference> {
191+
// A pasted or dropped File can be backed by a file on disk that the browser reads lazily. Clipboard
192+
// tools delete such temp files once something else is copied, and the preview and the send would
193+
// then fail to read it, so keep the bytes in memory from the start.
194+
async function detach(blob: Blob) {
195+
return blob instanceof File ? new Blob([await blob.arrayBuffer()], { type: blob.type }) : blob
196+
}
197+
198+
export async function createBlobReference(source: Blob): Promise<BlobReference> {
199+
const blob = await detach(source)
192200
const id = await blobID(blob)
193201

194202
return { id, url: blobUrl(id, blob) }
@@ -220,7 +228,8 @@ export function createDraftStore(driver: Driver, options: { grace?: number } = {
220228

221229
storeLoaders.set(storeId, loadBlobUrl)
222230

223-
const putBlob = async (blob: Blob) => {
231+
const putBlob = async (source: Blob) => {
232+
const blob = await detach(source)
224233
const id = await driver.putBlob(blob)
225234

226235
return { id, url: blobUrl(id, blob, grace) }

0 commit comments

Comments
 (0)