Skip to content

Commit 5f6c774

Browse files
committed
fix(mobile): preserve visible chosen image attachments
1 parent 6e94bf5 commit 5f6c774

3 files changed

Lines changed: 44 additions & 9 deletions

File tree

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,19 +536,25 @@ describe("composer file attachments", () => {
536536
},
537537
);
538538

539-
it("fits a large PNG with the photo picker renderer and sends the fitted bytes", async () => {
539+
it.each([false, true])("fits a large PNG with oversized output: %s", async (oversized) => {
540540
const header = Buffer.alloc(24);
541541
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy(header);
542542
header.write("IHDR", 12);
543543
header.writeUInt32BE(4000, 16);
544544
header.writeUInt32BE(3000, 20);
545-
mocks.readBase64.mockResolvedValue(header.toString("base64"));
545+
const original = header.toString("base64");
546+
mocks.readBase64.mockResolvedValue(original);
546547
const resize = vi.fn();
547548
const renderAsync = vi.fn(async () => ({
548549
width: 4000,
549550
height: 3000,
550551
release: vi.fn(),
551-
saveAsync: vi.fn(async () => ({ uri: "file:///cache/fitted.png", base64: "YWJj" })),
552+
saveAsync: vi.fn(async () => ({
553+
uri: "file:///cache/fitted.png",
554+
base64: oversized
555+
? Buffer.alloc(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1).toString("base64")
556+
: "YWJj",
557+
})),
552558
}));
553559
mocks.manipulate.mockImplementation(() => {
554560
const context = {
@@ -573,9 +579,11 @@ describe("composer file attachments", () => {
573579
expect.objectContaining({
574580
type: "image",
575581
mimeType: "image/png",
576-
sizeBytes: 3,
577-
dataUrl: "data:image/png;base64,YWJj",
578-
previewUri: "file:///cache/fitted.png",
582+
sizeBytes: oversized ? header.length : 3,
583+
dataUrl: `data:image/png;base64,${oversized ? original : "YWJj"}`,
584+
previewUri: oversized
585+
? `data:image/png;base64,${original}`
586+
: "file:///cache/fitted.png",
579587
}),
580588
],
581589
error: null,

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
2-
import { PROVIDER_SEND_TURN_MAX_ATTACHMENTS } from "@t3tools/contracts";
2+
import {
3+
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
4+
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
5+
} from "@t3tools/contracts";
36

47
const files = new Map<string, { base64: string; deleted: boolean; text?: string }>();
58

@@ -263,4 +266,20 @@ describe("composerStripAttachments", () => {
263266
"vid-1",
264267
]);
265268
});
269+
270+
it("keeps oversized document-picked images visible while sending them as files", async () => {
271+
const { composerStripAttachments, isComposerImageAttachment } =
272+
await import("./composerImages");
273+
const oversizedImage = {
274+
...video,
275+
id: "large-image",
276+
name: "large.png",
277+
mimeType: "image/png",
278+
fileUri: "file:///large.png",
279+
sizeBytes: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1,
280+
};
281+
282+
expect(composerStripAttachments([oversizedImage])).toEqual([oversizedImage]);
283+
expect(isComposerImageAttachment(oversizedImage)).toBe(false);
284+
});
266285
});

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ export function composerStripAttachments(
8282
attachments: ReadonlyArray<DraftComposerAttachment>,
8383
): ReadonlyArray<DraftComposerAttachment> {
8484
return attachments.filter(
85-
(attachment) => isComposerImageAttachment(attachment) || videoMimeType(attachment) !== null,
85+
(attachment) =>
86+
attachment.type === "image" ||
87+
imageMimeType(attachment) !== null ||
88+
videoMimeType(attachment) !== null,
8689
);
8790
}
8891

@@ -314,11 +317,16 @@ export async function pickComposerFiles(input: {
314317
try {
315318
const { File } = await import("expo-file-system");
316319
const original = await new File(attachment.fileUri).base64();
317-
const fitted = await fitComposerImage({
320+
const candidate = await fitComposerImage({
318321
uri: attachment.fileUri,
319322
mimeType,
320323
base64: original,
321324
});
325+
const fitted =
326+
candidate !== null &&
327+
estimateBase64ByteSize(candidate.base64) <= PROVIDER_SEND_TURN_MAX_IMAGE_BYTES
328+
? candidate
329+
: null;
322330
const base64 = fitted?.base64 ?? original;
323331
const sizeBytes = estimateBase64ByteSize(base64);
324332
if (sizeBytes <= 0 || sizeBytes > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) {

0 commit comments

Comments
 (0)