Skip to content

Commit a760342

Browse files
fix(mobile): delete unused renders of pasted images
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 120e77d commit a760342

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

‎apps/mobile/src/lib/composerImages.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ describe("native pasted image cleanup", () => {
157157
const uri =
158158
"file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/photo.png";
159159
files.set(uri, { base64: oversizedPng(), deleted: false });
160+
files.set("file:///cache/rendered.jpeg", { base64: "", deleted: false });
160161
manipulator.state.sourceSize = { width: 2000, height: 2000 };
161162

162163
const { attachments } = await convertPastedImagesToAttachments({
@@ -166,6 +167,7 @@ describe("native pasted image cleanup", () => {
166167

167168
expect(attachments).toHaveLength(1);
168169
expect(attachments[0]?.sizeBytes).toBeLessThanOrEqual(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES);
170+
expect(files.get("file:///cache/rendered.jpeg")?.deleted).toBe(true);
169171
});
170172

171173
it("reports a paste it cannot read instead of dropping it silently", async () => {

‎apps/mobile/src/lib/composerImages.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,13 +589,16 @@ export async function convertPastedImagesToAttachments(input: {
589589

590590
for (const [index, uri] of input.uris.entries()) {
591591
const ownedTemporaryFile = isOwnedPastedImageUri(uri);
592+
const renders: string[] = [];
593+
let keptRender: string | null = null;
592594
try {
593595
if (index >= Math.max(0, remainingSlots)) {
594596
continue;
595597
}
596598
const mimeType = mimeTypeFromUri(uri);
597599
const pastedBase64 = await new File(uri).base64();
598600
const fitted = await fitComposerImage({ uri, mimeType, base64: pastedBase64 });
601+
if (fitted) renders.push(fitted.uri);
599602
let image = {
600603
base64: fitted?.base64 ?? pastedBase64,
601604
mimeType,
@@ -604,6 +607,7 @@ export async function convertPastedImagesToAttachments(input: {
604607
};
605608
if (estimateBase64ByteSize(image.base64) > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) {
606609
image = await renderComposerJpeg(uri, image.name);
610+
renders.push(image.previewUri);
607611
}
608612
const sizeBytes = estimateBase64ByteSize(image.base64);
609613
if (sizeBytes <= 0 || sizeBytes > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) {
@@ -614,6 +618,7 @@ export async function convertPastedImagesToAttachments(input: {
614618
continue;
615619
}
616620
const dataUrl = `data:${image.mimeType};base64,${image.base64}`;
621+
if (!ownedTemporaryFile) keptRender = image.previewUri;
617622
attachments.push({
618623
id: uuidv4(),
619624
type: "image",
@@ -627,14 +632,15 @@ export async function convertPastedImagesToAttachments(input: {
627632
console.warn("Failed to read pasted image", uri, cause);
628633
error = "Could not read a pasted image.";
629634
} finally {
630-
if (ownedTemporaryFile) {
635+
for (const discarded of [...(ownedTemporaryFile ? [uri] : []), ...renders]) {
636+
if (discarded === keptRender) continue;
631637
try {
632-
const file = new File(uri);
638+
const file = new File(discarded);
633639
if (file.exists) {
634640
file.delete();
635641
}
636642
} catch (error) {
637-
console.warn("Failed to remove temporary pasted image", uri, error);
643+
console.warn("Failed to remove temporary pasted image", discarded, error);
638644
}
639645
}
640646
}

0 commit comments

Comments
 (0)