Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

feat(server): report image dimensions with signed asset URLs (#226)

Leo69dc0144af4c
diff --git a/apps/server/src/assets/AssetAccess.test.ts b/apps/server/src/assets/AssetAccess.test.ts
index aa47a7823..62fb151b5 100644
--- a/apps/server/src/assets/AssetAccess.test.ts
+++ b/apps/server/src/assets/AssetAccess.test.ts
@@ -72,6 +72,34 @@ describe("AssetAccess", () => {
}).pipe(Effect.provide(testLayer)),
);
+ it.effect("reports pixel dimensions from an image header and nothing for other files", () =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-asset-dimensions-" });
+ const png = Uint8Array.from([
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13, 0x49, 0x48, 0x44, 0x52, 0, 0,
+ 0x06, 0x40, 0, 0, 0x03, 0x84,
+ ]);
+ yield* fs.writeFile(path.join(root, "shot.png"), png);
+ yield* fs.writeFileString(path.join(root, "clip.html"), "<html></html>");
+ yield* fs.writeFileString(path.join(root, "broken.png"), "not a png");
+ const issue = (name: string) =>
+ issueAssetUrl({
+ resource: {
+ _tag: "workspace-file",
+ threadId: ThreadId.make("thread-1"),
+ path: path.join(root, name),
+ },
+ workspaceRoot: root,
+ });
+
+ expect((yield* issue("shot.png")).imageDimensions).toEqual({ width: 1600, height: 900 });
+ expect((yield* issue("clip.html")).imageDimensions).toBeUndefined();
+ expect((yield* issue("broken.png")).imageDimensions).toBeUndefined();
+ }).pipe(Effect.provide(testLayer)),
+ );
+
it.effect("rejects workspace files outside the authorized root", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
diff --git a/apps/server/src/assets/AssetAccess.ts b/apps/server/src/assets/AssetAccess.ts
index 232a41e5a..4ba414a97 100644
--- a/apps/server/src/assets/AssetAccess.ts
+++ b/apps/server/src/assets/AssetAccess.ts
@@ -19,6 +19,11 @@ import {
WORKSPACE_BROWSER_PREVIEW_EXTENSIONS,
WORKSPACE_IMAGE_PREVIEW_EXTENSIONS,
} from "@t3tools/shared/filePreview";
+import {
+ IMAGE_DIMENSIONS_HEADER_BYTES,
+ readImageDimensions,
+ type ImageDimensions,
+} from "@t3tools/shared/imageDimensions";
import { PROJECT_FAVICON_FALLBACK_MARKER } from "@t3tools/shared/projectFavicon";
import * as Clock from "effect/Clock";
import * as Crypto from "effect/Crypto";
@@ -183,6 +188,28 @@ const resolveCanonicalWorkspaceFileForRequest = (input: {
Effect.orElseSucceed(() => null),
);
+const HEADER_IMAGE_EXTENSIONS = new Set([".png", ".jpg", ".jpeg", ".gif", ".webp"]);
+
+/**
+ * Reads pixel dimensions from an image's header so clients can reserve the
+ * exact box before the bytes arrive. Best effort: an unreadable or unsupported
+ * file just leaves the field out, and the client measures after decode.
+ */
+const readImageDimensionsFromHeader = (filePath: string) =>
+ Effect.gen(function* () {
+ const path = yield* Path.Path;
+ if (!HEADER_IMAGE_EXTENSIONS.has(path.extname(filePath).toLowerCase())) {
+ return null;
+ }
+ const fileSystem = yield* FileSystem.FileSystem;
+ const bytes = yield* fileSystem.open(filePath).pipe(
+ Effect.flatMap((file) => file.readAlloc(IMAGE_DIMENSIONS_HEADER_BYTES)),
+ Effect.scoped,
+ Effect.map((chunk) => Option.getOrElse(chunk, () => new Uint8Array())),
+ );
+ return readImageDimensions(bytes);
+ }).pipe(Effect.orElseSucceed((): ImageDimensions | null => null));
+
export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (input: {
readonly resource: AssetResource;
readonly workspaceRoot?: string;
@@ -195,6 +222,7 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
let claims: AssetClaims;
let fileName: string;
let sourcePath: string | undefined;
+ let imageDimensions: ImageDimensions | null = null;
switch (input.resource._tag) {
case "workspace-file": {
@@ -248,6 +276,7 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
resource: input.resource,
});
}
+ imageDimensions = yield* readImageDimensionsFromHeader(canonicalFile);
const canonicalWorkspaceRoot = yield* fileSystem.realPath(workspaceRoot).pipe(
Effect.mapError(
(cause) =>
@@ -286,6 +315,7 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
resource: input.resource,
});
}
+ imageDimensions = yield* readImageDimensionsFromHeader(attachmentPath);
claims = {
version: 1,
kind: "attachment",
@@ -424,6 +454,7 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
relativeUrl: `${ASSET_ROUTE_PREFIX}/${token}/${encodeURIComponent(fileName)}`,
expiresAt,
...(sourcePath !== undefined ? { sourcePath } : {}),
+ ...(imageDimensions !== null ? { imageDimensions } : {}),
};
});
diff --git a/apps/web/src/assets/assetUrls.ts b/apps/web/src/assets/assetUrls.ts
index f8c0b5ae7..091997298 100644
--- a/apps/web/src/assets/assetUrls.ts
+++ b/apps/web/src/assets/assetUrls.ts
@@ -1,6 +1,6 @@
import { useAtomValue } from "@effect/atom-react";
import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
-import type { AssetResource, EnvironmentId } from "@t3tools/contracts";
+import type { AssetImageDimensions, AssetResource, EnvironmentId } from "@t3tools/contracts";
import { AsyncResult } from "effect/unstable/reactivity";
import { useMemo } from "react";
@@ -12,7 +12,12 @@ export { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
export type AssetUrlState =
| { readonly _tag: "Loading" }
| { readonly _tag: "Failure" }
- | { readonly _tag: "Success"; readonly url: string; readonly sourcePath?: string };
+ | {
+ readonly _tag: "Success";
+ readonly url: string;
+ readonly sourcePath?: string;
+ readonly imageDimensions?: AssetImageDimensions;
+ };
export function useAssetUrlState(
environmentId: EnvironmentId,
@@ -38,6 +43,9 @@ export function useAssetUrlState(
_tag: "Success",
url,
...(result.value.sourcePath !== undefined ? { sourcePath: result.value.sourcePath } : {}),
+ ...(result.value.imageDimensions !== undefined
+ ? { imageDimensions: result.value.imageDimensions }
+ : {}),
};
}
diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx
index 383200204..3440bfbee 100644
--- a/apps/web/src/components/ChatMarkdown.tsx
+++ b/apps/web/src/components/ChatMarkdown.tsx
@@ -1201,6 +1201,15 @@ const ChatMarkdownWorkspaceImage = memo(function ChatMarkdownWorkspaceImage(prop
/>
);
}
+ const knownSize = assetUrl.imageDimensions;
+ const sizeStyle = knownSize
+ ? {
+ width: knownSize.width,
+ height: "auto" as const,
+ aspectRatio: `${knownSize.width} / ${knownSize.height}`,
+ maxWidth: `min(100%, 30rem, ${(30 * knownSize.width) / knownSize.height}rem)`,
+ }
+ : undefined;
return (
<img
src={assetUrl.url}
@@ -1208,6 +1217,7 @@ const ChatMarkdownWorkspaceImage = memo(function ChatMarkdownWorkspaceImage(prop
loading="lazy"
draggable={false}
className={CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME}
+ style={sizeStyle}
onError={() => setFailedUrl(assetUrl.url)}
/>
);
diff --git a/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx b/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx
index 37a0f27a0..6e2d0047b 100644
--- a/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx
+++ b/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx
@@ -5,6 +5,7 @@ import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
const testState = vi.hoisted(() => ({
resources: [] as Array<unknown>,
assetState: "success" as "success" | "loading",
+ imageDimensions: undefined as { width: number; height: number } | undefined,
}));
vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null }));
@@ -13,7 +14,11 @@ vi.mock("../assets/assetUrls", () => ({
testState.resources.push(resource);
return testState.assetState === "loading"
? { _tag: "Loading" }
- : { _tag: "Success", url: "https://signed.test/workspace-image.svg" };
+ : {
+ _tag: "Success",
+ url: "https://signed.test/workspace-image.svg",
+ ...(testState.imageDimensions ? { imageDimensions: testState.imageDimensions } : {}),
+ };
},
}));
vi.mock("../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) }));
@@ -62,6 +67,7 @@ describe("ChatMarkdown workspace images", () => {
beforeEach(() => {
testState.resources = [];
testState.assetState = "success";
+ testState.imageDimensions = undefined;
});
it("loads every Windows workspace path form through a signed asset URL", () => {
@@ -117,6 +123,15 @@ describe("ChatMarkdown workspace images", () => {
expect(html).not.toContain("animate-pulse");
});
+ it("sizes the slot from server-reported dimensions so a portrait image never grows", () => {
+ testState.imageDimensions = { width: 720, height: 1400 };
+
+ const html = render("![shot](.t3/workspace-image.svg)");
+
+ expect(html).toContain("width:720px");
+ expect(html).toContain("aspect-ratio:720 / 1400");
+ });
+
it("never passes a workspace source to a raw image when thread context is unavailable", () => {
const html = renderWithoutThread(
"![file URL](file:///C:/Users/shawn/project/workspace-image.svg)",
diff --git a/packages/contracts/src/assets.ts b/packages/contracts/src/assets.ts
index 37bc67f98..661a7942e 100644
--- a/packages/contracts/src/assets.ts
+++ b/packages/contracts/src/assets.ts
@@ -33,12 +33,20 @@ export const AssetCreateUrlInput = Schema.Struct({
});
export type AssetCreateUrlInput = typeof AssetCreateUrlInput.Type;
+export const AssetImageDimensions = Schema.Struct({
+ width: NonNegativeInt.check(Schema.isGreaterThanOrEqualTo(1)),
+ height: NonNegativeInt.check(Schema.isGreaterThanOrEqualTo(1)),
+});
+export type AssetImageDimensions = typeof AssetImageDimensions.Type;
+
export const AssetCreateUrlResult = Schema.Struct({
relativeUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
expiresAt: Schema.Number,
sourcePath: Schema.optional(
TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
),
+ /** Pixel size read from the image header, so a client can reserve the exact box before the bytes arrive. */
+ imageDimensions: Schema.optional(AssetImageDimensions),
});
export type AssetCreateUrlResult = typeof AssetCreateUrlResult.Type;
diff --git a/packages/shared/package.json b/packages/shared/package.json
index 3773b16ac..fe83885d0 100644
--- a/packages/shared/package.json
+++ b/packages/shared/package.json
@@ -163,6 +163,10 @@
"types": "./src/filePreview.ts",
"import": "./src/filePreview.ts"
},
+ "./imageDimensions": {
+ "types": "./src/imageDimensions.ts",
+ "import": "./src/imageDimensions.ts"
+ },
"./chatList": {
"types": "./src/chatList.ts",
"import": "./src/chatList.ts"
diff --git a/packages/shared/src/imageDimensions.test.ts b/packages/shared/src/imageDimensions.test.ts
new file mode 100644
index 000000000..1b86bb974
--- /dev/null
+++ b/packages/shared/src/imageDimensions.test.ts
@@ -0,0 +1,140 @@
+import { describe, expect, it } from "vite-plus/test";
+
+import { readImageDimensions } from "./imageDimensions.ts";
+
+function bytes(...parts: ReadonlyArray<number | string | ReadonlyArray<number>>): Uint8Array {
+ const out: number[] = [];
+ for (const part of parts) {
+ if (typeof part === "string") for (const c of part) out.push(c.charCodeAt(0));
+ else if (typeof part === "number") out.push(part);
+ else out.push(...part);
+ }
+ return Uint8Array.from(out);
+}
+
+const u32 = (n: number) => [(n >>> 24) & 0xff, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff];
+const u16 = (n: number) => [(n >>> 8) & 0xff, n & 0xff];
+const u16le = (n: number) => [n & 0xff, (n >>> 8) & 0xff];
+
+describe("readImageDimensions", () => {
+ it("reads a PNG IHDR", () => {
+ const png = bytes(
+ [0x89],
+ "PNG",
+ [0x0d, 0x0a, 0x1a, 0x0a],
+ u32(13),
+ "IHDR",
+ u32(1600),
+ u32(900),
+ );
+ expect(readImageDimensions(png)).toEqual({ width: 1600, height: 900 });
+ });
+
+ it("reads a GIF logical screen", () => {
+ expect(readImageDimensions(bytes("GIF89a", u16le(320), u16le(240)))).toEqual({
+ width: 320,
+ height: 240,
+ });
+ });
+
+ it("reads a JPEG start-of-frame after an APP segment", () => {
+ const app1 = bytes([0xff, 0xe1], u16(2 + 6), "Exif\0\0");
+ const sof0 = bytes([0xff, 0xc0], u16(17), [8], u16(1400), u16(720));
+ expect(readImageDimensions(bytes([0xff, 0xd8], [...app1], [...sof0]))).toEqual({
+ width: 720,
+ height: 1400,
+ });
+ });
+
+ it("swaps the axes for a JPEG whose EXIF orientation rotates it 90 degrees", () => {
+ // Big-endian TIFF with one IFD0 entry: tag 0x0112 (orientation), SHORT, count 1, value 6.
+ const tiff = [
+ 0x4d, 0x4d, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x01, 0x12, 0x00, 0x03, 0x00,
+ 0x00, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ ];
+ const exif = ["Exif\0\0", tiff] as const;
+ const app1 = bytes([0xff, 0xe1], u16(2 + 6 + tiff.length), ...exif);
+ const sof0 = bytes([0xff, 0xc0], u16(17), [8], u16(3024), u16(4032));
+ expect(readImageDimensions(bytes([0xff, 0xd8], [...app1], [...sof0]))).toEqual({
+ width: 3024,
+ height: 4032,
+ });
+ // A later XMP APP1 segment must not clear the rotation.
+ const xmp = bytes([0xff, 0xe1], u16(2 + 29), "http://ns.adobe.com/xap/1.0/\0");
+ expect(readImageDimensions(bytes([0xff, 0xd8], [...app1], [...xmp], [...sof0]))).toEqual({
+ width: 3024,
+ height: 4032,
+ });
+ // Orientation 1 leaves the frame size alone.
+ const upright = [...tiff];
+ upright[19] = 0x01;
+ const app1Upright = bytes([0xff, 0xe1], u16(2 + 6 + upright.length), "Exif\0\0", upright);
+ expect(readImageDimensions(bytes([0xff, 0xd8], [...app1Upright], [...sof0]))).toEqual({
+ width: 4032,
+ height: 3024,
+ });
+ });
+
+ it("steps over standalone TEM and restart markers", () => {
+ const sof0 = bytes([0xff, 0xc0], u16(17), [8], u16(10), u16(20));
+ expect(readImageDimensions(bytes([0xff, 0xd8], [0xff, 0x01], [0xff, 0xd3], [...sof0]))).toEqual(
+ { width: 20, height: 10 },
+ );
+ });
+
+ it("does not mistake a Huffman table marker for a frame", () => {
+ const dht = bytes([0xff, 0xc4], u16(4), [0, 0]);
+ const sof2 = bytes([0xff, 0xc2], u16(17), [8], u16(10), u16(20));
+ expect(readImageDimensions(bytes([0xff, 0xd8], [...dht], [...sof2]))).toEqual({
+ width: 20,
+ height: 10,
+ });
+ });
+
+ it("reads each WebP container flavour", () => {
+ const riff = (chunk: string, body: ReadonlyArray<number>) =>
+ bytes("RIFF", u32(0), "WEBP", chunk, u32(body.length), body);
+ // VP8: frame tag (3), start code (3), then 14-bit width and height.
+ expect(
+ readImageDimensions(riff("VP8 ", [0, 0, 0, 0x9d, 0x01, 0x2a, ...u16le(800), ...u16le(600)])),
+ ).toEqual({ width: 800, height: 600 });
+ // VP8L: signature 0x2f, then width-1 (14 bits) and height-1 (14 bits) packed LE.
+ const packed = (800 - 1) | ((600 - 1) << 14);
+ expect(
+ readImageDimensions(
+ riff("VP8L", [
+ 0x2f,
+ packed & 0xff,
+ (packed >>> 8) & 0xff,
+ (packed >>> 16) & 0xff,
+ (packed >>> 24) & 0xff,
+ ]),
+ ),
+ ).toEqual({ width: 800, height: 600 });
+ // VP8X: flags (4), then 24-bit width-1 and height-1.
+ expect(
+ readImageDimensions(
+ riff("VP8X", [
+ 0,
+ 0,
+ 0,
+ 0,
+ 799 & 0xff,
+ (799 >> 8) & 0xff,
+ 0,
+ 599 & 0xff,
+ (599 >> 8) & 0xff,
+ 0,
+ ]),
+ ),
+ ).toEqual({ width: 800, height: 600 });
+ });
+
+ it("returns null for unsupported, truncated, or zero-sized input", () => {
+ expect(readImageDimensions(bytes("<svg xmlns='http://www.w3.org/2000/svg'/>"))).toBeNull();
+ expect(readImageDimensions(bytes([0x89], "PNG"))).toBeNull();
+ expect(readImageDimensions(bytes("GIF89a", u16le(0), u16le(240)))).toBeNull();
+ expect(readImageDimensions(bytes([0xff, 0xd8], [0xff, 0xd9]))).toBeNull();
+ expect(readImageDimensions(new Uint8Array())).toBeNull();
+ });
+});
diff --git a/packages/shared/src/imageDimensions.ts b/packages/shared/src/imageDimensions.ts
new file mode 100644
index 000000000..8eceb305b
--- /dev/null
+++ b/packages/shared/src/imageDimensions.ts
@@ -0,0 +1,154 @@
+/**
+ * Reads pixel dimensions from the header bytes of a PNG, JPEG, GIF, or WebP
+ * file so a client can reserve the exact box before the bytes arrive. Any
+ * other format, a truncated header, or a malformed file yields null; callers
+ * fall back to measuring after decode.
+ */
+export interface ImageDimensions {
+ readonly width: number;
+ readonly height: number;
+}
+
+/**
+ * Enough for every supported header. A JPEG's frame header can sit behind
+ * several 64 KiB metadata segments (EXIF, an ICC profile, XMP), so allow a
+ * few of them before giving up.
+ */
+export const IMAGE_DIMENSIONS_HEADER_BYTES = 256 * 1024;
+
+export function readImageDimensions(bytes: Uint8Array): ImageDimensions | null {
+ const dimensions = readPng(bytes) ?? readGif(bytes) ?? readWebp(bytes) ?? readJpeg(bytes);
+ return dimensions && dimensions.width > 0 && dimensions.height > 0 ? dimensions : null;
+}
+
+const view = (bytes: Uint8Array) => new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
+
+function readPng(bytes: Uint8Array): ImageDimensions | null {
+ if (bytes.length < 24) return null;
+ if (
+ bytes[0] !== 0x89 ||
+ bytes[1] !== 0x50 ||
+ bytes[2] !== 0x4e ||
+ bytes[3] !== 0x47 ||
+ bytes[12] !== 0x49 ||
+ bytes[13] !== 0x48 ||
+ bytes[14] !== 0x44 ||
+ bytes[15] !== 0x52
+ ) {
+ return null;
+ }
+ const data = view(bytes);
+ return { width: data.getUint32(16), height: data.getUint32(20) };
+}
+
+function readGif(bytes: Uint8Array): ImageDimensions | null {
+ if (bytes.length < 10) return null;
+ if (bytes[0] !== 0x47 || bytes[1] !== 0x49 || bytes[2] !== 0x46 || bytes[3] !== 0x38) return null;
+ const data = view(bytes);
+ return { width: data.getUint16(6, true), height: data.getUint16(8, true) };
+}
+
+function readWebp(bytes: Uint8Array): ImageDimensions | null {
+ if (bytes.length < 16) return null;
+ if (
+ bytes[0] !== 0x52 ||
+ bytes[1] !== 0x49 ||
+ bytes[2] !== 0x46 ||
+ bytes[3] !== 0x46 ||
+ bytes[8] !== 0x57 ||
+ bytes[9] !== 0x45 ||
+ bytes[10] !== 0x42 ||
+ bytes[11] !== 0x50
+ ) {
+ return null;
+ }
+ const data = view(bytes);
+ const chunk = String.fromCharCode(bytes[12]!, bytes[13]!, bytes[14]!, bytes[15]!);
+ switch (chunk) {
+ case "VP8 ":
+ if (bytes.length < 30) return null;
+ // Lossy: 14-bit dimensions after the 3-byte frame tag and 3-byte start code.
+ return {
+ width: data.getUint16(26, true) & 0x3fff,
+ height: data.getUint16(28, true) & 0x3fff,
+ };
+ case "VP8L": {
+ if (bytes.length < 25) return null;
+ // Lossless: width-1 in bits 0-13 and height-1 in bits 14-27 of the
+ // 32 bits after the signature byte.
+ const packed = data.getUint32(21, true);
+ return { width: (packed & 0x3fff) + 1, height: ((packed >>> 14) & 0x3fff) + 1 };
+ }
+ case "VP8X":
+ if (bytes.length < 30) return null;
+ // Extended: 24-bit canvas dimensions minus one.
+ return {
+ width: (bytes[24]! | (bytes[25]! << 8) | (bytes[26]! << 16)) + 1,
+ height: (bytes[27]! | (bytes[28]! << 8) | (bytes[29]! << 16)) + 1,
+ };
+ default:
+ return null;
+ }
+}
+
+function readJpeg(bytes: Uint8Array): ImageDimensions | null {
+ if (bytes.length < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8) return null;
+ const data = view(bytes);
+ let offset = 2;
+ let rotated = false;
+ while (offset + 9 <= bytes.length) {
+ if (bytes[offset] !== 0xff) return null;
+ const marker = bytes[offset + 1]!;
+ // Padding bytes between segments.
+ if (marker === 0xff) {
+ offset += 1;
+ continue;
+ }
+ // Start-of-frame markers carry the dimensions; skip the arithmetic-coding
+ // and Huffman-table markers that share the range.
+ if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {
+ const height = data.getUint16(offset + 5);
+ const width = data.getUint16(offset + 7);
+ return rotated ? { width: height, height: width } : { width, height };
+ }
+ if (marker === 0xd9 || marker === 0xda) return null;
+ // TEM and the restart markers stand alone, with no length field.
+ if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
+ offset += 2;
+ continue;
+ }
+ const length = data.getUint16(offset + 2);
+ // Viewers apply the EXIF orientation before display, so a phone photo
+ // stored on its side takes the swapped size on screen.
+ if (marker === 0xe1 && !rotated) {
+ rotated = exifOrientationSwapsAxes(bytes, offset + 4, offset + 2 + length);
+ }
+ offset += 2 + length;
+ }
+ return null;
+}
+
+/** Whether EXIF orientation 5-8 (a 90° rotation) applies. `start` is the APP1 payload. */
+function exifOrientationSwapsAxes(bytes: Uint8Array, start: number, end: number): boolean {
+ end = Math.min(end, bytes.length);
+ // "Exif\0\0" then a TIFF header: byte order, 0x2a, and the IFD0 offset.
+ if (end - start < 14 || String.fromCharCode(...bytes.subarray(start, start + 4)) !== "Exif") {
+ return false;
+ }
+ const tiff = start + 6;
+ const data = view(bytes);
+ const littleEndian = bytes[tiff] === 0x49 && bytes[tiff + 1] === 0x49;
+ if (!littleEndian && !(bytes[tiff] === 0x4d && bytes[tiff + 1] === 0x4d)) return false;
+ const ifd = tiff + data.getUint32(tiff + 4, littleEndian);
+ if (ifd + 2 > end) return false;
+ const entries = data.getUint16(ifd, littleEndian);
+ for (let i = 0; i < entries; i += 1) {
+ const entry = ifd + 2 + i * 12;
+ if (entry + 12 > end) return false;
+ if (data.getUint16(entry, littleEndian) === 0x0112) {
+ const orientation = data.getUint16(entry + 8, littleEndian);
+ return orientation >= 5 && orientation <= 8;
+ }
+ }
+ return false;
+}