Skip to content

Commit eaa7b1c

Browse files
committed
fix(web): allow full-resolution HEIC photo attachments
1 parent e451843 commit eaa7b1c

8 files changed

Lines changed: 16 additions & 977 deletions

File tree

‎apps/web/THIRD_PARTY_NOTICES.md‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,3 @@ Copyright (c) 2016 Roberto Huertas
99

1010
Licensed under the MIT License. The full license text is available in the
1111
upstream repository: <https://github.com/vscode-icons/vscode-icons/blob/master/LICENSE>.
12-
13-
## heic-to, libheif, and libde265
14-
15-
The web and desktop apps use `heic-to` to convert HEIC and HEIF photos to JPEG.
16-
The decoder includes `libheif` and `libde265`. These libraries are distributed
17-
under the GNU Lesser General Public License, version 3 or later.
18-
19-
The deployed application includes the decoder notice at `/licenses/NOTICE.txt`
20-
and copies of the required licenses at `/licenses/GPL-3.0.txt` and
21-
`/licenses/LGPL-3.0.txt`.
22-
23-
Source code and build instructions are available from the upstream projects:
24-
25-
- <https://github.com/hoppergee/heic-to>
26-
- <https://github.com/strukturag/libheif>
27-
- <https://github.com/strukturag/libde265>
28-
29-
The T3 Code source and build instructions are available at
30-
<https://github.com/pingdotgg/t3code>.

‎apps/web/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"culori": "^4.0.2",
3636
"effect": "catalog:",
3737
"heic-to": "^1.5.2",
38-
"image-size": "^2.0.2",
3938
"jose": "catalog:",
4039
"jsonc-parser": "3.3.1",
4140
"jszip": "3.10.1",

‎apps/web/public/licenses/GPL-3.0.txt‎

Lines changed: 0 additions & 674 deletions
This file was deleted.

‎apps/web/public/licenses/LGPL-3.0.txt‎

Lines changed: 0 additions & 165 deletions
This file was deleted.

‎apps/web/public/licenses/NOTICE.txt‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎apps/web/src/lib/imageCompression.test.ts‎

Lines changed: 13 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
compressImageToByteLimit,
66
isHeicImageFile,
77
MAX_COMPRESSIBLE_SOURCE_BYTES,
8-
MAX_HEIC_DECODE_PIXELS,
98
MAX_STASH_IMAGE_DATA_URL_CHARS,
109
prepareImageForAttachment,
1110
} from "./imageCompression";
@@ -33,45 +32,6 @@ function makeFile(sizeBytes: number, type = "image/png"): File {
3332
return new File([new Uint8Array(sizeBytes).fill(7)], "shot.png", { type });
3433
}
3534

36-
function makeHeicFile(options?: {
37-
name?: string;
38-
type?: string;
39-
width?: number;
40-
height?: number;
41-
lastModified?: number;
42-
}): File {
43-
const encoder = new TextEncoder();
44-
const makeBox = (name: string, ...contents: Uint8Array[]) => {
45-
const bytes = new Uint8Array(8 + contents.reduce((size, content) => size + content.length, 0));
46-
new DataView(bytes.buffer).setUint32(0, bytes.length);
47-
bytes.set(encoder.encode(name), 4);
48-
let offset = 8;
49-
for (const content of contents) {
50-
bytes.set(content, offset);
51-
offset += content.length;
52-
}
53-
return bytes;
54-
};
55-
56-
const dimensions = new Uint8Array(12);
57-
const dimensionView = new DataView(dimensions.buffer);
58-
dimensionView.setUint32(4, options?.width ?? 4000);
59-
dimensionView.setUint32(8, options?.height ?? 3000);
60-
const imageProperties = makeBox("iprp", makeBox("ipco", makeBox("ispe", dimensions)));
61-
62-
return new File(
63-
[
64-
makeBox("ftyp", encoder.encode("heic"), new Uint8Array(4)),
65-
makeBox("meta", new Uint8Array(4), imageProperties),
66-
],
67-
options?.name ?? "photo.heic",
68-
{
69-
type: options?.type ?? "image/heic",
70-
...(options?.lastModified !== undefined ? { lastModified: options.lastModified } : {}),
71-
},
72-
);
73-
}
74-
7535
/**
7636
* Installs a fake bitmap + canvas whose encoded size follows `sizeForQuality`.
7737
* `supportsWebp: false` makes `convertToBlob` hand back a differently-typed
@@ -315,9 +275,7 @@ describe("HEIC attachment preparation", () => {
315275
});
316276

317277
it("converts a HEIC photo with a missing MIME type into a named JPEG", async () => {
318-
const original = makeHeicFile({
319-
name: "IMG_1234.HEIC",
320-
type: "",
278+
const original = new File([new Uint8Array([1, 2, 3])], "IMG_1234.HEIC", {
321279
lastModified: 123,
322280
});
323281
mocks.heicTo.mockResolvedValueOnce(
@@ -339,8 +297,7 @@ describe("HEIC attachment preparation", () => {
339297
});
340298

341299
it("keeps oversized converted photos in JPEG format while shrinking them", async () => {
342-
const original = makeHeicFile({
343-
name: "photo.heif",
300+
const original = new File([new Uint8Array([1, 2, 3])], "photo.heif", {
344301
type: "image/heif",
345302
});
346303
mocks.heicTo.mockResolvedValueOnce(
@@ -356,44 +313,27 @@ describe("HEIC attachment preparation", () => {
356313
expect(fillRect).toHaveBeenCalled();
357314
});
358315

359-
it("compresses converted JPEGs that grow beyond the original source size limit", async () => {
360-
const original = makeHeicFile();
316+
it("compresses JPEG intermediates above the source safety ceiling", async () => {
317+
const original = new File([new Uint8Array([1, 2, 3])], "large.heic", {
318+
type: "image/heic",
319+
});
361320
mocks.heicTo.mockResolvedValueOnce(
362-
new Blob([new Uint8Array(MAX_COMPRESSIBLE_SOURCE_BYTES + 1)], { type: "image/jpeg" }),
321+
new Blob([new Uint8Array(MAX_COMPRESSIBLE_SOURCE_BYTES + 1)], {
322+
type: "image/jpeg",
323+
}),
363324
);
364-
stubCanvasPipeline(() => 200_000);
325+
const { close } = stubCanvasPipeline(() => 200_000);
365326

366327
const result = await prepareImageForAttachment(original, 1_000_000);
367328

329+
expect(result.ok && result.file.name).toBe("large.jpg");
368330
expect(result.ok && result.file.type).toBe("image/jpeg");
369331
expect(result.ok && result.file.size).toBeLessThanOrEqual(1_000_000);
370-
});
371-
372-
it("rejects oversized HEIC dimensions before loading the decoder", async () => {
373-
const original = makeHeicFile({ width: MAX_HEIC_DECODE_PIXELS, height: 2 });
374-
375-
expect(await prepareImageForAttachment(original, 1024)).toEqual({
376-
ok: false,
377-
reason: "too-large",
378-
});
379-
expect(mocks.heicTo).not.toHaveBeenCalled();
380-
});
381-
382-
it("rejects invalid HEIC metadata before loading the decoder", async () => {
383-
const original = new File([new Uint8Array([1, 2, 3])], "broken.heic", {
384-
type: "image/heic",
385-
});
386-
387-
expect(await prepareImageForAttachment(original, 1024)).toEqual({
388-
ok: false,
389-
reason: "unreadable",
390-
});
391-
expect(mocks.heicTo).not.toHaveBeenCalled();
332+
expect(close).toHaveBeenCalled();
392333
});
393334

394335
it("reports unreadable when HEIC decoding fails", async () => {
395-
const original = makeHeicFile({
396-
name: "broken.heic",
336+
const original = new File([new Uint8Array([1, 2, 3])], "broken.heic", {
397337
type: "image/heic",
398338
});
399339
mocks.heicTo.mockRejectedValueOnce(new Error("Invalid HEIC image"));

‎apps/web/src/lib/imageCompression.ts‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ export const MAX_STASH_IMAGE_DATA_URL_CHARS = 1_300_000;
2525
* ImageBitmap can OOM the tab — beyond this we refuse rather than risk it.
2626
*/
2727
export const MAX_COMPRESSIBLE_SOURCE_BYTES = 50 * 1024 * 1024;
28-
/** Bounds the full-resolution RGBA buffers allocated by the HEIC decoder. */
29-
export const MAX_HEIC_DECODE_PIXELS = 16_000_000;
3028
/**
3129
* Quality ladder tried in order until the encoded image fits the budget.
3230
* The floor stays high enough to avoid visible blocking on UI screenshots;
@@ -324,7 +322,9 @@ export async function compressImageForStash(
324322
* `File` (WebP or JPEG). Files already within the limit pass through
325323
* untouched, preserving their exact bytes and format. Sources above
326324
* `MAX_COMPRESSIBLE_SOURCE_BYTES` are refused outright — decoding them is
327-
* the risk, so no amount of output budget makes them safe.
325+
* the risk, so no amount of output budget makes them safe. An internally
326+
* converted image can provide its original source size when the intermediate
327+
* format expands beyond that ceiling.
328328
*/
329329
export async function compressImageToByteLimit(
330330
file: File,
@@ -374,21 +374,6 @@ export async function prepareImageForAttachment(
374374

375375
let converted: Blob;
376376
try {
377-
const { HEIF } = await import("image-size/types/heif");
378-
const sourceBytes = new Uint8Array(await file.arrayBuffer());
379-
if (!HEIF.validate(sourceBytes)) {
380-
return { ok: false, reason: "unreadable" };
381-
}
382-
const image = HEIF.calculate(sourceBytes);
383-
const images = image.images ?? [image];
384-
if (
385-
images.some(
386-
({ width, height }) => width <= 0 || height <= 0 || width > MAX_HEIC_DECODE_PIXELS / height,
387-
)
388-
) {
389-
return { ok: false, reason: "too-large" };
390-
}
391-
392377
const { heicTo } = await import("heic-to/csp");
393378
converted = await heicTo({ blob: file, type: "image/jpeg", quality: QUALITY_STEPS[0] });
394379
} catch {

0 commit comments

Comments
 (0)