feat(server): report image dimensions with signed asset URLs #10198
Mergedjuliusmarmingemerged 4 commits into
main fromrefs/pull/10198/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge changed the title
- apps/server/src/assets/MediaFile.ts line 106OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -95,3 +95,12 @@9595 );9696 });979798+/** Reads the leading bytes of an already-validated media file, never past the end. */99+export const readMediaFileHeader = (file: OpenMediaFile, byteCount: number) =>100+ Effect.tryPromise({101+ try: async () => {102+ const buffer = new Uint8Array(byteCount);103+ const { bytesRead } = await file.handle.read(buffer, 0, byteCount, 0);104+ return buffer.subarray(0, bytesRead);105+ },106+ catch: (cause) => new MediaFileStatError({ path: "<open handle>", cause }),
- packages/shared/src/imageDimensions.ts lines 114 to 120Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +91,21 @@91+ if (bytes.length < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8) return null;92+ const data = view(bytes);93+ let offset = 2;94+ let rotated = false;95+ while (offset + 9 <= bytes.length) {96+ if (bytes[offset] !== 0xff) return null;97+ const marker = bytes[offset + 1]!;98+ // Padding bytes between segments.99+ if (marker === 0xff) {100+ offset += 1;101+ continue;102+ }103+ // Start-of-frame markers carry the dimensions; skip the arithmetic-coding104+ // and Huffman-table markers that share the range.105+ if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {106+ const height = data.getUint16(offset + 5);107+ const width = data.getUint16(offset + 7);108+ return rotated ? { width: height, height: width } : { width, height };109+ }110+ if (marker === 0xd9 || marker === 0xda) return null;111+ const length = data.getUint16(offset + 2);apps/server/src/assets/AssetAccess.ts line 400Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -347,3 +394,5 @@347394 }),348395 ),349396 );397+ if (HEADER_IMAGE_EXTENSIONS.has(path.extname(resolved.relativePath).toLowerCase())) {398+ imageDimensions = yield* readImageDimensionsFromHeader(canonicalFile);
- packages/shared/src/imageDimensions.ts line 13OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,13 @@1+/**2+ * Reads pixel dimensions from the header bytes of a PNG, JPEG, GIF, or WebP3+ * file so a client can reserve the exact box before the bytes arrive. Any4+ * other format, a truncated header, or a malformed file yields null; callers5+ * fall back to measuring after decode.6+ */7+export interface ImageDimensions {8+ readonly width: number;9+ readonly height: number;10+}11+12+/** Enough for every supported header, including JPEG files with an EXIF/ICC prefix. */13+export const IMAGE_DIMENSIONS_HEADER_BYTES = 64 * 1024;packages/shared/src/imageDimensions.ts lines 119 to 119OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +99,21 @@99+ if (marker === 0xff) {100+ offset += 1;101+ continue;102+ }103+ // Start-of-frame markers carry the dimensions; skip the arithmetic-coding104+ // and Huffman-table markers that share the range.105+ if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {106+ const height = data.getUint16(offset + 5);107+ const width = data.getUint16(offset + 7);108+ return rotated ? { width: height, height: width } : { width, height };109+ }110+ if (marker === 0xd9 || marker === 0xda) return null;111+ // TEM and the restart markers stand alone, with no length field.112+ if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {113+ offset += 2;114+ continue;115+ }116+ const length = data.getUint16(offset + 2);117+ // Viewers apply the EXIF orientation before display, so a phone photo118+ // stored on its side takes the swapped size on screen.119+ if (marker === 0xe1) rotated = exifOrientationSwapsAxes(bytes, offset + 4, offset + 2 + length);
juliusmarminge merged thise2e6ce6
juliusmarminge closed this
Sign in to join this conversation.
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline:
f8b4c46· PR result:0c0d4ea· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.